-
Notifications
You must be signed in to change notification settings - Fork 0
/
feature_importance.py
316 lines (284 loc) · 15.5 KB
/
feature_importance.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
# Libraries
import pandas as pd
import numpy as np
import os
import seaborn as sns
import xgboost as xgb
import matplotlib.pyplot as plt
from matplotlib import pyplot
from sklearn.inspection import permutation_importance
import shap
import pickle
# Feature Importance
# --------------------------- Read data files --------------------------- #
class FeatureImportance:
"""
A class object to plot feature importances
Parameters:
----------
custom_name: str
A custom name represnting the running code instance
best_model: str
The name of the best model identfied by k-fold cross validation
"""
def __init__(self, custom_name: str, best_model: str):
pd.options.display.max_columns = 30
self.custom_name = custom_name
self.best_model = best_model
# ___________________________________________________
# Check directories
if not os.path.isdir(os.path.join(os.getcwd(),self.custom_name,"img/feature_importance/")):
os.mkdir(os.path.join(os.getcwd(),self.custom_name,"img/feature_importance/"))
def plotImportance(self, model: any, out_features: str,
train_x: pd.DataFrame, train_y: pd.DataFrame) -> None:
"""
Plot the feature importances
Parameters:
----------
model: any
ML structure and weights
out_features: str
Name of the FHG coeficients
train_x : pd.DataFrame
splited predictor data for training
train_y : np.array
splited target data for training
Example:
----------
>>> plotImportance(model, 'b', train_x, train_y)
"""
# ___________________________________________________
# model feature importance
if self.best_model == 'xgb':
print("Plotting XGB default feature importance for {0} -------------- ".format(str(out_features)))
xgb.plot_importance(model, grid=False)
plt.savefig(self.custom_name+'/img/feature_importance/'+str(self.custom_name)+'_'+out_features+'_'+str(self.best_model)+'.png',bbox_inches='tight', dpi = 600, facecolor='white')
pyplot.show()
plt.show()
# ___________________________________________________
# Permutation feature importance
print("Plotting Permutation feature importance for {0} -------------- ".format(str(out_features)))
results = permutation_importance(model, train_x, train_y, scoring='neg_mean_absolute_error')
importance = results.importances_mean
col_list = []
for col in train_x.columns:
col_list.append(col)
col_list = np.array(col_list)
col_list
feat_permut = pd.DataFrame.from_dict({"features":col_list, "p_score": importance}).sort_values(by=['p_score'])
fig, ax = plt.subplots(figsize=(30,25))
fig.tight_layout(pad=5)
# ___________________________________________________
# Creating a case-specific function to avoid code repetition
def plot_feat(x: str, y: str, xlabel: str, ylabel: str, rotation: int,
tick_bottom: bool, tick_left: bool) -> None:
"""
A simple bar plot
Parameters:
----------
x: str
x axis title
y: str
y axis title
xlabel: str
Label for x axis
ylabel: str
Label for y axis
rotation: int
rotation of labels in degrees
tick_bottom: bool
To have ticks on x or not
options:
- True
- False
tick_left: bool
To have ticks on y or not
options:
- True
- False
Example:
----------
>>> plot_feat(x='p_score', y='features',
xlabel='Importance', ylabel=None,
rotation=None, tick_bottom=True, tick_left=False)
"""
sns.barplot(x=x, y=y, data=feat_permut, color='slateblue')
plt.title('Feature importance \\Permutation', fontsize=85)
plt.xlabel(xlabel, fontsize=60)
plt.xticks(fontsize=45, rotation=rotation)
plt.ylabel(ylabel, fontsize=60)
plt.yticks(fontsize=45)
sns.despine(bottom=False, left=True)
plt.grid(False)
plt.tick_params(bottom=tick_bottom, left=tick_left)
return None
plot_feat(x='p_score', y='features',
xlabel='Importance', ylabel=None,
rotation=None, tick_bottom=True, tick_left=False)
plt.savefig(self.custom_name+'/img/feature_importance/'+str(self.custom_name)+'_'+out_features+'_Permute.png',bbox_inches='tight', dpi = 600, facecolor='white')
plt.show()
return
def plotShapImportance(self, model: any, out_features: str,
train_x: pd.DataFrame) -> None:
"""
A shap importance plots
Parameters:
----------
model: any
ML structure and weights
out_features: str
Name of the FHG coeficients
train_x: pd.DataFrame
splited predictor data for training
Example:
----------
>>> plotShapImportance(model, 'b', train_x)
"""
print("Plotting Shap feature importance {0} performance -------------- ".format(str(out_features)))
if self.best_model in ['xgb']:
# best_explainer = shap.Explainer(model, train_x) # Save f names
# pickle.dump(best_explainer, open(self.custom_name+"/model/"+str(self.custom_name)+'_'+out_features+"_Shap_Best.pickle.dat", "wb")) # Save f names
# best_shap_values = best_explainer(train_x) # Save f names
tree_explainer = shap.TreeExplainer(model)
pickle.dump(tree_explainer, open(self.custom_name+"/model/"+str(self.custom_name)+'_'+out_features+"_Shap_Tree.pickle.dat", "wb"))
tree_shap_values = tree_explainer.shap_values(train_x, check_additivity=False )
tree_expected_value = tree_explainer.expected_value
if isinstance(tree_expected_value, list):
tree_expected_value = tree_expected_value[1]
# agg_shap_values = np.abs(tree_shap_values).mean(0) # Save f names
# else: # Save f names
# agg_shap_values = np.abs(best_shap_values.values).mean(0) # Save f names
# feature_importance = pd.DataFrame(list(zip(train_x.columns, agg_shap_values)), columns=['feature_name','feature_importance']) # Save f names
# feature_importance.sort_values(by=['feature_importance'], ascending=False, inplace=True) # Save f names
#
# shap_data = {'columns': train_x.columns.to_list(),
# 'shap_names': best_shap_values.feature_names,
# 'shap_values': best_shap_values.data.mean(0)
# }
# shap_data = pd.DataFrame(shap_data)
best_explainer = shap.Explainer(model, train_x) # del Save f names
pickle.dump(best_explainer, open(self.custom_name+"/model/"+str(self.custom_name)+'_'+out_features+"_Shap_Best.pickle.dat", "wb")) # del Save f names
best_shap_values = best_explainer(train_x) # del Save f names
# best_expected_value = best_explainer.expected_value
# if isinstance(best_expected_value, list):
# best_expected_value = best_expected_value[1]
else:
best_explainer = shap.explainers.Permutation(model.predict, train_x)
pickle.dump(best_explainer, open(self.custom_name+"/model/"+str(self.custom_name)+'_'+out_features+"_Shap_Best.pickle.dat", "wb"))
best_shap_values = best_explainer(train_x) #test [:10]
# agg_shap_values = np.abs(best_shap_values.values).mean(0) # Save f names
# feature_importance = pd.DataFrame(list(zip(train_x.columns, agg_shap_values)), columns=['feature_name','feature_importance']) # Save f names
# feature_importance.sort_values(by=['feature_importance'], ascending=False, inplace=True) # Save f names
# shap_data = {'columns': train_x.columns.to_list(),
# 'shap_names': best_shap_values.feature_names,
# 'shap_values': best_shap_values.data.mean(0)
# }
# shap_data = pd.DataFrame(shap_data)
# best_expected_value = best_explainer.expected_value
# if isinstance(best_expected_value, list):
# best_expected_value = best_expected_value[1]
# feature_importance.to_parquet(self.custom_name+'/metrics/shap_'+str(self.custom_name)+'_'+out_features+'.parquet') # Save f names
# ___________________________________________________
# Heat maps
plt.clf()
plt.figure()
shap.plots.heatmap(best_shap_values, max_display=13, show=False)
plt.savefig(self.custom_name+'/img/feature_importance/'+str(self.custom_name)+'_'+out_features+'_Heatmap_'+str(self.best_model)+'.png',bbox_inches='tight', dpi = 600, facecolor='white')
plt.show()
plt.clf()
# ___________________________________________________
# plot the summery
if self.best_model in ['xgb']:
plt.figure()
shap.summary_plot(tree_shap_values, features=train_x, feature_names=train_x.columns, max_display=60, show=False)
my_plot = plt.gcf()
plt.savefig(self.custom_name+'/img/feature_importance/'+str(self.custom_name)+'_'+out_features+'_SHAP_Summary_'+str(self.best_model)+'.png',bbox_inches='tight', dpi = 600, facecolor='white')
plt.show()
plt.clf()
plt.figure()
shap.summary_plot(tree_shap_values, features=train_x, feature_names=train_x.columns, plot_type='bar', max_display=60, show=False)
my_plot = plt.gcf()
plt.savefig(self.custom_name+'/img/feature_importance/'+str(self.custom_name)+'_'+out_features+'_SHAP_Bar_Summary_'+str(self.best_model)+'.png',bbox_inches='tight', dpi = 600, facecolor='white')
plt.show()
plt.clf()
else:
plt.figure()
shap.summary_plot(best_shap_values, features=train_x, feature_names=train_x.columns, max_display=60, show=False) #test [:10]
my_plot = plt.gcf()
plt.savefig(self.custom_name+'/img/feature_importance/'+str(self.custom_name)+'_'+out_features+'_SHAP_Summary_'+str(self.best_model)+'.png',bbox_inches='tight', dpi = 600, facecolor='white')
plt.show()
plt.clf()
plt.figure()
shap.summary_plot(best_shap_values, features=train_x, feature_names=train_x.columns, plot_type='bar', max_display=50, show=False) #test [:10]
my_plot = plt.gcf()
plt.savefig(self.custom_name+'/img/feature_importance/'+str(self.custom_name)+'_'+out_features+'_SHAP_Bar_Summary_'+str(self.best_model)+'.png',bbox_inches='tight', dpi = 600, facecolor='white')
plt.show()
plt.clf()
# ___________________________________________________
# Barplot
plt.figure()
shap.plots.bar(best_shap_values.abs.mean(0), max_display=60, show=False)
#shap.plots.bar(np.abs(best_shap_values.values).mean(0), max_display=60, show=False) # Save f names
my_plot = plt.gcf()
plt.savefig(self.custom_name+'/img/feature_importance/'+str(self.custom_name)+'_'+out_features+'_Barplot_'+str(self.best_model)+'.png',bbox_inches='tight', dpi = 600, facecolor='white')
plt.show()
plt.clf()
# ___________________________________________________
# look at 5 important features
if self.best_model in ['xgb']:
imps = train_x.columns[np.argsort(np.abs(tree_shap_values).mean(0))]
imps = list(imps[-6:-1])
for feature in imps:
shap.plots.scatter(best_shap_values[:,feature], show=False)
my_plot = plt.gcf()
plt.savefig(self.custom_name+'/img/feature_importance/'+str(self.custom_name)+'_'+out_features+'_'+feature+'_Scatter_'+str(self.best_model)+'.png',bbox_inches='tight', dpi = 600, facecolor='white')
plt.show()
shap.dependence_plot(feature, tree_shap_values, train_x, show=False)
my_plot = plt.gcf()
plt.savefig(self.custom_name+'/img/feature_importance/'+str(self.custom_name)+'_'+out_features+'_'+feature+'_Scatter_Dependance_'+str(self.best_model)+'.png',bbox_inches='tight', dpi = 600, facecolor='white')
plt.show()
else:
imps = train_x.columns[np.argsort(np.abs(best_shap_values.values).mean(0))]
imps = list(imps[-6:-1])
for feature in imps:
shap.plots.scatter(best_shap_values[:,feature], show=False)
my_plot = plt.gcf()
plt.savefig(self.custom_name+'/img/feature_importance/'+str(self.custom_name)+'_'+out_features+'_'+feature+'_Scatter_'+str(self.best_model)+'.png',bbox_inches='tight', dpi = 600, facecolor='white')
plt.show()
shap.dependence_plot(feature, best_shap_values.values, train_x, show=False) # [:10]
my_plot = plt.gcf()
plt.savefig(self.custom_name+'/img/feature_importance/'+str(self.custom_name)+'_'+out_features+'_'+feature+'_Scatter_Dependance_'+str(self.best_model)+'.png',bbox_inches='tight', dpi = 600, facecolor='white')
plt.show()
# ___________________________________________________
# plot a single instance of observation
plt.clf()
obs = 8
plt.figure()
a = best_shap_values[obs]
shap.plots.waterfall(best_shap_values[obs], max_display=60, show=False)
my_plot = plt.gcf()
plt.savefig(self.custom_name+'/img/feature_importance/'+str(self.custom_name)+'_'+out_features+'_Obs_num_'+str(obs)+'_Waterfall_'+str(self.best_model)+'.png',bbox_inches='tight', dpi = 600, facecolor='white')
plt.show()
plt.clf()
# ___________________________________________________
# plot first 500 varaibles interactions
if self.best_model in ['xgb']:
plt.figure()
shap_interaction_values = tree_explainer.shap_interaction_values(train_x.iloc[:100,:])
shap.summary_plot(shap_interaction_values, train_x.iloc[:100,:], max_display = 12, show=False)
my_plot = plt.gcf()
plt.savefig(self.custom_name+'/img/feature_importance/'+str(self.custom_name)+'_'+out_features+'_Summary_Interactions_'+str(self.best_model)+'.png',bbox_inches='tight', dpi = 600, facecolor='white')
plt.show()
plt.clf()
# a custom interaction plot
# plt.figure()
# shap.dependence_plot(
# ("slope", "soil_texture_dummy"),
# shap_interaction_values, train_x.iloc[:100,:],
# display_features=train_x.iloc[:100,:]
# )
# my_plot = plt.gcf()
# plt.savefig('img/feature_importance/'+str(custom_name)+'_'+out_features+'_Custom_Interactions_'+str(self.best_model)+'.png',bbox_inches='tight', dpi = 600, facecolor='white')
# plt.show()
# plt.clf()
print("All plotting complete for {0} -------------- ".format(str(out_features)))