-
Notifications
You must be signed in to change notification settings - Fork 9
/
BidirectionalStepwiseSelection.py
242 lines (192 loc) · 10.3 KB
/
BidirectionalStepwiseSelection.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
#Copyright 2019 Emirhan Kartal
#
#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 BidirectionalStepwiseSelection(X, y, model_type ="linear", elimination_criteria = "aic", varchar_process = "dummy_dropfirst", senter=0.05, sstay=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
senter : Significance Level to be Selected in Forward Selection (default: 0.05)\n
sstay : Significance Level to be Stayed in Backward Elimination (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 __stepwiseSelectionRaw__(X, y, model_type = model_type,elimination_criteria = elimination_criteria , senter = senter, sstay = sstay)
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 __stepwiseSelectionRaw__(X, y, model_type ="linear",elimination_criteria = "aic", senter=0.05, sstay=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<= senter]
if pvals.shape[0] > 0:
model = regressor(y, X[selected_cols+[pvals["Cols"][0]]])
iterations_log += "\n" + str(i+1) + ". iteration"
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"
for k in range(X[selected_cols+[pvals["Cols"][0]]].shape[1]):
cols_backward = X[selected_cols+[pvals["Cols"][0]]].columns.tolist()
pvals_backward = pd.DataFrame(columns = ["bw_Cols","bw_Pval"])
for l in cols_backward:
pvals_backward = pvals_backward.append(pd.DataFrame([[l, model.pvalues[l]]],columns= ["bw_Cols","bw_Pval"]),ignore_index=True)
pvals_backward = pvals_backward.sort_values(by = ["bw_Pval"]).reset_index(drop=True)
pvals_backward_drop = pvals_backward[pvals_backward.bw_Pval > sstay].reset_index(drop=True)
if pvals_backward_drop.shape[0] > 0:
iterations_log += str("\nBackward Step Dropped : "+pvals_backward_drop["bw_Cols"][0] + "\n")
if pvals_backward_drop["bw_Cols"][0] in other_cols:
other_cols.remove(pvals_backward_drop["bw_Cols"][0])
else:
pass
if pvals_backward_drop["bw_Cols"][0] in selected_cols:
selected_cols.remove(pvals_backward_drop["bw_Cols"][0])
else:
pass
model = regressor(y, X[selected_cols+[pvals["Cols"][0]]])
iterations_log += "\n\n"+str(model.summary())+"\nAIC: "+ str(model.aic) + "\nBIC: "+ str(model.bic)+"\n\n"
else:
print("break : No need to Backward Elimination")
break
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)
iterations_log += "\nFinal Selection and Model Statistics\n"
for m in range(len(selected_cols)):
iterations_log += "\n" + str(m+1) + str(". Final Variable : "+ selected_cols[m])
iterations_log += "\n\n"+str(model.summary())+"\nAIC: "+ str(model.aic) + "\nBIC: "+ str(model.bic)+"\n\n"
return selected_cols, iterations_log