1
|
|
|
# Author: Simon Blanke |
2
|
|
|
# Email: [email protected] |
3
|
|
|
# License: MIT License |
4
|
|
|
|
5
|
|
|
import numpy as np |
6
|
|
|
|
7
|
|
|
from sklearn.datasets import load_iris |
8
|
|
|
from sklearn.model_selection import cross_val_score |
9
|
|
|
from sklearn.tree import DecisionTreeClassifier |
10
|
|
|
from hyperactive import Hyperactive |
11
|
|
|
|
12
|
|
|
data = load_iris() |
13
|
|
|
X = data.data |
14
|
|
|
y = data.target |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
def model(para, X, y): |
18
|
|
|
model = DecisionTreeClassifier( |
19
|
|
|
max_depth=para["max_depth"], |
20
|
|
|
min_samples_split=para["min_samples_split"], |
21
|
|
|
min_samples_leaf=para["min_samples_leaf"], |
22
|
|
|
) |
23
|
|
|
scores = cross_val_score(model, X, y, cv=3) |
24
|
|
|
|
25
|
|
|
return scores.mean() |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
search_config = { |
29
|
|
|
model: { |
30
|
|
|
"max_depth": range(1, 21), |
31
|
|
|
"min_samples_split": range(2, 21), |
32
|
|
|
"min_samples_leaf": range(1, 21), |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
|
37
|
|
|
def test_func_return(): |
38
|
|
|
def model1(para, X, y): |
39
|
|
|
model = DecisionTreeClassifier( |
40
|
|
|
criterion=para["criterion"], |
41
|
|
|
max_depth=para["max_depth"], |
42
|
|
|
min_samples_split=para["min_samples_split"], |
43
|
|
|
min_samples_leaf=para["min_samples_leaf"], |
44
|
|
|
) |
45
|
|
|
scores = cross_val_score(model, X, y, cv=3) |
46
|
|
|
|
47
|
|
|
return scores.mean(), model |
48
|
|
|
|
49
|
|
|
search_config1 = { |
50
|
|
|
model1: { |
51
|
|
|
"criterion": ["gini", "entropy"], |
52
|
|
|
"max_depth": range(1, 21), |
53
|
|
|
"min_samples_split": range(2, 21), |
54
|
|
|
"min_samples_leaf": range(1, 21), |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
opt = Hyperactive(X, y) |
59
|
|
|
opt.search(search_config1) |
60
|
|
|
|
61
|
|
|
|
62
|
|
|
def test_n_jobs_2(): |
63
|
|
|
opt = Hyperactive(X, y) |
64
|
|
|
opt.search(search_config, n_jobs=2) |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
def test_n_jobs_4(): |
68
|
|
|
opt = Hyperactive(X, y) |
69
|
|
|
opt.search(search_config, n_jobs=4) |
70
|
|
|
|
71
|
|
|
|
72
|
|
|
def test_positional_args(): |
73
|
|
|
opt0 = Hyperactive(X, y, random_state=False) |
74
|
|
|
opt0.search(search_config) |
75
|
|
|
|
76
|
|
|
opt1 = Hyperactive(X, y, random_state=1) |
77
|
|
|
opt1.search(search_config) |
78
|
|
|
|
79
|
|
|
opt2 = Hyperactive(X, y, random_state=1) |
80
|
|
|
opt2.search(search_config) |
81
|
|
|
|
82
|
|
|
|
83
|
|
|
def test_random_state(): |
84
|
|
|
opt0 = Hyperactive(X, y, random_state=False) |
85
|
|
|
opt0.search(search_config) |
86
|
|
|
|
87
|
|
|
opt1 = Hyperactive(X, y, random_state=0) |
88
|
|
|
opt1.search(search_config) |
89
|
|
|
|
90
|
|
|
opt2 = Hyperactive(X, y, random_state=1) |
91
|
|
|
opt2.search(search_config) |
92
|
|
|
|
93
|
|
|
|
94
|
|
|
def test_max_time(): |
95
|
|
|
opt0 = Hyperactive(X, y) |
96
|
|
|
opt0.search(search_config, max_time=0.001) |
97
|
|
|
|
98
|
|
|
|
99
|
|
|
def test_memory(): |
100
|
|
|
opt0 = Hyperactive(X, y, memory=True) |
101
|
|
|
opt0.search(search_config) |
102
|
|
|
|
103
|
|
|
opt1 = Hyperactive(X, y, memory=False) |
104
|
|
|
opt1.search(search_config) |
105
|
|
|
|
106
|
|
|
|
107
|
|
|
def test_verbosity(): |
108
|
|
|
opt0 = Hyperactive(X, y, verbosity=0) |
109
|
|
|
opt0.search(search_config) |
110
|
|
|
|
111
|
|
|
opt0 = Hyperactive(X, y, verbosity=0) |
112
|
|
|
opt0.search(search_config, n_jobs=2) |
113
|
|
|
|
114
|
|
|
opt1 = Hyperactive(X, y, verbosity=1) |
115
|
|
|
opt1.search(search_config) |
116
|
|
|
|
117
|
|
|
opt0 = Hyperactive(X, y, verbosity=1) |
118
|
|
|
opt0.search(search_config) |
119
|
|
|
|
120
|
|
|
opt1 = Hyperactive(X, y, verbosity=2) |
121
|
|
|
opt1.search(search_config, n_jobs=2) |
122
|
|
|
|
123
|
|
|
|
124
|
|
|
def test_scatter_init(): |
125
|
|
|
init_config = {model: {"scatter_init": 10}} |
126
|
|
|
opt = Hyperactive(X, y) |
127
|
|
|
opt.search(search_config, init_config=init_config) |
128
|
|
|
|
129
|
|
|
|
130
|
|
|
def test_warm_start(): |
131
|
|
|
init_config = {model: {"n_estimators": 10, "max_depth": 2, "min_samples_split": 5}} |
132
|
|
|
opt = Hyperactive(X, y) |
133
|
|
|
opt.search(search_config, n_jobs=1, init_config=init_config) |
134
|
|
|
|
135
|
|
|
|
136
|
|
|
def test_optimizer_args(): |
137
|
|
|
opt = Hyperactive(X, y) |
138
|
|
|
opt.search(search_config, optimizer={"HillClimbing": {"epsilon": 0.1}}) |
139
|
|
|
|
140
|
|
|
|
141
|
|
|
def test_get_search_path(): |
142
|
|
|
opt = Hyperactive(X, y, verbosity=10) |
143
|
|
|
opt.search(search_config) |
144
|
|
|
|
145
|
|
|
opt = Hyperactive(X, y, verbosity=10) |
146
|
|
|
opt.search(search_config, optimizer="ParticleSwarm") |
147
|
|
|
|