forked from hyperactive-project/Hyperactive
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbayesian_optimization_example.py
More file actions
75 lines (64 loc) · 2.43 KB
/
Copy pathbayesian_optimization_example.py
File metadata and controls
75 lines (64 loc) · 2.43 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
"""
Bayesian Optimization Example - Gaussian Process-based Optimization
Bayesian optimization uses a probabilistic model (typically Gaussian Process) to
model the objective function and an acquisition function to decide where to sample
next. This approach is highly sample-efficient and particularly useful when
function evaluations are expensive.
Characteristics:
- Sample-efficient optimization
- Uses Gaussian Process to model objective function
- Balances exploration vs exploitation via acquisition functions
- Excellent for expensive function evaluations
- Handles uncertainty quantification naturally
"""
import numpy as np
from sklearn.datasets import load_wine
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import cross_val_score
from hyperactive.experiment.integrations import SklearnCvExperiment
from hyperactive.opt.gfo import BayesianOptimizer
# Load dataset
X, y = load_wine(return_X_y=True)
print(f"Dataset: Wine classification ({X.shape[0]} samples, {X.shape[1]} features)")
# Create experiment
estimator = RandomForestClassifier(random_state=42)
experiment = SklearnCvExperiment(estimator=estimator, X=X, y=y, cv=3)
# Define search space - discrete values for Bayesian optimization
search_space = {
"n_estimators": list(range(10, 201, 10)), # Discrete integer values (step 10)
"max_depth": list(range(1, 21)), # Discrete integer values
"min_samples_split": list(range(2, 21)), # Discrete integer values
"min_samples_leaf": list(range(1, 11)), # Discrete integer values
}
# Configure Bayesian Optimization
# Provide some initial good points to help the GP model initialization
warm_start_points = [
{
"n_estimators": 100,
"max_depth": 10,
"min_samples_split": 5,
"min_samples_leaf": 2,
},
{
"n_estimators": 50,
"max_depth": 15,
"min_samples_split": 3,
"min_samples_leaf": 1,
},
]
optimizer = BayesianOptimizer(
search_space=search_space,
n_iter=15,
random_state=42,
initialize={"warm_start": warm_start_points},
experiment=experiment,
)
# Run optimization
# Bayesian optimization builds a GP model of the objective function
# and uses acquisition functions (like Expected Improvement) to select
# the most promising points to evaluate next
best_params = optimizer.solve()
# Results
print("\n=== Results ===")
print(f"Best parameters: {best_params}")
print("Bayesian optimization completed successfully")