简体   繁体   中英

Error during naive bayes classifier

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.

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]))

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"))];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM