0

I am trying to perform this loop in RStudio but get the error message:

"Error in knn(train = Glass.trainSet, test = Glass.testSet, cl = Glass.trainLabels,  : 
  'train' and 'class' have different lengths"

I'm wondering if there is anyone that knows what the problem is? Before the loop I have implemented the libs, class, packages and normalization of the function.

 count = 0 

 kNr <- c (3 , 5 , 7)
 distrTrainingVStest <- 
 list(
  first = c(0.70 , 0.30),
 second = c(0.75 , 0.25),
third = c(0.50 , 0.50)
 )
for (i in 1:3)
{
for (k in 1:3)
{
for (m in 1:3)
 {

#set.seed(1234)
  count <- count + 1
  print(paste("--- loop", count, "---"))
  print(paste("k =", kNr [i])) #var nionde loop så ändras k från 3->5->7
  
  if (m == 1) {
    print("Datan är inte normaliserad")
  } else if (m == 2) {
    Glass <- as.data.frame(lapply(Glass2[1:9], normalize))
    print("Datan här är normaliserad [0,1]")
  } else if (m == 3) {
    Glass <- as.data.frame(lapply(Glass2[1:9], standarlize))
    print("Datan här är normaliserad [-1,1]")
  }
  
  print(paste(
    "Train =",
    distrTrainingVStest[[k]][1],
    "Test =",
    distrTrainingVStest[[k]][2]
  ))
  cat(sep = "\n")
  
  idx <-
    sample(
      2,
      nrow(Glass),
      replace = TRUE,
      prob = c(distrTrainingVStest[[k]][1], distrTrainingVStest[[k]][2])
    )
  
  Glass.trainSet <- Glass[idx == 1, 1:9] #tränar systemet
  Glass.testSet <- Glass[idx == 2, 1:9] #används för att utvärdera det tränade systemet
  Glass.trainLabels <- Glass[idx == 1, 10]
  Glass.testLabels <- Glass[idx == 2, 10]
  
  Glass_pred <-
    knn(
      train = Glass.trainSet,
      test = Glass.testSet,
      cl = Glass.trainLabels,
      k = kNr[i]
    )
  
  confusionTable <- print(table(Glass_pred, Glass.testLabels))
  cat(sep = "\n")
  
   accuracy <- (sum(diag(confusionTable)) / sum(confusionTable))
   print(paste("Noggrannhet: ", accuracy))
   cat(sep = "\n")
  
    }
  }
 }
jake2389
  • 1,166
  • 8
  • 22
Anton
  • 1
  • 1
  • Does this answer your question? [KNN in R: 'train and class have different lengths'?](https://stackoverflow.com/questions/16276388/knn-in-r-train-and-class-have-different-lengths) – Phil Nov 29 '20 at 19:30
  • I have seen this one before but I cant get it to work I still get the same error – Anton Nov 29 '20 at 19:39

0 Answers0