-
Notifications
You must be signed in to change notification settings - Fork 0
/
featureImportance.py
370 lines (285 loc) · 12.7 KB
/
featureImportance.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#Feature Importance
#Feature importance refers to a class of techniques for assigning scores to input features to a predictive model that indicates the relative #importance of each feature when making a prediction.
#Feature importance scores can be calculated for problems that involve predicting a numerical value, called regression, and those problems that #involve predicting a class label, called classification.
#Coefficients as Feature Importance
#Linear machine learning algorithms fit a model where the prediction is the weighted sum of the input values.
#Examples include linear regression, logistic regression, and extensions that add regularization, such as ridge regression and the elastic net.
#All of these algorithms find a set of coefficients to use in the weighted sum in order to make a prediction. These coefficients can be used #directly as a crude type of feature importance score.
#Let’s take a closer look at using coefficients as feature importance for classification and regression. We will fit a model on the dataset to #find the coefficients, then summarize the importance scores for each input feature and finally create a bar chart to get an idea of the #relative importance of the features.#
# linear regression feature importance
from sklearn.linear_model import LinearRegression
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeRegressor
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestRegressor
from sklearn.ensemble import RandomForestClassifier
from xgboost import XGBRegressor
from xgboost import XGBClassifier
from sklearn.datasets import make_classification
from sklearn.neighbors import KNeighborsRegressor
from sklearn.neighbors import KNeighborsClassifier
from sklearn.inspection import permutation_importance
from sklearn.model_selection import train_test_split
from sklearn.feature_selection import SelectFromModel
from sklearn.metrics import accuracy_score
import numpy as np
import pandas as pd
from matplotlib import pyplot
import statsmodels.formula.api as smf
import warnings
#suppress warnings
warnings.filterwarnings('ignore')
class FeatureImportance:
def __init__(self):
print("calling __init__() constructor...")
#Linear Regression Feature Importance
#We can fit a LinearRegression model on the regression dataset and retrieve the coeff_ property that contains
#the coefficients found for each #input variable.
#These coefficients can provide the basis for a crude feature importance score. This assumes that
#the input variables have the same scale or #have been scaled prior to fitting a model.
def LinearRegresssionFeatureImportance(self,X, y):
#Inputs
# X - Input feature data
# y - Target Variable
# define the model
model = LinearRegression()
# fit the model
model.fit(X, y)
# get importance
importance = model.coef_
# summarize feature importance
for i,v in enumerate(importance):
print('Feature: %0d, Score: %.5f' % (i,v))
# plot feature importance
pyplot.bar([x for x in range(len(importance))], importance)
pyplot.show()
#Logistic Regression Feature Importance
#We can fit a LogisticRegression model on the regression dataset and retrieve
#the coeff_ property that contains the coefficients found for each input variable.
#These coefficients can provide the basis for a crude feature importance score.
#This assumes that the input variables have the same scale or have been scaled prior to fitting a model.
def logisticRegressionFeatureImportance(self,X, y):
#Inputs
# X - Input feature data
# y - Target Variable
# define the model
model = LogisticRegression()
# fit the model
model.fit(X, y)
# get importance
importance = model.coef_[0]
# summarize feature importance
for i,v in enumerate(importance):
print('Feature: %0d, Score: %.5f' % (i,v))
# plot feature importance
pyplot.bar([x for x in range(len(importance))], importance)
pyplot.show()
#CART Regression Feature Importance
#The complete example of fitting a DecisionTreeRegressor and summarizing
#the calculated feature importance scores.
def cartRegressionFeatureImportance(self,X, y):
#Inputs
# X - Input feature data
# y - Target Variable
# decision tree for feature importance on a regression problem
# define the model
model = DecisionTreeRegressor()
# fit the model
model.fit(X, y)
# get importance
importance = model.feature_importances_
# summarize feature importance
for i,v in enumerate(importance):
print('Feature: %0d, Score: %.5f' % (i,v))
# plot feature importance
pyplot.bar([x for x in range(len(importance))], importance)
pyplot.show()
#CART Classification Feature Importance
#The complete example of fitting a DecisionTreeClassifier and
#summarizing the calculated feature importance scores.
def cartClassificationFeatureImportance(self,X, y):
#Inputs
# X - Input feature data
# y - Target Variable
# define the model
model = DecisionTreeClassifier()
# fit the model
model.fit(X, y)
# get importance
importance = model.feature_importances_
# summarize feature importance
for i,v in enumerate(importance):
print('Feature: %0d, Score: %.5f' % (i,v))
# plot feature importance
pyplot.bar([x for x in range(len(importance))], importance)
pyplot.show()
#Random Forest Regression Feature Importance
#The complete example of fitting a RandomForestRegressor
#and summarizing the calculated feature importance scores.
def randomForestRegressionFeatureImportance(self,X, y):
#Inputs
# X - Input feature data
# y - Target Variable
# define the model
model = RandomForestRegressor()
# fit the model
model.fit(X, y)
# get importance
importance = model.feature_importances_
# summarize feature importance
for i,v in enumerate(importance):
print('Feature: %0d, Score: %.5f' % (i,v))
# plot feature importance
pyplot.bar([x for x in range(len(importance))], importance)
pyplot.show()
#Random Forest Classification Feature Importance
#The complete example of fitting a RandomForestClassifier
#and summarizing the calculated feature importance scores.
def randomForestClassificationFeatureImportance(self,X, y):
#Inputs
# X - Input feature data
# y - Target Variable
# define the model
model = RandomForestClassifier()
# fit the model
model.fit(X, y)
# get importance
importance = model.feature_importances_
# summarize feature importance
for i,v in enumerate(importance):
print('Feature: %0d, Score: %.5f' % (i,v))
# plot feature importance
pyplot.bar([x for x in range(len(importance))], importance)
pyplot.show()
#XGBoost Regression Feature Importance
#The complete example of fitting a XGBRegressor
#and summarizing the calculated feature importance scores.
def xgBoostRegressionFeatureImportance(self,X, y):
#Inputs
# X - Input feature data
# y - Target Variable
# define the model
model = XGBRegressor()
# fit the model
model.fit(X, y)
# get importance
importance = model.feature_importances_
# summarize feature importance
for i,v in enumerate(importance):
print('Feature: %0d, Score: %.5f' % (i,v))
# plot feature importance
pyplot.bar([x for x in range(len(importance))], importance)
pyplot.show()
#XGBoost Classification Feature Importance
#The complete example of fitting an XGBClassifier
#and summarizing the calculated feature importance scores is listed below.
def xgBoostClassificationFeatureImportance(self,X, y):
#Inputs
# X - Input feature data
# y - Target Variable
# define the model
model = XGBClassifier()
# fit the model
model.fit(X, y)
# get importance
importance = model.feature_importances_
# summarize feature importance
for i,v in enumerate(importance):
print('Feature: %0d, Score: %.5f' % (i,v))
# plot feature importance
pyplot.bar([x for x in range(len(importance))], importance)
pyplot.show()
#Permutation Feature Importance for Regression
#The complete example of fitting a KNeighborsRegressor
#and summarizing the calculated permutation feature importance scores is listed below.
def permutationFeatureImportanceRegression(self,X, y):
#Inputs
# X - Input feature data
# y - Target Variable
# define the model
model = KNeighborsRegressor()
# fit the model
model.fit(X, y)
# perform permutation importance
results = permutation_importance(model, X, y, scoring='neg_mean_squared_error')
# get importance
importance = results.importances_mean
# summarize feature importance
for i,v in enumerate(importance):
print('Feature: %0d, Score: %.5f' % (i,v))
# plot feature importance
pyplot.bar([x for x in range(len(importance))], importance)
pyplot.show()
#Permutation Feature Importance for Classification
#The complete example of fitting a KNeighborsClassifier
#and summarizing the calculated permutation feature importance scores is listed below.
def permutationFeatureImportanceClassification(self,X, y):
#Inputs
# X - Input feature data
# y - Target Variable
# define the model
model = KNeighborsClassifier()
# fit the model
model.fit(X, y)
# perform permutation importance
results = permutation_importance(model, X, y, scoring='accuracy')
# get importance
importance = results.importances_mean
# summarize feature importance
for i,v in enumerate(importance):
print('Feature: %0d, Score: %.5f' % (i,v))
# plot feature importance
pyplot.bar([x for x in range(len(importance))], importance)
pyplot.show()
#Feature Selection with Importance
#Feature importance scores can be used to help interpret the data,
#but they can also be used directly to help rank and select features that are most useful to a predictive model.
# feature selection
def select_features(self, X_train, y_train, X_test):
#Inputs
# X_train - Train input feature data
# X_test - Test input feature data
# y_train - Train Target Variable
# configure to select a subset of features
fs = SelectFromModel(RandomForestClassifier(n_estimators=1000), max_features=5)
# learn relationship from training data
fs.fit(X_train, y_train)
# transform train input data
X_train_fs = fs.transform(X_train)
# transform test input data
X_test_fs = fs.transform(X_test)
return X_train_fs, X_test_fs, fs
def featureSelectionwithImportance(self,X, y):
#Inputs
# X - Input feature data
# y - Target Variable
# define the dataset
#X, y = make_classification(n_samples=1000, n_features=10, n_informative=5, n_redundant=5, random_state=1)
# split into train and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=1)
# feature selection
X_train_fs, X_test_fs, fs = self.select_features(X_train, y_train, X_test)
# fit the model
model = LogisticRegression(solver='liblinear')
model.fit(X_train_fs, y_train)
# evaluate the model
yhat = model.predict(X_test_fs)
# evaluate predictions
accuracy = accuracy_score(y_test, yhat)
return (accuracy*100)
#print('Accuracy: %.2f' % (accuracy*100))
def oddsRatios(self,data,varInput):
#Inputs
# data - Input feature dataframe
# varInput - columns to columns to parse
log_reg = smf.logit( varInput, data=data).fit()
# ... Define and fit model
odds_ratios = pd.DataFrame(
{
"OR": log_reg.params,
"Lower CI": log_reg.conf_int()[0],
"Upper CI": log_reg.conf_int()[1],
}
)
#odds_ratios = np.exp(odds_ratios)
return odds_ratios