[英]I'm working with R. I am performing a k-NN analysis on the training data, but I keep getting an error on my knn function. How can I fix this
Here's my input: 这是我的输入:
For (j in I: m ){
model.knn <- knn(train.set[,vars],
test.set[,vars],
cl = class.train,
k=j,
prob = T)
error <- table(model.knn, class.test)
knn.error[j] <- (error[1,2] +
error [2,1]/sum(error))
}
Output: 输出:
Error in [.data.frame'(train.set, , vars) : undefined columns selected
Based on the error you are getting, it seems that you are trying to subset data frame train.set
by selecting columns which do not exist in that data frame. 根据收到的错误,您似乎正在尝试通过选择该数据帧中不存在的列来对数据帧
train.set
进行子集化。 To rectify this, try the following code: 要解决此问题,请尝试以下代码:
> colnames(train.set) # lists all column names in train.set
> vars # prints all columns you are trying to select
You need to make certain that train.set
has the columns which vars
is trying to reference. 您需要确定
train.set
包含vars
试图引用的列。 And while you are at it, you should do a similar check for the test.set
data frame. 并且在使用它时,应该对
test.set
数据帧进行类似的检查。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.