I want to try KNN algorithm on a dataset using R.I was able to perform KNN , but I want to check the accuracy of the model.
I tried to do the same with the help of confusionMatrix
function,but it shows the following error.
Error in confusionMatrix.default(k, knn_test$driver_tset):
the data cannot have more levels than the reference
The code is given below.
knn_data <- read.csv("Path/Data_without_Missing_values.csv")
knn_test<-read.csv("Path/Test.csv")
cl <- factor(knn_data[,4])
library('class') # required for knn()
k <- knn(knn_data[,c(1,2,3)], knn_test[,c(1,2,3)], cl)
library('caret') # required for confusionMatrix()
confusionMatrix(k,knn_test$driver_tset)
driver_tset
is the column which I have to test. When I checked the levels of k
and knn_test$driver_tset
,it displays the following:
levels(k)
# [1] "7" "8" "9" "10" "11" "12" "13" "14" "17"
levels(knn_test$driver_tset)
# NULL`
So its clear that levels are different, but I'm not getting the significance of levels here. Also,how do I fix this?