Get startedGet started for free

Mean squared error

Let's focus on the 2017 NBA predictions again. Every year, there are at least a couple of NBA teams that win way more games than expected. If you use the MAE, this accuracy metric does not reflect the bad predictions as much as if you use the MSE. Squaring the large errors from bad predictions will make the accuracy look worse.

In this example, NBA executives want to better predict team wins. You will use the mean squared error to calculate the prediction error. The actual wins are loaded as y_test and the predictions as predictions.

This exercise is part of the course

Model Validation in Python

View Course

Exercise instructions

  • Manually calculate the MSE. $$ MSE = \frac{\sum_{i=1}^{n} (y_i - \hat{y}_i ) ^2 }{n} $$
  • Calculate the MSE using sklearn.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

from sklearn.metrics import ____

n = ___(predictions)
# Finish the manual calculation of the MSE
mse_one = sum((y_test - predictions)____) / n
print('With a manual calculation, the error is {}'.format(mse_one))

# Use the scikit-learn function to calculate MSE
mse_two = ____
print('Using scikit-learn, the error is {}'.format(mse_two))
Edit and Run Code