R中的Caret和KNN:预测函数给出错误 [英] Caret and KNN in R: predict function gives error
问题描述
我尝试使用R中的插入符号包通过简化的KNN模型进行预测.即使在这里非常简单的可重现示例中,它总是会产生相同的错误:
I try to predict with a simplified KNN model using the caret package in R. It always gives the same error, even in the very simple reproducible example here:
library(caret)
set.seed(1)
#generate training dataset "a"
n = 10000
a = matrix(rnorm(n*8,sd=1000000),nrow = n)
y = round(runif(n))
a = cbind(y,a)
a = as.data.frame(a)
a[,1] = as.factor(a[,1])
colnames(a) = c("y",paste0("V",1:8))
#estimate simple KNN model
ctrl <- trainControl(method="none",repeats = 1)
knnFit <- train(y ~ ., data = a, method = "knn", trControl = ctrl, preProcess = c("center","scale"), tuneGrid = data.frame(k = 10))
#predict on the training dataset (=useless, but should work)
knnPredict <- predict(knnFit,newdata = a, type="prob")
这给
[.data.frame
中的错误(out,,obsLevels,drop = FALSE):
未定义的列已选择
Error in [.data.frame
(out, , obsLevels, drop = FALSE) :
undefined columns selected
在没有目标变量y的情况下定义更现实的测试数据集"b".
Defining a more realistic test dataset "b" without the target variable y...
#generate test dataset
b = matrix(rnorm(n*8,sd=1000000),nrow = n)
b = as.data.frame(b)
colnames(b) = c(paste0("V",1:8))
#predict on the test datase
knnPredict <- predict(knnFit,newdata = b, type="prob")
给出相同的错误
[.data.frame
中的错误(out,,obsLevels,drop = FALSE):
未定义的列已选择
Error in [.data.frame
(out, , obsLevels, drop = FALSE) :
undefined columns selected
我知道这些列很重要,但是在这里它们是相同的.这是怎么了谢谢!
I know that the columnames are important, but here they are identical. What is wrong here? Thanks!
推荐答案
问题是您的y变量.当您要求班级概率时,训练和/或预测函数会将其放入每个班级都有一列的数据框中.如果因子级别不是有效的变量名称,则会自动更改它们(例如,"0"变为"X0").另请参见此帖子.
The problem is your y variable. When you are asking for the class probabilities, the train and / or the predict function puts them into a data frame with a column for each class. If the factor levels are not valid variable names, they are automatically changed (e.g. "0" becomes "X0"). See also this post.
如果您在代码中更改此行,则该行应该有效:
If you change this line in your code it should work:
a[,1] = factor(a[,1], labels = c("no", "yes"))
这篇关于R中的Caret和KNN:预测函数给出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!