Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Classification with naiveBayes (e1071) does not work ($levels returns NULL)

I use naiveBayes (e1071 http://en.wikibooks.org/wiki/Data_Mining_Algorithms_In_R/Classification/Na%C3%AFve_Bayes) for classifying my data set (Classification class: "class" 0/1). Here is what I do:

library(e1071)
arrhythmia <- read.csv(file="/home/.../arrhythmia.csv", head=TRUE, sep=",")

#devide into training and test data 70:30
trainingIndex <- createDataPartition(arrhythmia$class, p=.7, list=F)
arrhythmia.training <- arrhythmia[trainingIndex,]
arrhythmia.testing <- arrhythmia[-trainingIndex,]

nb.classifier <- naiveBayes(class ~ ., data = arrhythmia.training)
predict(nb.classifier,arrhythmia.testing[,-260])

The classifier does not work, here is what I get:

> predict(nb.classifier,arrhythmia.testing[,-260])
factor(0)
Levels: 

> str(arrhythmia.training)
'data.frame':   293 obs. of  260 variables:
 $ age                         : int  75 55 13 40 44 50 62 54 30 46 ...
 $ sex                         : int  0 0 0 1 0 1 0 1 0 1 ...
 $ height                      : int  190 175 169 160 168 167 170 172 170 158 ...
 $ weight                      : int  80 94 51 52 56 67 72 58 73 58 ...
 $ QRSduration                 : int  91 100 100 77 84 89 102 78 91 70 ...
 $ PRinterval                  : int  193 202 167 129 118 130 135 155 180 120 ...
 # and so on (260 attributes)

> str(arrhythmia.training[260])
'data.frame':   293 obs. of  1 variable:
 $ class: int  1 0 1 0 0 1 1 1 1 0 ...


> nb.classifier$levels
NULL

I tried to use the included the data set (iris) and everything works fine. What's wrong with my approach?

like image 999
stfn Avatar asked Oct 11 '12 08:10

stfn


1 Answers

Make sure that you treat class variable as factor; i.e.

nb.classifier <- naiveBayes(as.factor(class) ~ ., data = arrhythmia.training)

By the way, you don't need to exclude class variable from predict call.

like image 151
Andrej Avatar answered Sep 20 '22 22:09

Andrej