-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexplain.py
More file actions
47 lines (39 loc) · 1.99 KB
/
Copy pathexplain.py
File metadata and controls
47 lines (39 loc) · 1.99 KB
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
import numpy as np
from joblib import Parallel, delayed, parallel_backend
class Explain:
"""
Computes feature importance using various interpretability strategies.
"""
def __init__(self, importance_type='default'):
self.importance_type = importance_type
def get_importance(self, predictor, val_df, label_column='class', n_repeats=1):
if self.importance_type == 'default':
return self._default_importance(predictor)
elif self.importance_type == 'permutation':
return self._permutation_importance(predictor, val_df, label_column, n_repeats)
else:
raise ValueError(f"Unknown importance_type: {self.importance_type}")
def _default_importance(self, predictor):
print(f"[Explain] Default importance from model {predictor.model_best}")
best_model = predictor._trainer.load_model(predictor.model_best).model
feature_names = predictor.feature_metadata.get_features()
importances = best_model.feature_importances_
return dict(zip(feature_names, importances))
def _permutation_importance(self, predictor, val_df, label_column, n_repeats):
print("[Explain] Permutation importance")
np.random.seed(42) # For reproducibility
base_feature = predictor._learner.feature_generator.feature_metadata_in.get_features()
base_acc = predictor.evaluate(val_df, silent=False)['accuracy']
importance = {}
for feature in base_feature:
drops = []
for _ in range(n_repeats):
tmp = val_df.copy()
tmp[feature] = np.random.permutation(tmp[feature])
perm_acc = predictor.evaluate(tmp, silent=True)['accuracy']
drops.append(base_acc - perm_acc)
importance[feature] = np.mean(drops)
return importance
def _get_feature_impact(self, predictor, df, base_acc):
perm_acc = predictor.evaluate(df, silent=True)['accuracy']
return base_acc - perm_acc