I have been trying to use the KNN function to start my predictions, however when i run the code it throws the error:
Error in knn(data.frame(tr5_train), data.frame(tr5_test), cl = pred_train_labels, : 'train' and 'class' have different lengths
I have checked that all data sets are data.frame and tried to use the label as a vector with no success
The following is the code I've used:
test_tr5_no_target<- test_tr5[-2]
tr5_train<- test_tr5_no_target[1:74475, , drop = FALSE]
tr5_test<- test_tr5_no_target[74476:93094, , drop = FALSE]
pred_train_labels<- test_tr5[1:74475, 2]
pred_test_labels<- test_tr5[74476:93094, 2]
#install.packages("class")
library(class)
##ensure all data is a dataframe
as.data.frame(tr5_train)
as.data.frame(tr5_test)
as.data.frame(pred_train_labels)
pred1<- knn(data.frame(tr5_train), data.frame(tr5_test), cl = pred_train_labels, k = 5)
Keep in mind for the labels column 2 is the numeric Target feature. I have researched all over and have not been able to find what is throwing this error, is there anything that i may be doing incorrectly?
Thanks for all the help, really appreciate it! (Unfortunately i can't share the data itself as it is restricted)
-Jose C.