#### 高斯过程回归

y=f(x)+ϵ

GP(m(x),k(x,x))

```import matplotlib.pyplot as plt
import numpy as np
import gpflow
# 生成数据的函数
def f(x):
return np.sin(2*np.pi*x)
# 我们生成一些假设高斯加性噪声的数据
X = np.array([0, 0, 0.2, 0.21, 0.7, 0.75, 0.8, 0.83, 0.9])
N = len(X)
epsilon = np.random.normal(scale=0.1, size=N)
y = f(X) + epsilon
X = X.reshape(-1, 1)
y = y.reshape(-1, 1)
# Plot the problem
plt.figure(figsize=(12, 6))
plt.plot(X, y, "kx", mew=2, c="g")
plt.xlabel("x")
plt.ylabel("y")
plt.savefig("problem.png", dpi=300)
# 定义核
kernel = gpflow.kernels.RBF()
# 构建模型
model = gpflow.models.GPR(data=(X, y), kernel=kernel, mean_function=None)
# Train the model
opt = gpflow.optimizers.Scipy()
opt.minimize(model.training_loss, model.trainable_variables)
# 生成用于预测的测试点
x_test = np.linspace(-0.5, 1.5, 100).reshape(100, 1)  # test points must be of shape (N, D)
## predict mean and variance of latent GP at test points
mean, var = model.predict_f(x_test)
## 绘制预测
plt.figure(figsize=(12, 6))
plt.plot(X, y, "kx", mew=2, c="g")
plt.plot(x_test, mean, "C0", lw=2)
plt.fill_between(
xx[:, 0],
mean[:, 0] - 2 * np.sqrt(var[:, 0]),
mean[:, 0] + 2 * np.sqrt(var[:, 0]),
color="C0",
alpha=0.2,
)
_ = plt.xlim(-0.5, 1.5)
plt.xlabel("x")
plt.ylabel("y")
plt.show()```