-
Notifications
You must be signed in to change notification settings - Fork 21
/
stepwiseSelection.py
310 lines (270 loc) · 13.4 KB
/
stepwiseSelection.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#Copyright 2019 Sinan Talha Hascelik
#
#Licensed under the Apache License, Version 2.0 (the "License");
#you may not use this file except in compliance with the License.
#You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
#Unless required by applicable law or agreed to in writing, software
#distributed under the License is distributed on an "AS IS" BASIS,
#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#See the License for the specific language governing permissions and
#limitations under the License.
import numpy as np
import pandas as pd
import statsmodels.formula.api as sm
def forwardSelection(X, y, model_type ="linear",elimination_criteria = "aic", varchar_process = "dummy_dropfirst", sl=0.05):
"""
Forward Selection is a function, based on regression models, that returns significant features and selection iterations.\n
Required Libraries: pandas, numpy, statmodels
Parameters
----------
X : Independent variables (Pandas Dataframe)\n
y : Dependent variable (Pandas Series, Pandas Dataframe)\n
model_type : 'linear' or 'logistic'\n
elimination_criteria : 'aic', 'bic', 'r2', 'adjr2' or None\n
'aic' refers Akaike information criterion\n
'bic' refers Bayesian information criterion\n
'r2' refers R-squared (Only works on linear model type)\n
'r2' refers Adjusted R-squared (Only works on linear model type)\n
varchar_process : 'drop', 'dummy' or 'dummy_dropfirst'\n
'drop' drops varchar features\n
'dummy' creates dummies for all levels of all varchars\n
'dummy_dropfirst' creates dummies for all levels of all varchars, and drops first levels\n
sl : Significance Level (default: 0.05)\n
Returns
-------
columns(list), iteration_logs(str)\n\n
Not Returns a Model
Tested On
---------
Python v3.6.7, Pandas v0.23.4, Numpy v1.15.04, StatModels v0.9.0
See Also
--------
https://en.wikipedia.org/wiki/Stepwise_regression
"""
X = __varcharProcessing__(X,varchar_process = varchar_process)
return __forwardSelectionRaw__(X, y, model_type = model_type,elimination_criteria = elimination_criteria , sl=sl)
def backwardSelection(X, y, model_type ="linear",elimination_criteria = "aic", varchar_process = "dummy_dropfirst", sl=0.05):
"""
Backward Selection is a function, based on regression models, that returns significant features and selection iterations.\n
Required Libraries: pandas, numpy, statmodels
Parameters
----------
X : Independent variables (Pandas Dataframe)\n
y : Dependent variable (Pandas Series, Pandas Dataframe)\n
model_type : 'linear' or 'logistic'\n
elimination_criteria : 'aic', 'bic', 'r2', 'adjr2' or None\n
'aic' refers Akaike information criterion\n
'bic' refers Bayesian information criterion\n
'r2' refers R-squared (Only works on linear model type)\n
'r2' refers Adjusted R-squared (Only works on linear model type)\n
varchar_process : 'drop', 'dummy' or 'dummy_dropfirst'\n
'drop' drops varchar features\n
'dummy' creates dummies for all levels of all varchars\n
'dummy_dropfirst' creates dummies for all levels of all varchars, and drops first levels\n
sl : Significance Level (default: 0.05)\n
Returns
-------
columns(list), iteration_logs(str)\n\n
Not Returns a Model
Tested On
---------
Python v3.6.7, Pandas v0.23.4, Numpy v1.15.04, StatModels v0.9.0
See Also
--------
https://en.wikipedia.org/wiki/Stepwise_regression
"""
X = __varcharProcessing__(X,varchar_process = varchar_process)
return __backwardSelectionRaw__(X, y, model_type = model_type,elimination_criteria = elimination_criteria , sl=sl)
def __varcharProcessing__(X, varchar_process = "dummy_dropfirst"):
dtypes = X.dtypes
if varchar_process == "drop":
X = X.drop(columns = dtypes[dtypes == np.object].index.tolist())
print("Character Variables (Dropped):", dtypes[dtypes == np.object].index.tolist())
elif varchar_process == "dummy":
X = pd.get_dummies(X,drop_first=False)
print("Character Variables (Dummies Generated):", dtypes[dtypes == np.object].index.tolist())
elif varchar_process == "dummy_dropfirst":
X = pd.get_dummies(X,drop_first=True)
print("Character Variables (Dummies Generated, First Dummies Dropped):", dtypes[dtypes == np.object].index.tolist())
else:
X = pd.get_dummies(X,drop_first=True)
print("Character Variables (Dummies Generated, First Dummies Dropped):", dtypes[dtypes == np.object].index.tolist())
X["intercept"] = 1
cols = X.columns.tolist()
cols = cols[-1:] + cols[:-1]
X = X[cols]
return X
def __forwardSelectionRaw__(X, y, model_type ="linear",elimination_criteria = "aic", sl=0.05):
iterations_log = ""
cols = X.columns.tolist()
def regressor(y,X, model_type=model_type):
if model_type == "linear":
regressor = sm.OLS(y, X).fit()
elif model_type == "logistic":
regressor = sm.Logit(y, X).fit()
else:
print("\nWrong Model Type : "+ model_type +"\nLinear model type is seleted.")
model_type = "linear"
regressor = sm.OLS(y, X).fit()
return regressor
selected_cols = ["intercept"]
other_cols = cols.copy()
other_cols.remove("intercept")
model = regressor(y, X[selected_cols])
if elimination_criteria == "aic":
criteria = model.aic
elif elimination_criteria == "bic":
criteria = model.bic
elif elimination_criteria == "r2" and model_type =="linear":
criteria = model.rsquared
elif elimination_criteria == "adjr2" and model_type =="linear":
criteria = model.rsquared_adj
for i in range(X.shape[1]):
pvals = pd.DataFrame(columns = ["Cols","Pval"])
for j in other_cols:
model = regressor(y, X[selected_cols+[j]])
pvals = pvals.append(pd.DataFrame([[j, model.pvalues[j]]],columns = ["Cols","Pval"]),ignore_index=True)
pvals = pvals.sort_values(by = ["Pval"]).reset_index(drop=True)
pvals = pvals[pvals.Pval<=sl]
if pvals.shape[0] > 0:
model = regressor(y, X[selected_cols+[pvals["Cols"][0]]])
iterations_log += str("\nEntered : "+pvals["Cols"][0] + "\n")
iterations_log += "\n\n"+str(model.summary())+"\nAIC: "+ str(model.aic) + "\nBIC: "+ str(model.bic)+"\n\n"
if elimination_criteria == "aic":
new_criteria = model.aic
if new_criteria < criteria:
print("Entered :", pvals["Cols"][0], "\tAIC :", model.aic)
selected_cols.append(pvals["Cols"][0])
other_cols.remove(pvals["Cols"][0])
criteria = new_criteria
else:
print("break : Criteria")
break
elif elimination_criteria == "bic":
new_criteria = model.bic
if new_criteria < criteria:
print("Entered :", pvals["Cols"][0], "\tBIC :", model.bic)
selected_cols.append(pvals["Cols"][0])
other_cols.remove(pvals["Cols"][0])
criteria = new_criteria
else:
print("break : Criteria")
break
elif elimination_criteria == "r2" and model_type =="linear":
new_criteria = model.rsquared
if new_criteria > criteria:
print("Entered :", pvals["Cols"][0], "\tR2 :", model.rsquared)
selected_cols.append(pvals["Cols"][0])
other_cols.remove(pvals["Cols"][0])
criteria = new_criteria
else:
print("break : Criteria")
break
elif elimination_criteria == "adjr2" and model_type =="linear":
new_criteria = model.rsquared_adj
if new_criteria > criteria:
print("Entered :", pvals["Cols"][0], "\tAdjR2 :", model.rsquared_adj)
selected_cols.append(pvals["Cols"][0])
other_cols.remove(pvals["Cols"][0])
criteria = new_criteria
else:
print("Break : Criteria")
break
else:
print("Entered :", pvals["Cols"][0])
selected_cols.append(pvals["Cols"][0])
other_cols.remove(pvals["Cols"][0])
else:
print("Break : Significance Level")
break
model = regressor(y, X[selected_cols])
if elimination_criteria == "aic":
criteria = model.aic
elif elimination_criteria == "bic":
criteria = model.bic
elif elimination_criteria == "r2" and model_type =="linear":
criteria = model.rsquared
elif elimination_criteria == "adjr2" and model_type =="linear":
criteria = model.rsquared_adj
print(model.summary())
print("AIC: "+str(model.aic))
print("BIC: "+str(model.bic))
print("Final Variables:", selected_cols)
return selected_cols, iterations_log
def __backwardSelectionRaw__(X, y, model_type ="linear",elimination_criteria = "aic", sl=0.05):
iterations_log = ""
last_eleminated = ""
cols = X.columns.tolist()
def regressor(y,X, model_type=model_type):
if model_type =="linear":
regressor = sm.OLS(y, X).fit()
elif model_type == "logistic":
regressor = sm.Logit(y, X).fit()
else:
print("\nWrong Model Type : "+ model_type +"\nLinear model type is seleted.")
model_type = "linear"
regressor = sm.OLS(y, X).fit()
return regressor
for i in range(X.shape[1]):
if i != 0 :
if elimination_criteria == "aic":
criteria = model.aic
new_model = regressor(y,X)
new_criteria = new_model.aic
if criteria < new_criteria:
print("Regained : ", last_eleminated)
iterations_log += "\n"+str(new_model.summary())+"\nAIC: "+ str(new_model.aic) + "\nBIC: "+ str(new_model.bic)+"\n"
iterations_log += str("\n\nRegained : "+last_eleminated + "\n\n")
break
elif elimination_criteria == "bic":
criteria = model.bic
new_model = regressor(y,X)
new_criteria = new_model.bic
if criteria < new_criteria:
print("Regained : ", last_eleminated)
iterations_log += "\n"+str(new_model.summary())+"\nAIC: "+ str(new_model.aic) + "\nBIC: "+ str(new_model.bic)+"\n"
iterations_log += str("\n\nRegained : "+last_eleminated + "\n\n")
break
elif elimination_criteria == "adjr2" and model_type =="linear":
criteria = model.rsquared_adj
new_model = regressor(y,X)
new_criteria = new_model.rsquared_adj
if criteria > new_criteria:
print("Regained : ", last_eleminated)
iterations_log += "\n"+str(new_model.summary())+"\nAIC: "+ str(new_model.aic) + "\nBIC: "+ str(new_model.bic)+"\n"
iterations_log += str("\n\nRegained : "+last_eleminated + "\n\n")
break
elif elimination_criteria == "r2" and model_type =="linear":
criteria = model.rsquared
new_model = regressor(y,X)
new_criteria = new_model.rsquared
if criteria > new_criteria:
print("Regained : ", last_eleminated)
iterations_log += "\n"+str(new_model.summary())+"\nAIC: "+ str(new_model.aic) + "\nBIC: "+ str(new_model.bic)+"\n"
iterations_log += str("\n\nRegained : "+last_eleminated + "\n\n")
break
else:
new_model = regressor(y,X)
model = new_model
iterations_log += "\n"+str(model.summary())+"\nAIC: "+ str(model.aic) + "\nBIC: "+ str(model.bic)+"\n"
else:
model = regressor(y,X)
iterations_log += "\n"+str(model.summary())+"\nAIC: "+ str(model.aic) + "\nBIC: "+ str(model.bic)+"\n"
maxPval = max(model.pvalues)
cols = X.columns.tolist()
if maxPval > sl:
for j in cols:
if (model.pvalues[j] == maxPval):
print("Eliminated :" ,j)
iterations_log += str("\n\nEliminated : "+j+ "\n\n")
del X[j]
last_eleminated = j
else:
break
print(str(model.summary())+"\nAIC: "+ str(model.aic) + "\nBIC: "+ str(model.bic))
print("Final Variables:", cols)
iterations_log += "\n"+str(model.summary())+"\nAIC: "+ str(model.aic) + "\nBIC: "+ str(model.bic)+"\n"
return cols, iterations_log