样本外误差度量

在 R 中使用 caret 的机器学习

Zach Mayer

Data Scientist at DataRobot and co-author of caret

样本外误差

  • 需要不过拟合、泛化好的模型
  • 模型在新数据上表现好吗?
  • 用新数据(测试集)评估模型
    • 机器学习的关键洞见
    • 样本内验证几乎必然过拟合
  • caret 与本课程的首要目标:避免过拟合
在 R 中使用 caret 的机器学习

示例:样本外 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 的机器学习

对比样本内 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 的机器学习

让我们来练习!

在 R 中使用 caret 的机器学习

Preparing Video For Download...