########################################################
# This is just a helper function, no need to worry about
# The internals.
# We will return to this example in Week 14
########################################################
from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.gaussian_process.kernels import RBF, ConstantKernel as C
np.random.seed(1)
# Mesh the input space for evaluations of the real function, the prediction and
# its MSE
x = np.atleast_2d(np.linspace(0, 10, 1000)).T
# Create a Gaussian Process model
#kernel = C(1.0, (1e-3, 1e3)) * RBF(10, (1e-2, 1e2))
kernel = C(1.0, (1e-3, 1e3)) * RBF(10, (1e-2, 1e2))
#gp = GaussianProcessRegressor(kernel=kernel, n_restarts_optimizer=9)
kernel = C(3.0)*RBF(1.5)
gp = GaussianProcessRegressor(kernel=kernel,alpha=1e-6,optimizer=None)
#gp = GaussianProcess(corr='cubic', theta0=1e-2, thetaL=1e-4, thetaU=1e-1,random_start=100)
# Now, ready to begin learning:
train_ind ={
'Upper CB': np.zeros(len(X_bo),dtype=bool),
'Random':np.zeros(len(X_bo),dtype=bool)
}
options = train_ind.keys()
possible_points = np.array(list(range(len(X_bo))))
# Possible Initialization options
# 1. Select different points randomly
#for i in range(2):
# for o in options:
# ind = np.random.choice(possible_points[~train_ind[o]],1)
# train_ind[o][ind] = True
# 2. Start with end-points
#for o in options:
# train_ind[o][0] = True
# train_ind[o][-1] = True
# 3. Start with same random points
for ind in np.random.choice(possible_points,2):
for o in options:
train_ind[o][ind] = True
plot_list = np.array([5,10,20,30,40,50,len(X_bo)])
for i in range(10):
# As i increases, we increase the number of points
plt.figure(figsize=(16,6))
for j,o in enumerate(options):
plt.subplot(1,2,j+1)
gp.fit(X_bo[train_ind[o],:],y_bo[train_ind[o]])
yp,sigma = gp.predict(X_bo[~train_ind[o],:], return_std=True)
ucb = yp + 1.96*sigma
if o == 'Upper CB':
#candidates = np.extract(MSE == np.amax(MSE),X_bo[~train_ind[o],:])
candidates = np.extract(ucb == np.amax(ucb),X_bo[~train_ind[o],:])
next_point = np.random.choice(candidates.flatten())
next_ind = np.argwhere(X_bo.flatten() == next_point)
elif o == 'Random':
next_ind = np.random.choice(possible_points[~train_ind[o]],1)
train_ind[o][next_ind] = True
# Plot intermediate results
yp,sigma = gp.predict(x, return_std=True)
plt.fill(np.concatenate([x, x[::-1]]),
np.concatenate([yp - 1.9600 * sigma,
(yp + 1.9600 * sigma)[::-1]]),'b',
alpha=0.05, ec='g', label='95% confidence interval')
n_train = np.count_nonzero(train_ind[o])
gp.fit(X_bo[train_ind[o],:],y_bo[train_ind[o]])
# Show progress
yp,sigma = gp.predict(x, return_std=True)
yt = f(x)
error = np.linalg.norm(yp-yt.flatten())
plt.fill(np.concatenate([x, x[::-1]]),
np.concatenate([yp - 1.9600 * sigma,
(yp + 1.9600 * sigma)[::-1]]),'b',
alpha=0.3, ec='None', label='95% confidence interval')
plt.plot(x,yt,'k--',alpha=1)
plt.plot(x,yp,'r-',alpha=1)
plt.scatter(X_bo[train_ind[o],:],y_bo[train_ind[o]],color='g',s=100)
plt.scatter(X_bo[next_ind,:].flatten(),y_bo[next_ind].flatten(),color='r',s=150)
plt.ylim([-10,15])
plt.xlim([0,10])
plt.title("%s\n%d training points\n%.2f error"%(o,n_train,error))
plt.show()