' knn中的错误(train = trainset,test = testset,cl,k = 1,l = 0,prob = FALSE,:' train'和' class'有不同长度

时间:2015-01-25 14:18:32

标签: r knn

使用以下代码获取上述错误:

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

mydata <- read.table("http://archive.ics.uci.edu/ml/machine-learning-databases/wine-quality/winequality-red.csv", sep=";", header=TRUE);

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


cl <- factor(c(rep("quality",3),rep("residual.sugar",3)))
knn(train = trainset, test = testset, cl, k = 1, l = 0, prob = FALSE, use.all = TRUE)

请指教。随时改变我设置的方式&#39; cl&#39;。老实说,我不知道我在做什么。我试图将质量归类为&#39;基于&#39; residual.sugar&#39;

1 个答案:

答案 0 :(得分:1)

如果您需要根据qualityresidual.sugar进行分类,那么quality就是您的cl参数。这也写在文档中:

  

cl:训练集的真实分类因子

因此,为了运行您的knn模型,您需要执行以下操作:

library("class")

mydata <- read.table("http://archive.ics.uci.edu/ml/machine-learning-databases/wine-quality/winequality-red.csv", sep=";", header=TRUE);

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

knn(train = trainset['residual.sugar'],   #you only need residual.sugar you said so just use that
    test=testset['residual.sugar'],       #again test is the residual.sugar
    cl=as.factor(trainset[['quality']]) , #your cl argument is quality
    k=1, l=0, prob=F, use.all=T)

之前根本没有定义cl