R's caret training errors when y is not a factor

前端 未结 1 1757
滥情空心
滥情空心 2021-01-21 17:44

I am using R-studio and am using kaggle\'s forest cover data and keep getting an error when trying to use the knn3 function in caret. here is my code:

library(c         


        
相关标签:
1条回答
  • 2021-01-21 18:20

    As the error message states, y must be a factor (here, y is the name of the second parameter to the function). In R, a factor variable is used to represent categorical data. You can turn y into a factor with factor(y) but it will just have the levels 1:7 for your data. If you want to give more meaningful values to your factor, try

    train$Cover_Type <- factor(train$Cover_Type, levels=1:7, 
        labels=c("Spruce/Fir","Lodgepole Pine","Ponderosa Pine",
        "Cottonwood/Willow","Aspen",
        "Douglas-fir","Krummholz"))
    

    That will make your function happier and give you more useful labels in the results

    0 讨论(0)
提交回复
热议问题