Caret and KNN in R: predict function gives error

后端 未结 1 897
攒了一身酷
攒了一身酷 2021-01-07 13:47

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:



        
相关标签:
1条回答
  • 2021-01-07 14:18

    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"))
    
    0 讨论(0)
提交回复
热议问题