R中knn函数中的cl参数是什么意思? [英] What does cl parameter in knn function in R mean?

查看:613
本文介绍了R中knn函数中的cl参数是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用R来实现knn. "class"包中的knn函数包含一个名为cl:

I'm using R to implement knn. The knn function in 'class' package contains a parameter called cl:

knn(train, test, cl, k = 1, l = 0, prob = FALSE, use.all = TRUE)

在包装文档中写道,cl是对训练集进行真正分类的一个因素.我不知道那意味着什么!有人可以帮忙吗?

It is written in the package documentation that cl is a factor of true classifications of training set. I don't know that means! could any one help?

推荐答案

正如您所指出的,CL代表分类. CL应包含属于训练测试的响应变量的类别.如果将X定义为自变量,将Y定义为自变量,然后从两者定义训练和测试集,则knn应称为:

As you point out CL stands for classifications. CL should contain the categories of the response variables belonging to the training test. If one defines X as the independent variables and Y as the independent variable and then defines a training and a test set from the two, than knn should be called as:

train <- sample(1 : dim(data)[1], round(dim(data)[1] / 3 * 2))
trainX <- data[train, ]
testX <- data[-train, ]
trainCl <- factor(data[train, "classifications"])
testCl <- factor(data[-train, "classifications"])
knnPred <- knn(trainX, testX, trainCl, k=1)
# confusion matrix
table(knnPred, testCl)

这篇关于R中knn函数中的cl参数是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆