使用knn函数

时间:2019-02-26 21:36:04

标签: r loops knn

cv = createFolds(y = cancer$Class, k = 3)

for (k in 1:9) {
  accuracy = c()
  for (test_rows in cv) {
    train = cancer_train[-test_rows,]
    test = cancer_test[test_rows,]
    pred_knn = knn(train = train[,1:9], test = test[,1:9], cl = train$Class, k = k)
    cm = confusionMatrix(pred_knn, test$Class)
    accuracy = c(accuracy, cm$overall[1])
  }
  print(mean(accuracy))
}

当我在与癌症研究有关的数据集上运行此值时,该值在数据集中的值均为1-10时,我收到此错误“错误knn(train = train [,1:9],test = test [,1:9] ,cl = train $ Class,:   不允许缺少任何值”

0 个答案:

没有答案