0

我是一名初学者,试图学习一些基本的机器学习技术。

我想使用留一法交叉验证和 train() 函数来训练模型。我的功能似乎可以正常工作。但是,我看不到模型的测试集预测。给定以下模型,您将如何做到这一点?

# Create custom trainControl: myControl
myControl <- trainControl(
  method = "loocv", 
  verboseIter = TRUE
)

# Fit glmnet model: model
model <- train(
  y ~ ., 
  data,
  method = "glmnet",
  trControl = myControl,
  preProcess = c("center", "scale", "pca")
)
4

2 回答 2

0

如果有人感兴趣,请回答我自己的后续问题:

myControl <- trainControl(
  method = "loocv"
  savePredictions = "final",
)

model <- train(
  y ~ ., 
  data,
  method = "glmnet",
  trControl = myControl,
  preProcess = c("center", "scale", "pca")
)
data$pred <- model$pred[ , "pred"]

于 2020-02-02T02:29:24.410 回答
0

您可以savePredictions=TRUE设置trainControl

myControl <- trainControl(
  method = "loocv", 
  savePredictions=TRUE
)

model <- train(
  mpg ~ ., 
  data,
  method = "glmnet",
  trControl = myControl,
  preProcess = c("center", "scale", "pca"),
  tuneGrid = expand.grid(alpha = c(0.1,0.01),lambda = c(0.1,0.01))
)

您可以使用每个参数组合查看预测:

      pred obs rowIndex alpha lambda Resample
1 22.56265  21        1  0.10   0.10   Fold01
2 22.59835  21        1  0.10   0.01   Fold01
3 22.57767  21        1  0.01   0.10   Fold01
4 22.59717  21        1  0.01   0.01   Fold01
5 22.12174  21        2  0.10   0.10   Fold02
6 22.14886  21        2  0.10   0.01   Fold02
7 22.13080  21        2  0.01   0.10   Fold02
8 22.14667  21        2  0.01   0.01   Fold02

我测试了 lambda 和 alpha 的 4 种组合,因此您可以在上面看到每个被遗漏的观察结果,它是预测

于 2020-02-02T00:16:20.090 回答