-
Notifications
You must be signed in to change notification settings - Fork 2
/
feature_importance.py
351 lines (242 loc) · 14 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
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
import numpy as np
import pandas as pd
from sklearn.compose import ColumnTransformer
from sklearn.pipeline import Pipeline
from sklearn.utils.validation import check_is_fitted
import plotly.express as px
class FeatureImportance:
"""
Extract & Plot the Feature Names & Importance Values from a Scikit-Learn Pipeline.
The input is a Pipeline that starts with a ColumnTransformer & ends with a regression or classification model.
As intermediate steps, the Pipeline can have any number or no instances from sklearn.feature_selection.
Note:
If the ColumnTransformer contains Pipelines and if one of the transformers in the Pipeline is adding completely new columns,
it must come last in the pipeline. For example, OneHotEncoder, MissingIndicator & SimpleImputer(add_indicator=True) add columns
to the dataset that didn't exist before, so there should come last in the Pipeline.
Parameters
----------
pipeline : a Scikit-learn Pipeline class where the a ColumnTransformer is the first element and model estimator is the last element
verbose : a boolean. Whether to print all of the diagnostics. Default is False.
Attributes
__________
column_transformer_features : A list of the feature names created by the ColumnTransformer prior to any selectors being applied
transformer_list : A list of the transformer names that correspond with the `column_transformer_features` attribute
discarded_features : A list of the features names that were not selected by a sklearn.feature_selection instance.
discarding_selectors : A list of the selector names corresponding with the `discarded_features` attribute
feature_importance : A Pandas Series containing the feature importance values and feature names as the index.
plot_importances_df : A Pandas DataFrame containing the subset of features and values that are actually displaced in the plot.
feature_info_df : A Pandas DataFrame that aggregates the other attributes. The index is column_transformer_features. The transformer column contains the transformer_list.
value contains the feature_importance values. discarding_selector contains discarding_selectors & is_retained is a Boolean indicating whether the feature was retained.
"""
def __init__(self, pipeline, verbose=False):
self.pipeline = pipeline
self.verbose = verbose
def get_feature_names(self, verbose=None):
"""
Get the column names from the a ColumnTransformer containing transformers & pipelines
Parameters
----------
verbose : a boolean indicating whether to print summaries.
default = False
Returns
-------
a list of the correct feature names
Note:
If the ColumnTransformer contains Pipelines and if one of the transformers in the Pipeline is adding completely new columns,
it must come last in the pipeline. For example, OneHotEncoder, MissingIndicator & SimpleImputer(add_indicator=True) add columns
to the dataset that didn't exist before, so there should come last in the Pipeline.
Inspiration: https://github.com/scikit-learn/scikit-learn/issues/12525
"""
if verbose is None:
verbose = self.verbose
if verbose: print('''\n\n---------\nRunning get_feature_names\n---------\n''')
column_transformer = self.pipeline[0]
assert isinstance(column_transformer, ColumnTransformer), "Input isn't a ColumnTransformer"
check_is_fitted(column_transformer)
new_feature_names, transformer_list = [], []
for i, transformer_item in enumerate(column_transformer.transformers_):
transformer_name, transformer, orig_feature_names = transformer_item
orig_feature_names = list(orig_feature_names)
if verbose:
print('\n\n', i, '. Transformer/Pipeline: ', transformer_name, ',',
transformer.__class__.__name__, '\n')
print('\tn_orig_feature_names:', len(orig_feature_names))
if transformer == 'drop':
continue
if isinstance(transformer, Pipeline):
# if pipeline, get the last transformer in the Pipeline
transformer = transformer.steps[-1][1]
if hasattr(transformer, 'get_feature_names'):
if 'input_features' in transformer.get_feature_names.__code__.co_varnames:
names = list(transformer.get_feature_names(orig_feature_names))
else:
names = list(transformer.get_feature_names())
elif hasattr(transformer,'indicator_') and transformer.add_indicator:
# is this transformer one of the imputers & did it call the MissingIndicator?
missing_indicator_indices = transformer.indicator_.features_
missing_indicators = [orig_feature_names[idx] + '_missing_flag'\
for idx in missing_indicator_indices]
names = orig_feature_names + missing_indicators
elif hasattr(transformer,'features_'):
# is this a MissingIndicator class?
missing_indicator_indices = transformer.features_
missing_indicators = [orig_feature_names[idx] + '_missing_flag'\
for idx in missing_indicator_indices]
else:
names = orig_feature_names
if verbose:
print('\tn_new_features:', len(names))
print('\tnew_features:\n', names)
new_feature_names.extend(names)
transformer_list.extend([transformer_name] * len(names))
self.transformer_list, self.column_transformer_features = transformer_list,\
new_feature_names
return new_feature_names
def get_selected_features(self, verbose=None):
"""
Get the Feature Names that were retained after Feature Selection (sklearn.feature_selection)
Parameters
----------
verbose : a boolean indicating whether to print summaries. default = False
Returns
-------
a list of the selected feature names
"""
if verbose is None:
verbose = self.verbose
assert isinstance(self.pipeline, Pipeline), "Input isn't a Pipeline"
features = self.get_feature_names()
if verbose: print('\n\n---------\nRunning get_selected_features\n---------\n')
all_discarded_features, discarding_selectors = [], []
for i, step_item in enumerate(self.pipeline.steps[:]):
step_name, step = step_item
if hasattr(step, 'get_support'):
if verbose: print('\nStep ', i, ": ", step_name, ',',
step.__class__.__name__, '\n')
check_is_fitted(step)
feature_mask_dict = dict(zip(features, step.get_support()))
features = [feature for feature, is_retained in feature_mask_dict.items()\
if is_retained]
discarded_features = [feature for feature, is_retained in feature_mask_dict.items()\
if not is_retained]
all_discarded_features.extend(discarded_features)
discarding_selectors.extend([step_name] * len(discarded_features))
if verbose:
print(f'\t{len(features)} retained, {len(discarded_features)} discarded')
if len(discarded_features) > 0:
print('\n\tdiscarded_features:\n\n', discarded_features)
self.discarded_features, self.discarding_selectors = all_discarded_features,\
discarding_selectors
return features
def get_feature_importance(self):
"""
Creates a Pandas Series where values are the feature importance values from the model and feature names are set as the index.
This Series is stored in the `feature_importance` attribute.
Returns
-------
A pandas Series containing the feature importance values and feature names as the index.
"""
assert isinstance(self.pipeline, Pipeline), "Input isn't a Pipeline"
features = self.get_selected_features()
assert hasattr(self.pipeline[-1], 'feature_importances_'),\
"The last element in the pipeline isn't an estimator with a feature_importances_ attribute"
importance_values = self.pipeline[-1].feature_importances_
assert len(features) == len(importance_values),\
"The number of feature names & importance values doesn't match"
feature_importance = pd.Series(importance_values, index=features)
self.feature_importance = feature_importance
# create feature_info_df
column_transformer_df =\
pd.DataFrame(dict(transformer=self.transformer_list),
index=self.column_transformer_features)
discarded_features_df =\
pd.DataFrame(dict(discarding_selector=self.discarding_selectors),
index=self.discarded_features)
importance_df = self.feature_importance.rename('value').to_frame()
self.feature_info_df = \
column_transformer_df\
.join([importance_df, discarded_features_df])\
.assign(is_retained = lambda df: ~df.value.isna())
return feature_importance
def plot(self, top_n_features=100, rank_features=True, max_scale=True,
display_imp_values=True, display_imp_value_decimals=1,
height_per_feature=25, orientation='h', width=750, height=None,
str_pad_width=15, yaxes_tickfont_family='Courier New',
yaxes_tickfont_size=15):
"""
Plot the Feature Names & Importances
Parameters
----------
top_n_features : the number of features to plot, default is 100
rank_features : whether to rank the features with integers, default is True
max_scale : Should the importance values be scaled by the maximum value & mulitplied by 100? Default is True.
display_imp_values : Should the importance values be displayed? Default is True.
display_imp_value_decimals : If display_imp_values is True, how many decimal places should be displayed. Default is 1.
height_per_feature : if height is None, the plot height is calculated by top_n_features * height_per_feature.
This allows all the features enough space to be displayed
orientation : the plot orientation, 'h' (default) or 'v'
width : the width of the plot, default is 500
height : the height of the plot, the default is top_n_features * height_per_feature
str_pad_width : When rank_features=True, this number of spaces to add between the rank integer and feature name.
This will enable the rank integers to line up with each other for easier reading.
Default is 15. If you have long feature names, you can increase this number to make the integers line up more.
It can also be set to 0.
yaxes_tickfont_family : the font for the feature names. Default is Courier New.
yaxes_tickfont_size : the font size for the feature names. Default is 15.
Returns
-------
plot
"""
if height is None:
height = top_n_features * height_per_feature
# prep the data
all_importances = self.get_feature_importance()
n_all_importances = len(all_importances)
plot_importances_df =\
all_importances\
.nlargest(top_n_features)\
.sort_values()\
.to_frame('value')\
.rename_axis('feature')\
.reset_index()
if max_scale:
plot_importances_df['value'] = \
plot_importances_df.value.abs() /\
plot_importances_df.value.abs().max() * 100
self.plot_importances_df = plot_importances_df.copy()
if len(all_importances) < top_n_features:
title_text = 'All Feature Importances'
else:
title_text = f'Top {top_n_features} (of {n_all_importances}) Feature Importances'
if rank_features:
padded_features = \
plot_importances_df.feature\
.str.pad(width=str_pad_width)\
.values
ranked_features =\
plot_importances_df.index\
.to_series()\
.sort_values(ascending=False)\
.add(1)\
.astype(str)\
.str.cat(padded_features, sep='. ')\
.values
plot_importances_df['feature'] = ranked_features
if display_imp_values:
text = plot_importances_df.value.round(display_imp_value_decimals)
else:
text = None
# create the plot
fig = px.bar(plot_importances_df,
x='value',
y='feature',
orientation=orientation,
width=width,
height=height,
text=text)
fig.update_layout(title_text=title_text, title_x=0.5)
fig.update(layout_showlegend=False)
fig.update_yaxes(tickfont=dict(family=yaxes_tickfont_family,
size=yaxes_tickfont_size),
title='')
fig.show()