-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpower_sim.py
95 lines (75 loc) · 3.17 KB
/
power_sim.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
# power sim
import numpy as np
from sklearn.ensemble import RandomForestClassifier
from sklearn.tree import DecisionTreeClassifier
import numpy as np
from tqdm import trange
from sklearn.model_selection import GridSearchCV
from treesmoothing import ShrinkageClassifier
def simulate_data(n_samples: int, relevance: float):
X = np.zeros((n_samples, 5))
X[:, 0] = np.random.normal(0, 1, n_samples)
n_categories = [2, 4, 10, 20]
for i in range(1, 5):
X[:, i] = np.random.choice(
a=n_categories[i - 1],
size=n_samples,
p=np.ones(n_categories[i - 1]) / n_categories[i - 1],
)
y = np.zeros(n_samples)
y[X[:, 1] == 0] = np.random.binomial(1, 0.5 - relevance, np.sum(X[:, 1] == 0))
y[X[:, 1] == 1] = np.random.binomial(1, 0.5 + relevance, np.sum(X[:, 1] == 1))
return X, y
sc = "roc_auc"
#sc = "balanced_accuracy"
ntrees = 50
relevances = [0.0, 0.05, 0.1, 0.15, 0.2]
#relevances = [0.15]
for rel in relevances:
iterations = np.arange(0, 20, 1)
X, y = simulate_data(200, rel)
scores = {}
scores["vanilla"] = []
scores["hs"] = []
scores["beta"] = []
for xx in iterations:
# vanilla
print("Vanilla Mode")
shrink_mode="vanilla"
#scores[shrink_mode] = []
clf = RandomForestClassifier(n_estimators=ntrees) #DecisionTreeClassifier() #RandomForestClassifier(n_estimators=1) ## DecisionTreeClassifier() #
clf.fit(X, y)
scores[shrink_mode].append(clf.feature_importances_)
# hs
print("HS Mode")
shrink_mode="hs"
#scores[shrink_mode] = []
param_grid = {
"lmb": [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'))
clf.fit(X, y)
scores[shrink_mode].append(clf.estimator_.feature_importances_)
# beta
print("Beta Shrinkage")
shrink_mode="beta"
#scores[shrink_mode] = []
param_grid = {
"alpha": [8000, 5000, 4000, 2000, 1000, 800, 500, 100, 50, 30, 10, 1],
"beta": [8000, 5000, 4000, 2000, 1000, 800, 500, 100, 50, 30, 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)
clf = ShrinkageClassifier(RandomForestClassifier(n_estimators=ntrees),shrink_mode=shrink_mode, alpha=best_params.get('alpha'), beta=best_params.get('beta'))
clf.fit(X, y)
scores[shrink_mode].append(clf.estimator_.feature_importances_)
print(scores)
RES = np.vstack([scores['vanilla'],scores['hs'],scores['beta']])
print(RES)
np.savetxt(str(rel),RES, delimiter='\t')