1
|
|
|
# Author: Simon Blanke |
2
|
|
|
# Email: [email protected] |
3
|
|
|
# License: MIT License |
4
|
|
|
|
5
|
|
|
from sklearn.datasets import load_iris |
6
|
|
|
from sklearn.model_selection import cross_val_score |
7
|
|
|
from sklearn.tree import DecisionTreeClassifier |
8
|
|
|
from hyperactive import Hyperactive |
9
|
|
|
|
10
|
|
|
data = load_iris() |
11
|
|
|
X = data.data |
12
|
|
|
y = data.target |
13
|
|
|
|
14
|
|
|
|
15
|
|
View Code Duplication |
def model(para, X_train, y_train): |
|
|
|
|
16
|
|
|
model = DecisionTreeClassifier( |
17
|
|
|
criterion=para["criterion"], |
18
|
|
|
max_depth=para["max_depth"], |
19
|
|
|
min_samples_split=para["min_samples_split"], |
20
|
|
|
min_samples_leaf=para["min_samples_leaf"], |
21
|
|
|
) |
22
|
|
|
scores = cross_val_score(model, X_train, y_train, cv=3) |
23
|
|
|
|
24
|
|
|
return scores.mean(), model |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
search_config = { |
28
|
|
|
model: { |
29
|
|
|
"criterion": ["gini", "entropy"], |
30
|
|
|
"max_depth": range(1, 21), |
31
|
|
|
"min_samples_split": range(2, 21), |
32
|
|
|
"min_samples_leaf": range(1, 21), |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
warm_start = { |
37
|
|
|
model: { |
38
|
|
|
"criterion": ["gini"], |
39
|
|
|
"max_depth": [2], |
40
|
|
|
"min_samples_split": [2], |
41
|
|
|
"min_samples_leaf": [2], |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
def test_n_jobs_2(): |
47
|
|
|
opt = Hyperactive(search_config, n_jobs=2) |
48
|
|
|
opt.fit(X, y) |
49
|
|
|
|
50
|
|
|
|
51
|
|
|
def test_n_jobs_4(): |
52
|
|
|
opt = Hyperactive(search_config, n_jobs=4) |
53
|
|
|
opt.fit(X, y) |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
def test_positional_args(): |
57
|
|
|
opt0 = Hyperactive(search_config, random_state=False) |
58
|
|
|
opt0.fit(X, y) |
59
|
|
|
|
60
|
|
|
opt1 = Hyperactive(search_config, random_state=1) |
61
|
|
|
opt1.fit(X, y) |
62
|
|
|
|
63
|
|
|
opt2 = Hyperactive(search_config, random_state=1) |
64
|
|
|
opt2.fit(X, y) |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
def test_random_state(): |
68
|
|
|
opt0 = Hyperactive(search_config, random_state=False) |
69
|
|
|
opt0.fit(X, y) |
70
|
|
|
|
71
|
|
|
opt1 = Hyperactive(search_config, random_state=0) |
72
|
|
|
opt1.fit(X, y) |
73
|
|
|
|
74
|
|
|
opt2 = Hyperactive(search_config, random_state=1) |
75
|
|
|
opt2.fit(X, y) |
76
|
|
|
|
77
|
|
|
|
78
|
|
|
def test_memory(): |
79
|
|
|
opt0 = Hyperactive(search_config, memory=True) |
80
|
|
|
opt0.fit(X, y) |
81
|
|
|
|
82
|
|
|
opt1 = Hyperactive(search_config, memory=False) |
83
|
|
|
opt1.fit(X, y) |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
def test_verbosity(): |
87
|
|
|
opt0 = Hyperactive(search_config, verbosity=0) |
88
|
|
|
opt0.fit(X, y) |
89
|
|
|
|
90
|
|
|
opt1 = Hyperactive(search_config, verbosity=1) |
91
|
|
|
opt1.fit(X, y) |
92
|
|
|
|
93
|
|
|
|
94
|
|
|
def test_scatter_init(): |
95
|
|
|
opt = Hyperactive(search_config, scatter_init=10) |
96
|
|
|
opt.fit(X, y) |
97
|
|
|
|
98
|
|
|
|
99
|
|
|
def test_scatter_init_and_warm_start(): |
100
|
|
|
opt = Hyperactive(search_config, warm_start=warm_start, scatter_init=10) |
101
|
|
|
opt.fit(X, y) |
102
|
|
|
|
103
|
|
|
opt = Hyperactive(search_config, warm_start=warm_start, scatter_init=10) |
104
|
|
|
opt.fit(X, y) |
105
|
|
|
|
106
|
|
|
|
107
|
|
|
def test_warm_start_multiple_jobs(): |
108
|
|
|
opt = Hyperactive(search_config, n_jobs=4, warm_start=warm_start) |
109
|
|
|
opt.fit(X, y) |
110
|
|
|
|
111
|
|
|
|
112
|
|
|
def test_warm_start(): |
113
|
|
|
opt = Hyperactive(search_config, n_jobs=1, warm_start=warm_start) |
114
|
|
|
opt.fit(X, y) |
115
|
|
|
|