# ガウス過程回帰(Gaussian Process Regression, GPR)の例
# 1) データを生成
# from sklearn.datasets import make_regression
# X, y = make_regression(n_samples=80, n_features=1, noise=12.0, random_state=42)
import numpy as np
X = np.linspace(start=0, stop=10, num=1_000).reshape(-1, 1)
y = np.squeeze(X * np.sin(X))
rng = np.random.RandomState(0)
training_indices = rng.choice(np.arange(y.size), size=6, replace=False)
X_train, y_train = X[training_indices], y[training_indices]
# 2) ガウス過程のカーネル設定
# カーネルは Constant * RBF + WhiteKernel(典型的な選択)
from sklearn.gaussian_process.kernels import RBF, WhiteKernel, ConstantKernel as C
kernel = C(1.0, (1e-3, 1e3)) * RBF(length_scale=1.0, length_scale_bounds=(1e-2, 1e3)) \
+ WhiteKernel(noise_level=1.0, noise_level_bounds=(1e-5, 1e1))
# 3) モデル学習
from sklearn.gaussian_process import GaussianProcessRegressor
gp = GaussianProcessRegressor(kernel=kernel, normalize_y=True, random_state=42)
gp.fit(X_train, y_train)
# 4) 予測(きれいな曲線を出すため細かいグリッド上で)
import matplotlib.pyplot as plt
X_test = np.linspace(X.min() - 1.0, X.max() + 1.0, 400).reshape(-1, 1)
y_mean, y_std = gp.predict(X_test, return_std=True)
y_upper = y_mean + 1.96 * y_std # 95%信頼帯
y_lower = y_mean - 1.96 * y_std
# 5) プロット
mean_prediction, std_prediction = gp.predict(X, return_std=True)
plt.plot(X, y, label=r"$f(x) = x \sin(x)$", linestyle="dotted")
plt.scatter(X_train, y_train, label="Observations")
plt.plot(X, mean_prediction, label="Mean prediction")
plt.fill_between(
X.ravel(),
mean_prediction - 1.96 * std_prediction,
mean_prediction + 1.96 * std_prediction,
alpha=0.5,
label=r"95% confidence interval",
)
plt.legend()
plt.xlabel("$x$")
plt.ylabel("$f(x)$")
_ = plt.title("Gaussian process regression on noise-free dataset")