'火车'和'班级'有不同的长度

时间:2015-01-29 19:51:20

标签: r knn

获得好的'''火车'和'班级'有不同的长度“错误。请指教。我也可能不应该使用一个变量作为分类器来获得完全分类的测试集。现在请注意,我打算使用一个变量。所以请解决“'火车'和'班级'有不同长度”的错误。

install.packages("class")
library("class")

mydata <- read.table("http://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data", sep=",", header=FALSE)

index <- 1:nrow(mydata)
testindex <- sample(index, trunc(length(index)/4))
testset <-mydata[testindex,]
trainset <- mydata[-testindex,]

#Iris type classifed by sepal length

kblah <- knn(train = trainset['V1'],  
         test=testset['V1'],     
         cl=as.factor(testset[['V5']]) ,
         k=1, l=0, prob=F, use.all=T)

#Iris type classified by sepal width

kblah2 <- knn(train = trainset['V2'],  
         test=testset['V2'],     
         cl=as.factor(testset[['V5']]) ,
         k=1, l=0, prob=F, use.all=T)

#Iris type classified by petal length

kblah3 <- knn(train = trainset['V3'],  
         test=testset['V3'],     
         cl=as.factor(testset[['V5']]) ,
         k=1, l=0, prob=F, use.all=T)

#Iris type classified by petal width 

kblah4 <- knn(train = trainset['V4'],  
         test=testset['V4'],     
         cl=as.factor(testset[['V5']]) ,
         k=1, l=0, prob=F, use.all=T)

plot(kblah)
plot(kblah2)
plot(kblah3)
plot(kblah4)

table(kblah)
table(kblah2)
table(kblah3)
table(kblah4)

1 个答案:

答案 0 :(得分:0)

错误告诉你究竟出了什么问题。在?knn帮助页面中,它将cl=参数描述为“训练集的真实分类因子”,但您从测试集传递值。那是错的。它应该是cl=as.factor(trainset[['V5']])