-
Notifications
You must be signed in to change notification settings - Fork 0
/
bayesian_optimization.py
36 lines (27 loc) · 1.06 KB
/
bayesian_optimization.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
import warnings
warnings.filterwarnings('ignore')
import time
from numpy import mean
from rich.console import Console
from sklearn.datasets import make_blobs
from sklearn.model_selection import cross_val_score
from sklearn.neighbors import KNeighborsClassifier
from skopt import gp_minimize
from skopt.space import Integer
from skopt.utils import use_named_args
console = Console()
X, y = make_blobs(n_samples=500, centers=3, n_features=2)
model = KNeighborsClassifier()
search_space = [Integer(1, 5, name='n_neighbors'), Integer(1, 2, name='p')]
@use_named_args(search_space)
def evaluate_model(**params):
model.set_params(**params)
result = cross_val_score(model, X, y, cv=5, n_jobs=-1, scoring='accuracy')
estimate = mean(result)
return 1.0 - estimate
start_time = time.time()
with console.status("[bold green] Searching...") as status:
result = gp_minimize(evaluate_model, search_space)
print(f'Total time: {time.time() - start_time} sec')
print('Best Accuracy: %.3f' % (1.0 - result.fun))
print('Best Parameters: n_neighbors=%d, p=%d' % (result.x[0], result.x[1]))