Skip to content

Instantly share code, notes, and snippets.

@HajimeKawahara
Created January 31, 2022 07:46
Show Gist options
  • Select an option

  • Save HajimeKawahara/5f25c7563883d25269513123aa7bc6d9 to your computer and use it in GitHub Desktop.

Select an option

Save HajimeKawahara/5f25c7563883d25269513123aa7bc6d9 to your computer and use it in GitHub Desktop.
import GPy
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(seed=1)
kernel = GPy.kern.Matern32(2, ARD=True)
N = 100
X = np.random.uniform(-3.,3.,(N, 2))
Z = np.sin(X[:,0:1]) * np.sin(X[:,1:2]) + np.random.randn(N,1)*0.05
model = GPy.models.GPRegression(X, Z, kernel)
model.optimize(messages=True, max_iters=1e5)
## prediction
Nx=50
Ny=75
xgrid=np.linspace(-3, 3, Nx)
ygrid=np.linspace(-3, 3, Ny)
X1, X2 = np.meshgrid(xgrid,ygrid)
input_grid = np.array([X1.flatten(), X2.flatten()]).T
z_pred = model.predict_quantiles(input_grid, quantiles=(2.5, 50, 97.5))[1]
from mpl_toolkits.mplot3d import Axes3D
Zpred=z_pred.reshape(Ny,Nx)
fig=plt.figure()
ax = fig.add_subplot(111, projection='3d')
surf = ax.plot_surface(X1, X2, Zpred, cmap='bwr', linewidth=0,alpha=0.3)
ax.scatter3D(X[:,0],X[:,1],Z,color="k")
fig.colorbar(surf)
plt.show()
@HajimeKawahara
Copy link
Copy Markdown
Author

Figure_gpy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment