開始使用免費開始

均方誤差

再回到 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))
編輯並執行程式碼