樣本外誤差量測

使用 R 的 caret 進行 Machine Learning

Zach Mayer

Data Scientist at DataRobot and co-author of caret

樣本外誤差

  • 需要不過度擬合、能良好泛化的模型
  • 這些模型在新資料上表現如何?
  • 在新資料或測試集上測試模型
    • 機器學習的關鍵洞見
    • 樣本內驗證幾乎保證過度擬合
  • caret 與本課程的主要目標:避免過度擬合
使用 R 的 caret 進行 Machine Learning

範例:樣本外 RMSE

# Fit a model to the mtcars data
data(mtcars)
model <- lm(mpg ~ hp, mtcars[1:20, ])
# Predict out-of-sample
predicted <- predict(
  model, mtcars[21:32, ], type = "response"
)
# Evaluate error
actual <- mtcars[21:32, "mpg"]
sqrt(mean((predicted - actual) ^ 2))
5.507236
使用 R 的 caret 進行 Machine Learning

對照樣本內 RMSE

# Fit a model to the full dataset
model2 <- lm(mpg ~ hp, mtcars)
# Predict in-sample
predicted2 <- predict(
  model, mtcars, type = "response"
)
# Evaluate error
actual2 <- mtcars[, "mpg"]
sqrt(mean((predicted2 - actual2) ^ 2))
3.74

與樣本外 RMSE 5.5 相比。

使用 R 的 caret 進行 Machine Learning

一起來練習吧!

使用 R 的 caret 進行 Machine Learning

Preparing Video For Download...