I change my codes like this:
ax.plot(X_new, pred_samples.posterior['f_pred'], 'C1-', alpha=0.3)
but still get error
AttributeError: ‘InferenceData’ object has no attribute ‘posterior’
My purpose is to plot the fitted function on the original data to visually inspect its fit to the data and the uncertainty in the predictions,like this:
Here is my entire code
X = x[:, None]
with pm.Model() as model_reg:
# hyperprior for lengthscale kernel parameter
ℓ = pm.Gamma('ℓ', 2, 0.5)
# instanciate a covariance function
cov = pm.gp.cov.ExpQuad(1, ls=ℓ)
# instanciate a GP prior
gp = pm.gp.Marginal(cov_func=cov)
# prior
ϵ = pm.HalfNormal('ϵ', 25)
# likelihood
y_pred = gp.marginal_likelihood('y_pred', X=X, y=y, sigma=ϵ)
trace_reg = pm.sample(2000)
az.plot_trace(trace_reg)
#%%
X_new = np.linspace(np.floor(x.min()), np.ceil(x.max()), 100)[:,None]
#%%
with model_reg:
f_pred = gp.conditional('f_pred', X_new)
#%%
with model_reg:
# pred_samples = pm.sample_posterior_predictive(f_pred)
pred_samples = pm.sample_posterior_predictive(trace_reg, var_names=['f_pred'])
#%%
_, ax = plt.subplots(figsize=(12,5))
#ax.plot(X_new, pred_samples.posterior['f_pred'].T, 'C1-', alpha=0.3)
ax.plot(X_new, pred_samples.posterior['f_pred'], 'C1-', alpha=0.3)
ax.plot(X, y, 'ko')
ax.set_xlabel('X')```
