我正在使用class
包来使用KNN算法。我也使用ROCR
包来计算AUC值。
knn_one<-knn(train, test, train$Digit, k=1)
计算另一种方法的AUC值,例如分类树,我使用了以下一系列命令:
treeTrain_Pred<-predict(Tree_Train, test , type = "prob")[,2]
Pred<-prediction(treeTrain_Pred, test$Digit)
Perf<-performance(Pred, "auc")
Perf@y.values[[1]]
然而,当我尝试
时knn_one = predict(knn_one, test, type="prob")[,2]
我收到以下错误:
Error in UseMethod("predict") :
no applicable method for 'predict' applied to an object of class "factor"
如何修复此问题并获取KNN功能的AUC值?
答案 0 :(得分:1)
对于knn模型没有预测方法,而是在单个调用中训练和接收预测。声纳数据示例:
library(mlbench)
data(Sonar)
创建数据分区:
set.seed(1)
tr_ind <- sample(1:nrow(Sonar), 150)
train <- Sonar[tr_ind,]
test <- Sonar[-tr_ind,]
mod <- class::knn(cl = train$Class,
test = test[,1:60],
train = train[,1:60],
k = 5,
prob = TRUE)
现在预测的概率在:
attributes(mod)$prob
library(pROC)
roc(test$Class, attributes(mod)$prob)
#output
Call:
roc.default(response = test$Class, predictor = attributes(mod)$prob)
Data: attributes(mod)$prob in 30 controls (test$Class M) < 28 cases (test$Class R).
Area under the curve: 0.4667
plot(roc(test$Class, attributes(mod)$prob),
print.thres = T,
print.auc=T)
让我们试试k = 4
mod <- class::knn(cl = train$Class,
test = test[,1:60],
train = train[,1:60],
k = 4,
prob = TRUE)
plot(roc(test$Class, attributes(mod)$prob),
print.thres = T,
print.auc = T,
print.auc.y = 0.2)