Estimating test error
Now that you have your posterior_predictive (available to you in your workspace), you can evaluate model performance on new data. To do this, you will need to loop over the test observations, and for each of them, compute the prediction error as the difference between the predictive distribution for this observation and the actual, true value. This will give you the distribution of your model's error, which you can then visualize.
You will need pymc3 and numpy, which have been imported for you as pm and np, respectively. The test data, bikes_test, is also available in your workspace. Let's get to it!
Este exercício faz parte do curso
Bayesian Data Analysis in Python
Instruções do exercício
- Initialize
errorsas an empty list. - For each row in
bikes_test, calculate prediction error as the predictive draws for this row fromposterior_predictiveminus the single true value ofnum_bikesfrom the row. - Reshape
errorsby converting them to anumpyarray and applying the.reshape()method to the outcome, and assign the final result toerror_distribution. - Plot the test error distribution using
pymc3'splot_posterior()function.
Exercício interativo prático
Experimente este exercício completando este código de exemplo.
# Initialize errors
errors = ____
# Iterate over rows of bikes_test to compute error per row
for index, test_example in bikes_test.iterrows():
error = ____[____][:, ____] - ____[____]
errors.append(error)
# Reshape errors
error_distribution = ____(____).____()
# Plot the error distribution
____
plt.show()