使用以下代码获取上述错误:
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;
答案 0 :(得分:1)
如果您需要根据quality
对residual.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
。