-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgaussian_process.py
More file actions
28 lines (20 loc) · 821 Bytes
/
gaussian_process.py
File metadata and controls
28 lines (20 loc) · 821 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import numpy as np
import matplotlib.pyplot as plt
from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.gaussian_process.kernels import RBF
X = np.array([[-1, 0], [-0.5, 0.5], [0, 1], [1, 0]])
y = np.array([-1, 0.5, 2, 0])
kernel = RBF(length_scale=1.0)
model = GaussianProcessRegressor(kernel=kernel)
model.fit(X, y)
new_input = np.array([[-0.2, 0.2]])
prediction, std_dev = model.predict(new_input, return_std=True)
print("Prediction: ", prediction[0])
print("Standard deviation: ", std_dev[0])
x_plot = np.linspace(-1.5, 1.5, 100)
X_plot = np.vstack((x_plot, np.zeros(100))).T
y_plot, std_plot = model.predict(X_plot, return_std=True)
plt.plot(x_plot, y_plot)
plt.fill_between(x_plot, y_plot - std_plot, y_plot + std_plot, alpha=0.2)
plt.scatter(X[:, 0], y, c='r', marker='x')
plt.show()