gpt4 book ai didi

r - “The format of predictions is incorrect”

转载 作者:行者123 更新时间:2023-12-02 11:01:02 28 4
gpt4 key购买 nike

实现ROCR曲线,kNN,K进行10倍交叉验证。
我正在使用电离层数据集。

这是属性信息,供您引用:

-如上所述,所有34个都是连续的
-根据定义,第35个属性为“好”或“坏”
以上总结。这是一个二进制分类任务。

data1<-read.csv('https://archive.ics.uci.edu/ml/machine-learning-databases/ionosphere/ionosphere.data',header = FALSE)

knn本身可以工作,kfold的kNN也可以。但是当我输入ROCR代码时,它并不喜欢它。
我收到错误:“预测格式不正确”。
我检查了数据帧pred和Class1。尺寸相同。我尝试使用data.test $ V35而不是Class1,使用此选项时遇到相同的错误。
 install.packages("class")
library(class)

nrFolds <- 10
data1[,35]<-as.numeric(data1[,35])

# generate array containing fold-number for each sample (row)
folds <- rep_len(1:nrFolds, nrow(data1))


# actual cross validation
for(k in 1:nrFolds) {
# actual split of the data
fold <- which(folds == k)
data.train <- data1[-fold,]
data.test <- data1[fold,]

Class<-data.train[,35]
Class1<-data.test[,35]
# train and test your model with data.train and data.test

pred<-knn(data.train, data.test, Class, k = 5, l = 0, prob = FALSE, use.all = TRUE)
data<-data.frame('predict'=pred, 'actual'=Class1)
count<-nrow(data[data$predict==data$actual,])
total<-nrow(data.test)
avg = (count*100)/total
avg =format(round(avg, 2), nsmall = 2)
method<-"KNN"
accuracy<-avg
cat("Method = ", method,", accuracy= ", accuracy,"\n")
}

install.packages("ROCR")
library(ROCR)
rocrPred=prediction(pred, Class1, NULL)
rocrPerf=performance(rocrPred, 'tpr', 'fpr')
plot(rocrPerf, colorize=TRUE, text.adj=c(-.2,1.7))

任何帮助表示赞赏。

最佳答案

这对我有用。

install.packages("class")
library(class)
library(ROCR)
nrFolds <- 10
data1[,35]<-as.numeric(data1[,35])

# generate array containing fold-number for each sample (row)
folds <- rep_len(1:nrFolds, nrow(data1))


# actual cross validation
for(k in 1:nrFolds) {
# actual split of the data
fold <- which(folds == k)
data.train <- data1[-fold,]
data.test <- data1[fold,]

Class<-data.train[,35]
Class1<-data.test[,35]
# train and test your model with data.train and data.test

pred<-knn(data.train, data.test, Class, k = 5, l = 0, prob = FALSE, use.all = TRUE)
data<-data.frame('predict'=pred, 'actual'=Class1)
count<-nrow(data[data$predict==data$actual,])
total<-nrow(data.test)
avg = (count*100)/total
avg =format(round(avg, 2), nsmall = 2)
method<-"KNN"
accuracy<-avg
cat("Method = ", method,", accuracy= ", accuracy,"\n")

pred <- prediction(Class1,pred)
perf <- performance(pred, "tpr", "fpr")
plot(perf, colorize=T, add=TRUE)
abline(0,1)
}

关于r - “The format of predictions is incorrect”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40429542/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com