17
17
18
18
@app .cell
19
19
def _ ():
20
- import numpy as np
21
20
import matplotlib .pyplot as plt
21
+ import numpy as np
22
22
from sklearn .gaussian_process import GaussianProcessRegressor
23
- from sklearn .gaussian_process .kernels import Matern , WhiteKernel , ConstantKernel as C
23
+ from sklearn .gaussian_process .kernels import ConstantKernel as C
24
+ from sklearn .gaussian_process .kernels import Matern , WhiteKernel
24
25
25
26
def black_box_function (x ):
26
27
return - (np .sin (3 * x ) + 0.5 * x )
@@ -81,10 +82,10 @@ def _(X, X_sample, gpr, plt, y, y_sample):
81
82
82
83
# Plot the result
83
84
plt .figure (figsize = (10 , 5 ))
84
- plt .plot (X , y , ' k--' , label = "True function" )
85
- plt .plot (X , mu , 'b-' , label = "GPR mean" )
85
+ plt .plot (X , y , " k--" , label = "True function" )
86
+ plt .plot (X , mu , "b-" , label = "GPR mean" )
86
87
plt .fill_between (X .ravel (), mu - std , mu + std , alpha = 0.3 , label = "Uncertainty" )
87
- plt .scatter (X_sample , y_sample , c = ' red' , label = "Samples" )
88
+ plt .scatter (X_sample , y_sample , c = " red" , label = "Samples" )
88
89
plt .legend ()
89
90
plt .title ("Gaussian Process Fit" )
90
91
plt .xlabel ("x" )
@@ -101,7 +102,7 @@ def expected_improvement(X, X_sample, y_sample, model, xi=0.01):
101
102
mu , std = model .predict (X , return_std = True )
102
103
mu_sample_opt = np .min (y_sample )
103
104
104
- with np .errstate (divide = ' warn' ):
105
+ with np .errstate (divide = " warn" ):
105
106
imp = mu_sample_opt - mu - xi # because we are minimizing
106
107
Z = imp / std
107
108
ei = imp * norm .cdf (Z ) + std * norm .pdf (Z )
@@ -118,7 +119,7 @@ def _(X, X_sample, expected_improvement, gpr, np, plt, y_sample):
118
119
119
120
plt .figure (figsize = (10 , 4 ))
120
121
plt .plot (X , ei , label = "Expected Improvement" )
121
- plt .axvline (X [np .argmax (ei )], color = 'r' , linestyle = '--' , label = "Next sample point" )
122
+ plt .axvline (X [np .argmax (ei )], color = "r" , linestyle = "--" , label = "Next sample point" )
122
123
plt .title ("Acquisition Function (Expected Improvement)" )
123
124
plt .xlabel ("x" )
124
125
plt .ylabel ("EI(x)" )
@@ -135,7 +136,7 @@ def bayesian_optimization(n_iter=10):
135
136
X_sample = np .array ([[1.0 ], [2.5 ], [4.0 ]])
136
137
y_sample = black_box_function (X_sample )
137
138
138
- for i in range (n_iter ):
139
+ for _ in range (n_iter ):
139
140
gpr .fit (X_sample , y_sample )
140
141
ei = expected_improvement (X , X_sample , y_sample , gpr )
141
142
x_next = X [np .argmax (ei )].reshape (- 1 , 1 )
@@ -161,8 +162,8 @@ def _(bayesian_optimization):
161
162
@app .cell
162
163
def _ (X , X_opt , black_box_function , plt , y_opt ):
163
164
# Plot final sampled points
164
- plt .plot (X , black_box_function (X ), ' k--' , label = "True function" )
165
- plt .scatter (X_opt , y_opt , c = ' red' , label = "Sampled Points" )
165
+ plt .plot (X , black_box_function (X ), " k--" , label = "True function" )
166
+ plt .scatter (X_opt , y_opt , c = " red" , label = "Sampled Points" )
166
167
plt .title ("Bayesian Optimization with Gaussian Process" )
167
168
plt .xlabel ("x" )
168
169
plt .ylabel ("f(x)" )
0 commit comments