均方误差
我们再来看 2017 年 NBA 的预测。每年总会有至少几支球队的胜场数远远高于预期。如果使用 MAE,这个准确性度量对这些糟糕预测的惩罚不如使用 MSE 强。将这些大误差平方后,会让模型的准确性看起来更差。
在这个示例中,NBA 管理层希望更好地预测球队胜场数。您将使用均方误差来计算预测误差。实际胜场数已加载为 y_test,预测结果为 predictions。
本练习是课程的一部分
Python 中的模型验证
练习说明
- 手动计算 MSE。 $$ MSE = \frac{\sum_{i=1}^{n} (y_i - \hat{y}_i ) ^2 }{n} $$
- 使用
sklearn计算 MSE。
交互式实操练习
通过完成这段示例代码来试试这个练习。
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))