-
Notifications
You must be signed in to change notification settings - Fork 0
/
fimp.py
130 lines (101 loc) · 4.39 KB
/
fimp.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
import sys
#sys.path.append("../arne/")
import numpy as np
from sklearn.ensemble import RandomForestClassifier
from sklearn.tree import DecisionTreeClassifier
import numpy as np
from treesmoothing import ShrinkageClassifier
from treesmoothing import importance
from tqdm import trange
import joblib
import pandas as pd
from shap import TreeExplainer
from sklearn.datasets import make_classification
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import train_test_split
from sklearn.metrics import balanced_accuracy_score
from sklearn.metrics import roc_auc_score
FI_no_hsc = list()
FI_hsc = list()
FI_beta = list()
for xx in range(0,30):
print(xx)
X, y = make_classification(n_samples=500,
n_features=100,
shuffle=False,
n_informative=20,
n_redundant=2,
class_sep=1)
#flip_y = 0.3)
#weights=[0.50])
ntrees = 100
sc = "roc_auc"
#sc = "balanced_accuracy"
# Compute importances for classical RF/DT
clf = RandomForestClassifier(n_estimators=ntrees).fit(X, y)
FI_no_hsc.append(clf.feature_importances_)
#np.savetxt("FI_no_hsc",FI_no_hsc, delimiter='\t')
# SHAP
#explainer = TreeExplainer(clf, X, check_additivity=False)
#shap_values = np.array(explainer.shap_values(X))
#shap_values = abs(shap_values[0,:,:])+abs(shap_values[1,:,:])
#importances = []
#for i in range(shap_values.shape[1]):
# importances.append(np.mean(np.abs(shap_values[:, i])))
#np.savetxt("FI_no_hsc_SHAP",importances, delimiter='\t')
###################################
# Hierarchical Shrinkage
###################################
lambdas = [0., 0.1, 1.0, 10.0, 25.0, 50.0, 100.0]
shrink_mode = "hs"
param_grid = {
"lmb": [0.0001, 0.001, 0.01, 0.1, 1, 10, 25, 50, 100, 200],
"shrink_mode": ["hs"]
}
grid_search = GridSearchCV(ShrinkageClassifier(RandomForestClassifier(n_estimators=ntrees)), param_grid, cv=5, n_jobs=-1, scoring=sc)
grid_search.fit(X, y)
best_params = grid_search.best_params_
print(best_params)
clf = ShrinkageClassifier(RandomForestClassifier(n_estimators=ntrees),shrink_mode=shrink_mode, lmb=best_params.get('lmb'))
#print(clf)
clf.fit(X, y)
FI_hsc.append(clf.estimator_.feature_importances_)
#np.savetxt("FI_hsc",FI_hsc, delimiter='\t')
# SHAP
#explainer = TreeExplainer(hsc.estimator_, X, check_additivity=False)
#shap_values = np.array(explainer.shap_values(X))
#shap_values = abs(shap_values[0,:,:])+abs(shap_values[1,:,:])
#importances = []
#for i in range(shap_values.shape[1]):
# importances.append(np.mean(np.abs(shap_values[:, i])))
#np.savetxt("FI_hsc_SHAP",importances, delimiter='\t')
#########################################
# BETA-based Hierarchical Shrinkage
#########################################
shrink_mode="beta"
#scores[shrink_mode] = []
param_grid = {
"alpha": [5000, 2000, 1000, 700, 500, 200, 100, 50, 10, 1],
"beta": [5000, 2000, 1000, 700, 500, 200, 100, 50, 10, 1],
"shrink_mode": ["beta"]}
grid_search = GridSearchCV(ShrinkageClassifier(RandomForestClassifier(n_estimators=ntrees)), param_grid, cv=5, n_jobs=-1, scoring=sc)
grid_search.fit(X, y)
best_params = grid_search.best_params_
print(best_params)
clf2 = ShrinkageClassifier(RandomForestClassifier(n_estimators=ntrees),shrink_mode=shrink_mode, alpha=best_params.get('alpha'), beta=best_params.get('beta'))
clf2.fit(X, y)
#FI_beta = clf2.estimator_.feature_importances_
FI_beta.append(clf2.estimator_.feature_importances_)
#FI_beta.append(importance(clf2))
#np.savetxt("FI_beta",FI_beta, delimiter='\t')
# SHAP
#explainer = TreeExplainer(ehsc.estimator_, X, check_additivity=False)
#shap_values = np.array(explainer.shap_values(X))
#shap_values = abs(shap_values[0,:,:])+abs(shap_values[1,:,:])
#importances = []
#for i in range(shap_values.shape[1]):
# importances.append(np.mean(np.abs(shap_values[:, i])))
#np.savetxt("FI_ehsc_SHAP",importances, delimiter='\t')
np.savetxt("FI_no_hsc",FI_no_hsc, delimiter='\t')
np.savetxt("FI_hsc",FI_hsc, delimiter='\t')
np.savetxt("FI_beta",FI_beta, delimiter='\t')