简体   繁体   English

我正在与R合作。我正在对训练数据进行k-NN分析,但是我的knn函数始终出现错误。 我怎样才能解决这个问题

[英]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.

相关问题 如何执行引导程序以找到 R 中 k-nn 模型的置信区间? - How can I perform bootstrap to find the confidence interval for a k-nn model in R? 如何在r中不使用k-nn函数实现Knn算法? - How to implement Knn-algorithm without using k-nn function in r? 使用hcv函数在R错误中进行交叉验证。 我该如何解决错误? - Cross-Validation in R error with hcv function. How can I fix the error? 我在 R 中有一个因子列。 我正在尝试删除它,但我不断收到错误消息。 如何删除 R 中的因子列? - I have a factor columns in R. I am trying to delete it but I keep getting errors messages. How do I delete a factor column in R? 我希望在 r 中向我的 ggmap 添加比例尺,但不断收到“转换应该是逻辑的”错误。 我该如何解决这个问题? - I am looking to add a scalebar to my ggmap in r, but keep getting “transform should be logical” error. How do iI fix this? 我在R中使用朴素贝叶斯(Naive Bayes)。 - I am using Naive Bayes in R. I have the titanic set, but predict() function produces error 如何解决我使用 inner_join function 时遇到的这个错误? - How can I fix this error that I'm getting using the inner_join function? 我是 r 的新手。 如何将数据框变量值从数字转换为名称? 请参阅下面的代码 - I am new to r. How can I convert a data frame variable value from a number to a name? See code below 如何将此表变成数据框? 我正在使用 R。 试图得到它的方差分析表 - How do I make this table into a data frame? I'm using R. Trying to get the ANOVA table for it 为随机森林树训练数据时出错 - 我该如何解决? - Getting an error while training data for random forest tree- how can I fix?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM