import numpy as np
from math import inf
from spotPython.fun.objectivefunctions import analytical
from spotPython.spot import spot
import matplotlib.pyplot as plt
7 Expected Improvement
7.1 Example: Spot
and the 1-dim Sphere Function
7.1.1 The Objective Function: 1-dim Sphere
- The
spotPython
package provides several classes of objective functions. - We will use an analytical objective function, i.e., a function that can be described by a (closed) formula: \[f(x) = x^2 \]
= analytical().fun_sphere fun
= analytical().fun_sphere fun
- The size of the
lower
bound vector determines the problem dimension. - Here we will use
np.array([-1])
, i.e., a one-dim function.
Similar to the one-dimensional case, which was introduced in Section Section 1.7, we can use TensorBoard to monitor the progress of the optimization. We will use the same code, only the prefix is different:
from spotPython.utils.file import get_experiment_name
from spotPython.utils.init import fun_control_init
from spotPython.utils.file import get_spot_tensorboard_path
= "07_Y"
PREFIX = get_experiment_name(prefix=PREFIX)
experiment_name print(experiment_name)
= fun_control_init(
fun_control =get_spot_tensorboard_path(experiment_name),
spot_tensorboard_path=0,
sigma=123,) seed
07_Y_bartz08-2_2023-07-10_00-21-27
= spot.Spot(fun=fun,
spot_1 = 25,
fun_evals = np.array([-1]),
lower = np.array([1]),
upper ={"init_size": 10},
design_control= np.sqrt(np.spacing(1)),
tolerance_x = fun_control,)
fun_control
spot_1.run()
<spotPython.spot.spot.Spot at 0x103f7ca30>
7.1.2 Results
spot_1.print_results()
min y: 2.135607331180881e-12
x0: -1.4613717292943917e-06
[['x0', -1.4613717292943917e-06]]
=True) spot_1.plot_progress(log_y
7.2 Same, but with EI as infill_criterion
= "07_EI_ISO"
PREFIX = get_experiment_name(prefix=PREFIX)
experiment_name print(experiment_name)
= fun_control_init(
fun_control =get_spot_tensorboard_path(experiment_name),
spot_tensorboard_path=0,
sigma=123,) seed
07_EI_ISO_bartz08-2_2023-07-10_00-21-29
= spot.Spot(fun=fun,
spot_1_ei = np.array([-1]),
lower = np.array([1]),
upper = 25,
fun_evals = np.sqrt(np.spacing(1)),
tolerance_x = "ei",
infill_criterion ={"init_size": 10},
design_control= fun_control,)
fun_control spot_1_ei.run()
<spotPython.spot.spot.Spot at 0x2bb95dea0>
=True) spot_1_ei.plot_progress(log_y
spot_1_ei.print_results()
min y: 2.1963022660037201e-10
x0: 1.4819926673245452e-05
[['x0', 1.4819926673245452e-05]]
7.3 Non-isotropic Kriging
= "07_EI_NONISO"
PREFIX = get_experiment_name(prefix=PREFIX)
experiment_name print(experiment_name)
= fun_control_init(
fun_control =get_spot_tensorboard_path(experiment_name),
spot_tensorboard_path=0,
sigma=123,) seed
07_EI_NONISO_bartz08-2_2023-07-10_00-21-31
= spot.Spot(fun=fun,
spot_2_ei_noniso = np.array([-1, -1]),
lower = np.array([1, 1]),
upper = 25,
fun_evals = np.sqrt(np.spacing(1)),
tolerance_x = "ei",
infill_criterion =True,
show_models={"init_size": 10},
design_control={"noise": False,
surrogate_control"cod_type": "norm",
"min_theta": -4,
"max_theta": 3,
"n_theta": 2,
"model_fun_evals": 1000,
},=fun_control,)
fun_control spot_2_ei_noniso.run()
<spotPython.spot.spot.Spot at 0x2bb65e9b0>
=True) spot_2_ei_noniso.plot_progress(log_y
spot_2_ei_noniso.print_results()
min y: 1.088759927339735e-07
x0: -0.0002833471276146305
x1: 0.00016908695398081962
[['x0', -0.0002833471276146305], ['x1', 0.00016908695398081962]]
spot_2_ei_noniso.surrogate.plot()
7.4 Using sklearn
Surrogates
7.4.1 The spot Loop
The spot
loop consists of the following steps:
- Init: Build initial design \(X\)
- Evaluate initial design on real objective \(f\): \(y = f(X)\)
- Build surrogate: \(S = S(X,y)\)
- Optimize on surrogate: \(X_0 = \text{optimize}(S)\)
- Evaluate on real objective: \(y_0 = f(X_0)\)
- Impute (Infill) new points: \(X = X \cup X_0\), \(y = y \cup y_0\).
- Got 3.
The spot
loop is implemented in R
as follows:
7.4.2 spot: The Initial Model
7.4.2.1 Example: Modifying the initial design size
This is the “Example: Modifying the initial design size” from Chapter 4.5.1 in [bart21i].
= spot.Spot(fun=fun,
spot_ei = np.array([-1,-1]),
lower = np.array([1,1]),
upper={"init_size": 5})
design_control spot_ei.run()
<spotPython.spot.spot.Spot at 0x2bc25e050>
spot_ei.plot_progress()
min(spot_1.y), np.min(spot_ei.y) np.
(2.135607331180881e-12, 1.881581967484049e-05)
7.4.3 Init: Build Initial Design
from spotPython.design.spacefilling import spacefilling
from spotPython.build.kriging import Kriging
from spotPython.fun.objectivefunctions import analytical
= spacefilling(2)
gen = np.random.RandomState(1)
rng = np.array([-5,-0])
lower = np.array([10,15])
upper = analytical().fun_branin
fun
= gen.scipy_lhd(10, lower=lower, upper = upper)
X print(X)
= fun(X, fun_control=fun_control)
y print(y)
[[ 8.97647221 13.41926847]
[ 0.66946019 1.22344228]
[ 5.23614115 13.78185824]
[ 5.6149825 11.5851384 ]
[-1.72963184 1.66516096]
[-4.26945568 7.1325531 ]
[ 1.26363761 10.17935555]
[ 2.88779942 8.05508969]
[-3.39111089 4.15213772]
[ 7.30131231 5.22275244]]
[128.95676449 31.73474356 172.89678121 126.71295908 64.34349975
70.16178611 48.71407916 31.77322887 76.91788181 30.69410529]
= Kriging(name='kriging', seed=123)
S
S.fit(X, y) S.plot()
= spacefilling(2, seed=123)
gen = gen.scipy_lhd(3)
X0 = spacefilling(2, seed=345)
gen = gen.scipy_lhd(3)
X1 = gen.scipy_lhd(3)
X2 = spacefilling(2, seed=123)
gen = gen.scipy_lhd(3)
X3 X0, X1, X2, X3
(array([[0.77254938, 0.31539299],
[0.59321338, 0.93854273],
[0.27469803, 0.3959685 ]]),
array([[0.78373509, 0.86811887],
[0.06692621, 0.6058029 ],
[0.41374778, 0.00525456]]),
array([[0.121357 , 0.69043832],
[0.41906219, 0.32838498],
[0.86742658, 0.52910374]]),
array([[0.77254938, 0.31539299],
[0.59321338, 0.93854273],
[0.27469803, 0.3959685 ]]))
7.4.4 Evaluate
7.4.5 Build Surrogate
7.4.6 A Simple Predictor
The code below shows how to use a simple model for prediction.
Assume that only two (very costly) measurements are available:
- f(0) = 0.5
- f(2) = 2.5
We are interested in the value at \(x_0 = 1\), i.e., \(f(x_0 = 1)\), but cannot run an additional, third experiment.
from sklearn import linear_model
= np.array([[0], [2]])
X = np.array([0.5, 2.5])
y = linear_model.LinearRegression()
S_lm = S_lm.fit(X, y)
S_lm = np.array([[1]])
X0 = S_lm.predict(X0)
y0 print(y0)
[1.5]
- Central Idea:
- Evaluation of the surrogate model
S_lm
is much cheaper (or / and much faster) than running the real-world experiment \(f\).
- Evaluation of the surrogate model
7.5 Gaussian Processes regression: basic introductory example
This example was taken from scikit-learn. After fitting our model, we see that the hyperparameters of the kernel have been optimized. Now, we will use our kernel to compute the mean prediction of the full dataset and plot the 95% confidence interval.
import numpy as np
import matplotlib.pyplot as plt
import math as m
from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.gaussian_process.kernels import RBF
= np.linspace(start=0, stop=10, num=1_000).reshape(-1, 1)
X = np.squeeze(X * np.sin(X))
y = np.random.RandomState(1)
rng = rng.choice(np.arange(y.size), size=6, replace=False)
training_indices = X[training_indices], y[training_indices]
X_train, y_train
= 1 * RBF(length_scale=1.0, length_scale_bounds=(1e-2, 1e2))
kernel = GaussianProcessRegressor(kernel=kernel, n_restarts_optimizer=9)
gaussian_process
gaussian_process.fit(X_train, y_train)
gaussian_process.kernel_
= gaussian_process.predict(X, return_std=True)
mean_prediction, std_prediction
=r"$f(x) = x \sin(x)$", linestyle="dotted")
plt.plot(X, y, label="Observations")
plt.scatter(X_train, y_train, label="Mean prediction")
plt.plot(X, mean_prediction, label
plt.fill_between(
X.ravel(),- 1.96 * std_prediction,
mean_prediction + 1.96 * std_prediction,
mean_prediction =0.5,
alpha=r"95% confidence interval",
label
)
plt.legend()"$x$")
plt.xlabel("$f(x)$")
plt.ylabel(= plt.title("sk-learn Version: Gaussian process regression on noise-free dataset") _
from spotPython.build.kriging import Kriging
import numpy as np
import matplotlib.pyplot as plt
= np.random.RandomState(1)
rng = np.linspace(start=0, stop=10, num=1_000).reshape(-1, 1)
X = np.squeeze(X * np.sin(X))
y = rng.choice(np.arange(y.size), size=6, replace=False)
training_indices = X[training_indices], y[training_indices]
X_train, y_train
= Kriging(name='kriging', seed=123, log_level=50, cod_type="norm")
S
S.fit(X_train, y_train)
= S.predict(X, return_val="all")
mean_prediction, std_prediction, ei
std_prediction
=r"$f(x) = x \sin(x)$", linestyle="dotted")
plt.plot(X, y, label="Observations")
plt.scatter(X_train, y_train, label="Mean prediction")
plt.plot(X, mean_prediction, label
plt.fill_between(
X.ravel(),- 1.96 * std_prediction,
mean_prediction + 1.96 * std_prediction,
mean_prediction =0.5,
alpha=r"95% confidence interval",
label
)
plt.legend()"$x$")
plt.xlabel("$f(x)$")
plt.ylabel(= plt.title("spotPython Version: Gaussian process regression on noise-free dataset") _
7.6 The Surrogate: Using scikit-learn models
Default is the internal kriging
surrogate.
= Kriging(name='kriging', seed=123) S_0
Models from scikit-learn
can be selected, e.g., Gaussian Process:
# Needed for the sklearn surrogates:
from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.gaussian_process.kernels import RBF
from sklearn.tree import DecisionTreeRegressor
from sklearn.ensemble import RandomForestRegressor
from sklearn import linear_model
from sklearn import tree
import pandas as pd
= 1 * RBF(length_scale=1.0, length_scale_bounds=(1e-2, 1e2))
kernel = GaussianProcessRegressor(kernel=kernel, n_restarts_optimizer=9) S_GP
- and many more:
= DecisionTreeRegressor(random_state=0)
S_Tree = linear_model.LinearRegression()
S_LM = linear_model.Ridge()
S_Ridge = RandomForestRegressor(max_depth=2, random_state=0) S_RF
- The scikit-learn GP model
S_GP
is selected.
= S_GP S
isinstance(S, GaussianProcessRegressor)
True
from spotPython.fun.objectivefunctions import analytical
= analytical().fun_branin
fun = np.array([-5,-0])
lower = np.array([10,15])
upper ={"init_size": 5}
design_control={
surrogate_control"infill_criterion": None,
"n_points": 1,
}= spot.Spot(fun=fun, lower = lower, upper= upper, surrogate=S,
spot_GP = 15, noise = False, log_level = 50,
fun_evals =design_control,
design_control=surrogate_control)
surrogate_control
spot_GP.run()
<spotPython.spot.spot.Spot at 0x2c317a170>
spot_GP.y
array([ 69.32459936, 152.38491454, 107.92560483, 24.51465459,
76.73500031, 86.304256 , 11.00307816, 10.96066519,
16.06668258, 24.08432082, 4.08948415, 1.42303775,
1.47359526, 16.04703294, 0.6989341 ])
spot_GP.plot_progress()
spot_GP.print_results()
min y: 0.6989341031319167
x0: 3.358292789592623
x1: 2.3886120108545597
[['x0', 3.358292789592623], ['x1', 2.3886120108545597]]
7.7 Additional Examples
# Needed for the sklearn surrogates:
from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.gaussian_process.kernels import RBF
from sklearn.tree import DecisionTreeRegressor
from sklearn.ensemble import RandomForestRegressor
from sklearn import linear_model
from sklearn import tree
import pandas as pd
= 1 * RBF(length_scale=1.0, length_scale_bounds=(1e-2, 1e2))
kernel = GaussianProcessRegressor(kernel=kernel, n_restarts_optimizer=9) S_GP
from spotPython.build.kriging import Kriging
import numpy as np
import spotPython
from spotPython.fun.objectivefunctions import analytical
from spotPython.spot import spot
= Kriging(name='kriging',
S_K =123,
seed=50,
log_level= "y",
infill_criterion =1,
n_theta=False,
noise="norm")
cod_type= analytical().fun_sphere
fun = np.array([-1,-1])
lower = np.array([1,1])
upper
={"init_size": 10}
design_control={
surrogate_control"n_points": 1,
}= spot.Spot(fun=fun,
spot_S_K = lower,
lower = upper,
upper=S_K,
surrogate= 25,
fun_evals = False,
noise = 50,
log_level =design_control,
design_control=surrogate_control)
surrogate_control
spot_S_K.run()
<spotPython.spot.spot.Spot at 0x2c34ba6e0>
=True) spot_S_K.plot_progress(log_y
spot_S_K.surrogate.plot()
spot_S_K.print_results()
min y: 1.809224307539433e-06
x0: -0.001330101474082372
x1: 0.0002001358942901893
[['x0', -0.001330101474082372], ['x1', 0.0002001358942901893]]
7.7.1 Optimize on Surrogate
7.7.2 Evaluate on Real Objective
7.7.3 Impute / Infill new Points
7.8 Tests
import numpy as np
from spotPython.spot import spot
from spotPython.fun.objectivefunctions import analytical
= analytical().fun_sphere
fun_sphere = spot.Spot(
spot_1 =fun_sphere,
fun=np.array([-1, -1]),
lower=np.array([1, 1]),
upper= 2
n_points
)
# (S-2) Initial Design:
= spot_1.design.scipy_lhd(
spot_1.X "init_size"], lower=spot_1.lower, upper=spot_1.upper
spot_1.design_control[
)print(spot_1.X)
# (S-3): Eval initial design:
= spot_1.fun(spot_1.X)
spot_1.y print(spot_1.y)
spot_1.surrogate.fit(spot_1.X, spot_1.y)= spot_1.suggest_new_X()
X0 print(X0)
assert X0.size == spot_1.n_points * spot_1.k
[[ 0.86352963 0.7892358 ]
[-0.24407197 -0.83687436]
[ 0.36481882 0.8375811 ]
[ 0.415331 0.54468512]
[-0.56395091 -0.77797854]
[-0.90259409 -0.04899292]
[-0.16484832 0.35724741]
[ 0.05170659 0.07401196]
[-0.78548145 -0.44638164]
[ 0.64017497 -0.30363301]]
[1.36857656 0.75992983 0.83463487 0.46918172 0.92329124 0.8170764
0.15480068 0.00815134 0.81623768 0.502017 ]
[[0.00151305 0.00405727]
[0.00151305 0.00405727]]
7.9 EI: The Famous Schonlau Example
= np.array([1, 2, 3, 4, 12]).reshape(-1,1)
X_train0 = np.linspace(start=0, stop=10, num=5).reshape(-1, 1) X_train
from spotPython.build.kriging import Kriging
import numpy as np
import matplotlib.pyplot as plt
= np.array([1., 2., 3., 4., 12.]).reshape(-1,1)
X_train = np.array([0., -1.75, -2, -0.5, 5.])
y_train
= Kriging(name='kriging', seed=123, log_level=50, n_theta=1, noise=False, cod_type="norm")
S
S.fit(X_train, y_train)
= np.linspace(start=0, stop=13, num=1000).reshape(-1, 1)
X = S.predict(X, return_val="all")
mean_prediction, std_prediction, ei
="Observations")
plt.scatter(X_train, y_train, label="Mean prediction")
plt.plot(X, mean_prediction, labelif True:
plt.fill_between(
X.ravel(),- 2 * std_prediction,
mean_prediction + 2 * std_prediction,
mean_prediction =0.5,
alpha=r"95% confidence interval",
label
)
plt.legend()"$x$")
plt.xlabel("$f(x)$")
plt.ylabel(= plt.title("Gaussian process regression on noise-free dataset") _
#plt.plot(X, y, label=r"$f(x) = x \sin(x)$", linestyle="dotted")
# plt.scatter(X_train, y_train, label="Observations")
-ei, label="Expected Improvement")
plt.plot(X,
plt.legend()"$x$")
plt.xlabel("$f(x)$")
plt.ylabel(= plt.title("Gaussian process regression on noise-free dataset") _
S.log
{'negLnLike': array([1.20788205]),
'theta': array([1.09275997]),
'p': [],
'Lambda': []}
7.10 EI: The Forrester Example
from spotPython.build.kriging import Kriging
import numpy as np
import matplotlib.pyplot as plt
import spotPython
from spotPython.fun.objectivefunctions import analytical
from spotPython.spot import spot
# exact x locations are unknown:
= np.array([0.0, 0.175, 0.225, 0.3, 0.35, 0.375, 0.5,1]).reshape(-1,1)
X_train
= analytical().fun_forrester
fun = fun_control_init(
fun_control =get_spot_tensorboard_path(experiment_name),
spot_tensorboard_path=1.0,
sigma=123,)
seed= fun(X_train, fun_control=fun_control)
y_train
= Kriging(name='kriging', seed=123, log_level=50, n_theta=1, noise=False, cod_type="norm")
S
S.fit(X_train, y_train)
= np.linspace(start=0, stop=1, num=1000).reshape(-1, 1)
X = S.predict(X, return_val="all")
mean_prediction, std_prediction, ei
="Observations")
plt.scatter(X_train, y_train, label="Mean prediction")
plt.plot(X, mean_prediction, labelif True:
plt.fill_between(
X.ravel(),- 2 * std_prediction,
mean_prediction + 2 * std_prediction,
mean_prediction =0.5,
alpha=r"95% confidence interval",
label
)
plt.legend()"$x$")
plt.xlabel("$f(x)$")
plt.ylabel(= plt.title("Gaussian process regression on noise-free dataset") _
#plt.plot(X, y, label=r"$f(x) = x \sin(x)$", linestyle="dotted")
# plt.scatter(X_train, y_train, label="Observations")
-ei, label="Expected Improvement")
plt.plot(X,
plt.legend()"$x$")
plt.xlabel("$f(x)$")
plt.ylabel(= plt.title("Gaussian process regression on noise-free dataset") _
7.11 Noise
import numpy as np
import spotPython
from spotPython.fun.objectivefunctions import analytical
from spotPython.spot import spot
from spotPython.design.spacefilling import spacefilling
from spotPython.build.kriging import Kriging
import matplotlib.pyplot as plt
= spacefilling(1)
gen = np.random.RandomState(1)
rng = np.array([-10])
lower = np.array([10])
upper = analytical().fun_sphere
fun = fun_control_init(
fun_control =get_spot_tensorboard_path(experiment_name),
spot_tensorboard_path=2.0,
sigma=123,)
seed= gen.scipy_lhd(10, lower=lower, upper = upper)
X print(X)
= fun(X, fun_control=fun_control)
y print(y)
y.shape= X.reshape(-1,1)
X_train = y
y_train
= Kriging(name='kriging',
S =123,
seed=50,
log_level=1,
n_theta=False)
noise
S.fit(X_train, y_train)
= np.linspace(start=-13, stop=13, num=1000).reshape(-1, 1)
X_axis = S.predict(X_axis, return_val="all")
mean_prediction, std_prediction, ei
#plt.plot(X, y, label=r"$f(x) = x \sin(x)$", linestyle="dotted")
="Observations")
plt.scatter(X_train, y_train, label#plt.plot(X, ei, label="Expected Improvement")
="mue")
plt.plot(X_axis, mean_prediction, label
plt.legend()"$x$")
plt.xlabel("$f(x)$")
plt.ylabel(= plt.title("Sphere: Gaussian process regression on noisy dataset") _
[[ 0.63529627]
[-4.10764204]
[-0.44071975]
[ 9.63125638]
[-8.3518118 ]
[-3.62418901]
[ 4.15331 ]
[ 3.4468512 ]
[ 6.36049088]
[-7.77978539]]
[-1.57464135 16.13714981 2.77008442 93.14904827 71.59322218 14.28895359
15.9770567 12.96468767 39.82265329 59.88028242]
S.log
{'negLnLike': array([25.26601608]),
'theta': array([-1.98024606]),
'p': [],
'Lambda': []}
= Kriging(name='kriging',
S =123,
seed=50,
log_level=1,
n_theta=True)
noise
S.fit(X_train, y_train)
= np.linspace(start=-13, stop=13, num=1000).reshape(-1, 1)
X_axis = S.predict(X_axis, return_val="all")
mean_prediction, std_prediction, ei
#plt.plot(X, y, label=r"$f(x) = x \sin(x)$", linestyle="dotted")
="Observations")
plt.scatter(X_train, y_train, label#plt.plot(X, ei, label="Expected Improvement")
="mue")
plt.plot(X_axis, mean_prediction, label
plt.legend()"$x$")
plt.xlabel("$f(x)$")
plt.ylabel(= plt.title("Sphere: Gaussian process regression with nugget on noisy dataset") _
S.log
{'negLnLike': array([21.82530943]),
'theta': array([-0.41935831]),
'p': [],
'Lambda': array([5.20850895e-05])}
7.12 Cubic Function
import numpy as np
import spotPython
from spotPython.fun.objectivefunctions import analytical
from spotPython.spot import spot
from spotPython.design.spacefilling import spacefilling
from spotPython.build.kriging import Kriging
import matplotlib.pyplot as plt
= spacefilling(1)
gen = np.random.RandomState(1)
rng = np.array([-10])
lower = np.array([10])
upper = analytical().fun_cubed
fun = fun_control_init(
fun_control =get_spot_tensorboard_path(experiment_name),
spot_tensorboard_path=10.0,
sigma=123,)
seed
= gen.scipy_lhd(10, lower=lower, upper = upper)
X print(X)
= fun(X, fun_control=fun_control)
y print(y)
y.shape= X.reshape(-1,1)
X_train = y
y_train
= Kriging(name='kriging', seed=123, log_level=50, n_theta=1, noise=False)
S
S.fit(X_train, y_train)
= np.linspace(start=-13, stop=13, num=1000).reshape(-1, 1)
X_axis = S.predict(X_axis, return_val="all")
mean_prediction, std_prediction, ei
="Observations")
plt.scatter(X_train, y_train, label#plt.plot(X, ei, label="Expected Improvement")
="mue")
plt.plot(X_axis, mean_prediction, label
plt.legend()"$x$")
plt.xlabel("$f(x)$")
plt.ylabel(= plt.title("Cubed: Gaussian process regression on noisy dataset") _
[[ 0.63529627]
[-4.10764204]
[-0.44071975]
[ 9.63125638]
[-8.3518118 ]
[-3.62418901]
[ 4.15331 ]
[ 3.4468512 ]
[ 6.36049088]
[-7.77978539]]
[ -9.63480707 -72.98497325 12.7936499 895.34567477 -573.35961837
-41.83176425 65.27989461 46.37081417 254.1530734 -474.09587355]
= Kriging(name='kriging', seed=123, log_level=0, n_theta=1, noise=True)
S
S.fit(X_train, y_train)
= np.linspace(start=-13, stop=13, num=1000).reshape(-1, 1)
X_axis = S.predict(X_axis, return_val="all")
mean_prediction, std_prediction, ei
="Observations")
plt.scatter(X_train, y_train, label#plt.plot(X, ei, label="Expected Improvement")
="mue")
plt.plot(X_axis, mean_prediction, label
plt.legend()"$x$")
plt.xlabel("$f(x)$")
plt.ylabel(= plt.title("Cubed: Gaussian process with nugget regression on noisy dataset") _
import numpy as np
import spotPython
from spotPython.fun.objectivefunctions import analytical
from spotPython.spot import spot
from spotPython.design.spacefilling import spacefilling
from spotPython.build.kriging import Kriging
import matplotlib.pyplot as plt
= spacefilling(1)
gen = np.random.RandomState(1)
rng = np.array([-10])
lower = np.array([10])
upper = analytical().fun_runge
fun = fun_control_init(
fun_control =get_spot_tensorboard_path(experiment_name),
spot_tensorboard_path=0.25,
sigma=123,)
seed
= gen.scipy_lhd(10, lower=lower, upper = upper)
X print(X)
= fun(X, fun_control=fun_control)
y print(y)
y.shape= X.reshape(-1,1)
X_train = y
y_train
= Kriging(name='kriging', seed=123, log_level=50, n_theta=1, noise=False)
S
S.fit(X_train, y_train)
= np.linspace(start=-13, stop=13, num=1000).reshape(-1, 1)
X_axis = S.predict(X_axis, return_val="all")
mean_prediction, std_prediction, ei
="Observations")
plt.scatter(X_train, y_train, label#plt.plot(X, ei, label="Expected Improvement")
="mue")
plt.plot(X_axis, mean_prediction, label
plt.legend()"$x$")
plt.xlabel("$f(x)$")
plt.ylabel(= plt.title("Gaussian process regression on noisy dataset") _
[[ 0.63529627]
[-4.10764204]
[-0.44071975]
[ 9.63125638]
[-8.3518118 ]
[-3.62418901]
[ 4.15331 ]
[ 3.4468512 ]
[ 6.36049088]
[-7.77978539]]
[ 0.46517267 -0.03599548 1.15933822 0.05915901 0.24419145 0.21502359
-0.10432134 0.21312309 -0.05502681 -0.06434374]
= Kriging(name='kriging',
S =123,
seed=50,
log_level=1,
n_theta=True)
noise
S.fit(X_train, y_train)
= np.linspace(start=-13, stop=13, num=1000).reshape(-1, 1)
X_axis = S.predict(X_axis, return_val="all")
mean_prediction, std_prediction, ei
="Observations")
plt.scatter(X_train, y_train, label#plt.plot(X, ei, label="Expected Improvement")
="mue")
plt.plot(X_axis, mean_prediction, label
plt.legend()"$x$")
plt.xlabel("$f(x)$")
plt.ylabel(= plt.title("Gaussian process regression with nugget on noisy dataset") _
7.13 Factors
"num"] * 3 [
['num', 'num', 'num']
from spotPython.design.spacefilling import spacefilling
from spotPython.build.kriging import Kriging
from spotPython.fun.objectivefunctions import analytical
import numpy as np
= spacefilling(2)
gen = 30
n = np.random.RandomState(1)
rng = np.array([-5,-0])
lower = np.array([10,15])
upper = analytical().fun_branin_factor
fun #fun = analytical(sigma=0).fun_sphere
= gen.scipy_lhd(n, lower=lower, upper = upper)
X0 = np.random.randint(low=1, high=3, size=(n,))
X1 = np.c_[X0, X1]
X = fun(X)
y = Kriging(name='kriging', seed=123, log_level=50, n_theta=3, noise=False, var_type=["num", "num", "num"])
S
S.fit(X, y)= Kriging(name='kriging', seed=123, log_level=50, n_theta=3, noise=False, var_type=["num", "num", "factor"])
Sf
Sf.fit(X, y)= 50
n = gen.scipy_lhd(n, lower=lower, upper = upper)
X0 = np.random.randint(low=1, high=3, size=(n,))
X1 = np.c_[X0, X1]
X = fun(X)
y =np.sum(np.abs(S.predict(X)[0] - y))
s=np.sum(np.abs(Sf.predict(X)[0] - y))
sf- s sf
-40.513457642582125
# vars(S)
# vars(Sf)