1
|
|
|
from sklearn.model_selection import cross_val_score |
2
|
|
|
from sklearn.ensemble import GradientBoostingRegressor, RandomForestRegressor |
3
|
|
|
from sklearn.datasets import load_boston |
4
|
|
|
|
5
|
|
|
from hyperactive import Hyperactive |
6
|
|
|
|
7
|
|
|
# import the ProgressBoard |
8
|
|
|
from hyperactive.dashboards import ProgressBoard |
9
|
|
|
|
10
|
|
|
data = load_boston() |
11
|
|
|
X, y = data.data, data.target |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
def model_gbr(opt): |
15
|
|
|
gbr = GradientBoostingRegressor( |
16
|
|
|
n_estimators=opt["n_estimators"], |
17
|
|
|
max_depth=opt["max_depth"], |
18
|
|
|
min_samples_split=opt["min_samples_split"], |
19
|
|
|
) |
20
|
|
|
scores = cross_val_score(gbr, X, y, cv=5) |
21
|
|
|
|
22
|
|
|
return scores.mean() |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
def model_rfr(opt): |
26
|
|
|
gbr = RandomForestRegressor( |
27
|
|
|
n_estimators=opt["n_estimators"], |
28
|
|
|
min_samples_split=opt["min_samples_split"], |
29
|
|
|
min_samples_leaf=opt["min_samples_leaf"], |
30
|
|
|
) |
31
|
|
|
scores = cross_val_score(gbr, X, y, cv=5) |
32
|
|
|
|
33
|
|
|
return scores.mean() |
34
|
|
|
|
35
|
|
|
|
36
|
|
|
search_space_gbr = { |
37
|
|
|
"n_estimators": list(range(30, 200, 5)), |
38
|
|
|
"max_depth": list(range(2, 12)), |
39
|
|
|
"min_samples_split": list(range(2, 22)), |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
search_space_rfr = { |
44
|
|
|
"n_estimators": list(range(10, 100, 1)), |
45
|
|
|
"min_samples_split": list(range(2, 22)), |
46
|
|
|
"min_samples_leaf": list(range(2, 22)), |
47
|
|
|
} |
48
|
|
|
# create an instance of the ProgressBoard |
49
|
|
|
progress_board1 = ProgressBoard() |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
""" |
53
|
|
|
Maybe you do not want to have the information of both searches on the same browser tab? |
54
|
|
|
If you want to open multiple progres board tabs at the same time you can just create |
55
|
|
|
as many instances of the ProgressBoard-class as you want and pass it two the corresponding |
56
|
|
|
searches. |
57
|
|
|
""" |
58
|
|
|
# progress_board2 = ProgressBoard() |
59
|
|
|
""" |
60
|
|
|
uncomment the line above and pass progress_board2 |
61
|
|
|
to one .add_search(...) to open two browser tabs at the same time |
62
|
|
|
""" |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
hyper = Hyperactive() |
66
|
|
|
hyper.add_search( |
67
|
|
|
model_gbr, |
68
|
|
|
search_space_gbr, |
69
|
|
|
n_iter=200, |
70
|
|
|
n_jobs=2, # the progress board works seamlessly with multiprocessing |
71
|
|
|
progress_board=progress_board1, # pass the instance of the ProgressBoard to .add_search(...) |
72
|
|
|
) |
73
|
|
|
# if you add more searches to Hyperactive they will appear in the same progress board |
74
|
|
|
hyper.add_search( |
75
|
|
|
model_rfr, |
76
|
|
|
search_space_rfr, |
77
|
|
|
n_iter=200, |
78
|
|
|
n_jobs=4, |
79
|
|
|
progress_board=progress_board1, |
80
|
|
|
) |
81
|
|
|
# a terminal will open, which opens a dashboard in your browser |
82
|
|
|
hyper.run() |
83
|
|
|
|