def run_regression_1D():
np.random.seed(42)
print "create dataset ..."
N = 50
rng = np.random.RandomState(42)
X = np.sort(2 * rng.rand(N, 1) - 1, axis=0)
Y = np.array([np.pi * np.sin(10 * X).ravel(),
np.pi * np.cos(10 * X).ravel()]).T
Y += (0.5 - rng.rand(*Y.shape))
Y = Y / np.std(Y, axis=0)
def plot(model, alpha, fname):
xx = np.linspace(-1.2, 1.2, 200)[:, None]
if isinstance(model, IndepSGPR):
mf, vf = model.predict_f(xx, alpha)
else:
# mf, vf = model.predict_f(xx, alpha, use_mean_only=False)
mf, vf = model.predict_f(xx, alpha, use_mean_only=True)
colors = ['r', 'b']
plt.figure()
for i in range(model.Dout):
plt.subplot(model.Dout, 1, i + 1)
plt.plot(X, Y[:, i], 'x', color=colors[i], mew=2)
zu = model.models[i].zu
mean_u, var_u = model.models[i].predict_f(zu, alpha)
plt.plot(xx, mf[:, i], '-', color=colors[i], lw=2)
plt.fill_between(
xx[:, 0],
mf[:, i] - 2 * np.sqrt(vf[:, i]),
mf[:, i] + 2 * np.sqrt(vf[:, i]),
color=colors[i], alpha=0.3)
# plt.errorbar(zu[:, 0], mean_u, yerr=2*np.sqrt(var_u), fmt='ro')
plt.xlim(-1.2, 1.2)
plt.savefig(fname)
# inference
print "create independent output model and optimize ..."
M = N
alpha = 0.01
indep_model = IndepSGPR(X, Y, M)
indep_model.train(alpha=alpha)
plot(indep_model, alpha, '/tmp/reg_indep_multioutput.pdf')
print "create correlated output model and optimize ..."
M = N
ar_model = AutoSGPR(X, Y, M)
ar_model.train(alpha=alpha)
plot(ar_model, alpha, '/tmp/reg_autoreg_multioutput.pdf')
评论列表
文章目录