I am trying to do naive bayes classification in R. I have seen this example in following link.
http://en.wikibooks.org/wiki/Data_Mining_Algorithms_In_R/Classification/Na%C3%AFve_Bayes
Only 2 lines are there. First classify and then predict.
> classifier<-naiveBayes(iris[,1:4], iris[,5])
> table(predict(classifier, iris[,-5]), iris[,5])
This same code on “iris dataset” working fine. But when i applied the same on my dataset, I am getting some errors.
My dataset contains 4 attributes and 4th attribute the class attribute.
> str(data1)
'data.frame': 1370 obs. of 4 variables:
$ TenScore : num 85 84.2 67.2 91.5 79.3 ...
$ TwelthScore : num 69 87.9 67.5 82.7 72.4 ...
$ GDegreeScore : num 63.3 70.7 61.3 78.2 62.1 ...
$ Got_Admission: chr "No" "No" "No" "No" ...
So, I tried this.
> classifier<-naiveBayes(data1[,1:3], data1[,4])
> table(predict(classifier, data1[,-4]), data1[,4])
Error in table(predict(classifier, data1[, -4]), data1[, 4]) :
all arguments must have the same length
I am getting above error when I am executing the command. When I just use predict, its giving me following output.
> predict(classifier, data1[,-4])
factor(0)
Levels:
str(data1) 'data.frame': 1370 obs. of 4 variables:
$ TenScore : num 85 84.2 67.2 91.5 79.3 ...
$ TwelthScore : num 69 87.9 67.5 82.7 72.4 ...
$ GDegreeScore : num 63.3 70.7 61.3 78.2 62.1 ...
$ Got_Admission: chr "No" "No" "No" "No" ...
Please explain me whats the errors about and how to solve?
I can produce the same error by changing the 5th column of iris to character:
So you should probably do this:
If your ‘Got_Admission’ column is not in good order you will get confusing results (the GIGO effect). You should first look at the contents with: