KNN in R: 'train and class have different lengths'?

前端 未结 5 1114
清酒与你
清酒与你 2020-12-11 01:49

Here is my code:

train_points <- read.table(\"kaggle_train_points.txt\", sep=\"\\t\")
train_labels <- read.table(\"kaggle_train_labels.txt\", sep=\"\\t         


        
相关标签:
5条回答
  • 2020-12-11 02:13

    I had the same issue in trying to apply knn on breast cancer diagnosis from wisconsin dataset I found that the issue was linked to the fact that cl argument need to be a vector factor (my mistake was to write cl=labels , I thought this was the vector to be predicted it was in fact a data frame of one column ) so the solution was to use the following syntax : knn (train, test,cl=labels$diagnosis,k=21) diagnosis was the header of the one column data frame labels and it worked well Hope this help !

    0 讨论(0)
  • 2020-12-11 02:16

    Try converting the data into a dataframe using as.dataframe(). I was having the same problem & afterwards it worked fine:

    train_pointsdf <- as.data.frame(train_points)
    train_labelsdf <- as.data.frame(train_labels)
    test_pointsdf <- as.data.frame(test_points)
    
    0 讨论(0)
  • 2020-12-11 02:24

    Simply set drop = TRUE while you're excluding cl from dataframe, it causes to remove dimension from an array which have only one level:

    cl = train_labels[,1, drop = TRUE]
    knn(train_points, test_points, cl, k = 5)
    
    0 讨论(0)
  • 2020-12-11 02:27

    Without access to the data, it's really hard to help. However, I suspect that train_labels should be a vector. So try

    cl = train_labels[,1]
    knn(train_points, test_points, cl, k = 5)
    

    Also double check:

    dim(train_points)
    dim(test_points)
    length(cl)
    
    0 讨论(0)
  • 2020-12-11 02:36

    I have recently encountered a very similar issue. I wanted to give only a single column as a predictor. In such cases, selecting a column, you have to remember about drop argument and set it to FALSE. The knn() function accepts only matrices or data frames as train and test arguments. Not vectors.

    knn(train = trainSet[, 2, drop = FALSE], test = testSet[, 2, drop = FALSE], cl = trainSet$Direction, k = 5)

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