準確度指標:迴歸模型

Python 的模型驗證

Kasey Jones

Data Scientist

迴歸模型

迴歸模型用於連續變數。例如得分、加侖數或小狗數量!

Python 的模型驗證

平均絕對誤差(MAE)

 

$$ MAE = \frac{\sum_{i=1}^{n} |y_i - \hat{y}_i|}{n} $$

  • 最簡單且直觀的指標
  • 對所有點一視同仁
  • 對離群值不敏感
Python 的模型驗證

平均平方誤差(MSE)

 

$$ MSE = \frac{\sum_{i=1}^{n} (y_i - \hat{y}_i) ^2}{n} $$

  • 最常用的迴歸指標
  • 讓離群誤差對總體誤差影響更大
  • 例如隨機的全家自駕旅行可能造成預測誤差很大
Python 的模型驗證

MAE vs. MSE

  • 準確度指標總是依應用情境而定
  • MAE 與 MSE 的誤差單位不同,不能直接比較
Python 的模型驗證

平均絕對誤差

rfr = RandomForestRegressor(n_estimators=500, random_state=1111)
rfr.fit(X_train, y_train)
test_predictions = rfr.predict(X_test)

sum(abs(y_test - test_predictions))/len(test_predictions)
9.99
from sklearn.metrics import mean_absolute_error
mean_absolute_error(y_test, test_predictions)
9.99
Python 的模型驗證

平均平方誤差

sum(abs(y_test - test_predictions)**2)/len(test_predictions)
141.4
from sklearn.metrics import mean_squared_error
mean_squared_error(y_test, test_predictions)
141.4
Python 的模型驗證

資料子集的準確度

chocolate_preds = rfr.predict(X_test[X_test[:, 1] == 1])
mean_absolute_error(y_test[X_test[:, 1] == 1], chocolate_preds)
8.79
nonchocolate_preds = rfr.predict(X_test[X_test[:, 1] == 0])
mean_absolute_error(y_test[X_test[:, 1] == 0], nonchocolate_preds)
10.99
Python 的模型驗證

Let's practice

Python 的模型驗證

Preparing Video For Download...