1
|
|
|
import time |
2
|
|
|
from sklearn.model_selection import cross_val_score |
3
|
|
|
from sklearn.tree import DecisionTreeRegressor |
4
|
|
|
from sklearn.datasets import load_boston |
5
|
|
|
from hyperactive import Hyperactive |
6
|
|
|
|
7
|
|
|
data = load_boston() |
8
|
|
|
X, y = data.data, data.target |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
def model(opt): |
12
|
|
|
gbr = DecisionTreeRegressor( |
13
|
|
|
max_depth=opt["max_depth"], |
14
|
|
|
min_samples_split=opt["min_samples_split"], |
15
|
|
|
) |
16
|
|
|
scores = cross_val_score(gbr, X, y, cv=10) |
17
|
|
|
|
18
|
|
|
return scores.mean() |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
search_space = { |
22
|
|
|
"max_depth": list(range(10, 35)), |
23
|
|
|
"min_samples_split": list(range(2, 22)), |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
c_time1 = time.time() |
27
|
|
|
hyper = Hyperactive() |
28
|
|
|
hyper.add_search(model, search_space, n_iter=100) |
29
|
|
|
hyper.run() |
30
|
|
|
d_time1 = time.time() - c_time1 |
31
|
|
|
print("Optimization time 1:", round(d_time1, 2)) |
32
|
|
|
|
33
|
|
|
# Hyperactive collects the search data |
34
|
|
|
search_data = hyper.results(model) |
35
|
|
|
|
36
|
|
|
# You can pass the search data to memory_warm_start to save time |
37
|
|
|
c_time2 = time.time() |
38
|
|
|
hyper = Hyperactive() |
39
|
|
|
hyper.add_search(model, search_space, n_iter=100, memory_warm_start=search_data) |
40
|
|
|
# The next run will be faster, because Hyperactive knows parts of the search space |
41
|
|
|
hyper.run() |
42
|
|
|
d_time2 = time.time() - c_time2 |
43
|
|
|
print("Optimization time 2:", round(d_time2, 2)) |
44
|
|
|
|