1- from __future__ import annotations
2-
3- # google-style docstring documentation, type information, an example, and return values: 2023, July, 15th
4- # tests: None
5-
61import copy
72from math import erf
83import matplotlib .pyplot as plt
4742
4843
4944class Kriging (surrogates ):
45+ """Kriging surrogate.
46+
47+ Attributes:
48+ nat_range_X (list):
49+ List of X natural ranges.
50+ nat_range_y (list):
51+ List of y nat ranges.
52+ noise (bool):
53+ noisy objective function. Default: False. If `True`, regression kriging will be used.
54+ var_type (str):
55+ variable type. Can be either `"num`" (numerical) of `"factor"` (factor).
56+ num_mask (array):
57+ array of bool variables. `True` represent numerical (float) variables.
58+ factor_mask (array):
59+ array of factor variables. `True` represents factor (unordered) variables.
60+ int_mask (array):
61+ array of integer variables. `True` represents integers (ordered) variables.
62+ ordered_mask (array):
63+ array of ordered variables. `True` represents integers or float (ordered) variables.
64+ Set of veriables which an order relation, i.e., they are either num (float) or int.
65+ name (str):
66+ Surrogate name
67+ seed (int):
68+ Random seed.
69+ use_cod_y (bool):
70+ Use coded y values.
71+ sigma (float):
72+ Kriging sigma.
73+ gen (method):
74+ Design generator, e.g., spotPython.design.spacefilling.spacefilling.
75+ min_theta (float):
76+ min log10 theta value. Defaults: -6.
77+ max_theta (float):
78+ max log10 theta value. Defaults: 3.
79+ min_p (float):
80+ min p value. Default: 1.
81+ max_p (float):
82+ max p value. Default: 2.
83+ """
5084 def __init__ (
5185 self : object ,
5286 noise : bool = False ,
@@ -68,7 +102,7 @@ def __init__(
68102 ** kwargs
69103 ):
70104 """
71- Kriging surrogate.
105+ Initialize the Kriging surrogate.
72106
73107 Args:
74108 noise (bool): Use regression instead of interpolation kriging. Defaults to False.
@@ -92,73 +126,39 @@ def __init__(
92126 spot_writer : Spot writer.
93127 counter : Counter.
94128
95- Attributes:
96- nat_range_X (list):
97- List of X natural ranges.
98- nat_range_y (list):
99- List of y nat ranges.
100- noise (bool):
101- noisy objective function. Default: False. If `True`, regression kriging will be used.
102- var_type (str):
103- variable type. Can be either `"num`" (numerical) of `"factor"` (factor).
104- num_mask (array):
105- array of bool variables. `True` represent numerical (float) variables.
106- factor_mask (array):
107- array of factor variables. `True` represents factor (unordered) variables.
108- int_mask (array):
109- array of integer variables. `True` represents integers (ordered) variables.
110- ordered_mask (array):
111- array of ordered variables. `True` represents integers or float (ordered) variables.
112- Set of veriables which an order relation, i.e., they are either num (float) or int.
113- name (str):
114- Surrogate name
115- seed (int):
116- Random seed.
117- use_cod_y (bool):
118- Use coded y values.
119- sigma (float):
120- Kriging sigma.
121- gen (method):
122- Design generator, e.g., spotPython.design.spacefilling.spacefilling.
123- min_theta (float):
124- min log10 theta value. Defaults: -6.
125- max_theta (float):
126- max log10 theta value. Defaults: 3.
127- min_p (float):
128- min p value. Default: 1.
129- max_p (float):
130- max p value. Default: 2.
131-
132129 Examples:
133- Surrogate of the x*sin(x) function.
134- See:
135- [scikit-learn](https://scikit-learn.org/stable/auto_examples/gaussian_process/plot_gpr_noisy_targets.html)
130+ Surrogate of the x*sin(x) function, see [1].
136131
137132 >>> from spotPython.build.kriging import Kriging
138- >>> import numpy as np
139- >>> import matplotlib.pyplot as plt
140- >>> rng = np.random.RandomState(1)
141- >>> X = linspace(start=0, stop=10, num=1_000).reshape(-1, 1)
142- >>> y = np.squeeze(X * np.sin(X))
143- >>> training_indices = rng.choice(arange(y.size), size=6, replace=False)
144- >>> X_train, y_train = X[training_indices], y[training_indices]
145- >>> S = Kriging(name='kriging', seed=124)
146- >>> S.fit(X_train, y_train)
147- >>> mean_prediction, std_prediction = S.predict(X)
148- >>> plt.plot(X, y, label=r"$f(x)$", linestyle="dotted")
149- >>> plt.scatter(X_train, y_train, label="Observations")
150- >>> plt.plot(X, mean_prediction, label="Mean prediction")
151- >>> plt.fill_between(
152- X.ravel(),
153- mean_prediction - 1.96 * std_prediction,
154- mean_prediction + 1.96 * std_prediction,
155- alpha=0.5,
156- label=r"95% confidence interval",
157- )
158- >>> plt.legend()
159- >>> plt.xlabel("$x$")
160- >>> plt.ylabel("$f(x)$")
161- >>> _ = plt.title("Gaussian process regression on noise-free dataset")
133+ import numpy as np
134+ import matplotlib.pyplot as plt
135+ rng = np.random.RandomState(1)
136+ X = linspace(start=0, stop=10, num=1_000).reshape(-1, 1)
137+ y = np.squeeze(X * np.sin(X))
138+ training_indices = rng.choice(arange(y.size), size=6, replace=False)
139+ X_train, y_train = X[training_indices], y[training_indices]
140+ S = Kriging(name='kriging', seed=124)
141+ S.fit(X_train, y_train)
142+ mean_prediction, std_prediction = S.predict(X)
143+ plt.plot(X, y, label=r"$f(x)$", linestyle="dotted")
144+ plt.scatter(X_train, y_train, label="Observations")
145+ plt.plot(X, mean_prediction, label="Mean prediction")
146+ plt.fill_between(
147+ X.ravel(),
148+ mean_prediction - 1.96 * std_prediction,
149+ mean_prediction + 1.96 * std_prediction,
150+ alpha=0.5,
151+ label=r"95% confidence interval",
152+ )
153+ plt.legend()
154+ plt.xlabel("$x$")
155+ plt.ylabel("$f(x)$")
156+ _ = plt.title("Gaussian process regression on noise-free dataset")
157+ plt.show()
158+
159+ References:
160+
161+ [[1](https://scikit-learn.org/stable/auto_examples/gaussian_process/plot_gpr_noisy_targets.html)] scikit-learn: Gaussian Processes regression: basic introductory example
162162
163163 """
164164 super ().__init__ (name , seed , log_level )
0 commit comments