Reputation: 3
I have a dataset of 5000 points and 12 attributes(out of which is class variable)..I divided data in training(3000 points) and testing(2000 points) and the performed the classification on training data and wnat to check the error rate using accuracy metric but unfortunately an error is being thrown can you please help me out..
b=as.factor(test_data$Personal.Loan)
model_naivebayes = naiveBayes(Personal.Loan ~.,data=train_data);
naive_predict = predict(model_naivebayes, test_data);
table(naive_predict,b)
Error: Error in table(naive_predict, b) : all arguments must have the same length
when I checked the contents in naive_predict it say Factor W/ '0' evels
Regards, Sri.
Upvotes: 1
Views: 2121
Reputation: 733
I had similar issue and resolved it by this way. I will show it with the iris
data:
This code will give the error message:
iris[ , 5] <- as.character(iris[ , 5] )
classifier<-naiveBayes(iris[,1:4], iris[,5])
table(predict(classifier, iris[,-5]), iris[,5])
If you use factor, it would not:
iris[ , 5] <- as.character(iris[ , 5] )
classifier<-naiveBayes(iris[,1:4], factor(iris[,5]) )
table(predict(classifier, iris[,-5]), factor(iris[,5]))
Upvotes: 1
Reputation: 4179
Looks like the error is on the 3rd line. You need to exclude your class variables when predicting.
naive_predict = predict(model_naivebayes, test_data[,-which(names(predictors) %in% c("Personal.Loans"))];
Upvotes: 0