簡體   English   中英

當使用帶有交叉驗證訓練控制的 train() 函數時,您如何看待模型在測試集上的預測?

[英]When using the train() function with cross-validation train-control, how do you see the model's predictions on the test set?

我是一個初學者,正在嘗試學習一些基本的機器學習技術。

我想使用留一法交叉驗證和 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")
)

您可以在trainControl設置savePredictions=TRUE

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 種組合,因此您可以在上面看到每個被遺漏的觀察結果,這是預測

如果有人感興趣,請回答我自己的后續問題:

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM