🙆
Gpyopt,Plotly出力
import numpy as np
import GPyOpt
import plotly.graph_objects as go
# 実験データ
x_data = np.array([-2, -1, 0, 1, 2])
y_data = np.array([4, 1, 0, 1, 4])
# ガウス過程モデルの設定
kernel = GPyOpt.kernels.Matern52(input_dim=1, variance=1.0, lengthscale=1.0)
model = GPyOpt.models.GPModel(kernel=kernel, optimize_restarts=5, verbose=False)
# ベイズ最適化の設定
optimizer = GPyOpt.methods.BayesianOptimization(f=None, domain=[(-5, 5)], model=model)
# ベイズ最適化の実行
optimizer.run_optimization(max_iter=10)
# 予測関数のプロット
x_pred = np.linspace(-5, 5, 100).reshape(-1, 1)
y_pred, y_std = optimizer.model.predict(x_pred)
fig = go.Figure()
fig.add_trace(go.Scatter(x=x_pred.flatten(), y=y_pred.flatten(), mode='lines', name='Predicted Function'))
fig.update_layout(title='Predicted Function')
fig.show()
# 信頼区間のプロット
fig = go.Figure()
fig.add_trace(go.Scatter(x=x_pred.flatten(), y=y_pred.flatten(), mode='lines', name='Predicted Function'))
fig.add_trace(go.Scatter(x=x_pred.flatten(), y=(y_pred + 2 * y_std).flatten(), mode='lines', name='Upper Confidence Bound'))
fig.add_trace(go.Scatter(x=x_pred.flatten(), y=(y_pred - 2 * y_std).flatten(), mode='lines', name='Lower Confidence Bound'))
fig.update_layout(title='Confidence Intervals')
fig.show()
# 獲得関数のプロット
x_acq = np.linspace(-5, 5, 100).reshape(-1, 1)
y_acq = optimizer.acquisition.acquisition_function(x_acq)
fig = go.Figure()
fig.add_trace(go.Scatter(x=x_acq.flatten(), y=y_acq.flatten(), mode='lines', name='Acquisition Function'))
fig.update_layout(title='Acquisition Function')
fig.show()
Discussion