開始使用免費開始

平均絕對誤差

要把模型結果解釋清楚並不容易。不過,多數客戶能理解「平均而言,預測模型偏差了多少」。這讓解釋平均絕對誤差變得簡單。舉例來說,當你預測一支籃球隊的勝場數為 42,實際結果是 40,你就能很直接地說明誤差是 2 場。

在這個練習中,你正在面試一個新職位,手邊有兩個陣列:y_test 是 2017 年所有 30 支 NBA 球隊的真實勝場數;predictions 則包含對每一隊的預測。為了測試你的理解,你需要同時手動計算 MAE,並使用 sklearn 計算一次。

本練習屬於課程

Python 的模型驗證

檢視課程

練習說明

  • 手動計算 MAE,並以 n 表示被預測的觀測數量。
  • 使用 sklearn 計算 MAE。
  • 使用提供的 print 陳述式印出兩個誤差數值。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

from sklearn.metrics import mean_absolute_error

# Manually calculate the MAE
n = ____(predictions)
mae_one = sum(____(y_test - predictions)) / n
print('With a manual calculation, the error is {}'.format(____))

# Use scikit-learn to calculate the MAE
mae_two = ____(____, ____)
print('Using scikit-learn, the error is {}'.format(____))
編輯並執行程式碼