Aan de slagGa gratis aan de slag

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!

Deze oefening maakt deel uit van de cursus

Bayesian Data Analysis in Python

Cursus bekijken

Oefeninstructies

  • Initialize errors as an empty list.
  • For each row in bikes_test, calculate prediction error as the predictive draws for this row from posterior_predictive minus the single true value of num_bikes from the row.
  • Reshape errors by converting them to a numpy array and applying the .reshape() method to the outcome, and assign the final result to error_distribution.
  • Plot the test error distribution using pymc3's plot_posterior() function.

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

# 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()
Code bewerken en uitvoeren