-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathtpe.py
38 lines (25 loc) · 872 Bytes
/
tpe.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
from sklearn.datasets import load_iris
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import cross_val_score
from hyperactive import Hyperactive
from hyperactive.optimizers import TreeStructuredParzenEstimators
data = load_iris()
X, y = data.data, data.target
def model(opt):
knr = KNeighborsClassifier(n_neighbors=opt["n_neighbors"])
scores = cross_val_score(knr, X, y, cv=5)
score = scores.mean()
return score
search_space = {
"n_neighbors": list(range(1, 100)),
}
hyper = Hyperactive()
hyper.add_search(model, search_space, n_iter=100)
hyper.run()
search_data = hyper.search_data(model)
optimizer = TreeStructuredParzenEstimators(
gamma_tpe=0.5, warm_start_smbo=search_data, rand_rest_p=0.05
)
hyper = Hyperactive()
hyper.add_search(model, search_space, optimizer=optimizer, n_iter=100)
hyper.run()