使用交叉验证功能的Knn

时间:2019-05-28 01:04:40

标签: r cross-validation knn caret

我需要运行R代码以找到number of folder = 1的{​​{1}},但是显示了以下警告:

k=(c(1:12))

这是使用> warnings() Mensagens de aviso: 1: model fit failed for Fold1.Rep1: k= 1 Error in x[1, 1] : subscript out of bounds 2: model fit failed for Fold1.Rep1: k= 2 Error in x[1, 1] : subscript out of bounds 3: model fit failed for Fold1.Rep1: k= 3 Error in x[1, 1] : subscript out of bounds . . . 12: model fit failed for Fold1.Rep1: k=12 Error in x[1, 1] : subscript out of bounds 13: In nominalTrainWorkflow(x = x, y = y, wts = weights, info = trainInfo, ... : There were missing values in resampled performance measures. 软件包的R代码。

caret

1 个答案:

答案 0 :(得分:0)

尝试一下。

grid <- expand.grid(k = 1:12)

{
  set.seed(1)

  index <- caret::createDataPartition(biopsy_$class, p = 0.75, list = FALSE) # partiotion test-train

  train <- biopsy_[index, ]
  test  <- biopsy_[-index, ]

  ctrl <- caret::trainControl(method  = "repeatedcv", 
                              number  = 10, # see this
                              repeats = 10   # see this
                              )  

  model <- caret::train(class~., 
                        data = train, 
                        method = "knn",
                        trControl = ctrl,
                        preProcess = c("center","scale"),
                        tuneGrid = grid)
}

# plot(model)
# model$bestTune # best k

# library(dplyr)
# predictions <- model %>% predict(test)
# RMSE(predictions, test$class)
相关问题