使用naiveBayes(e1071)进行分类不起作用($ levels返回NULL)

stf*_*tfn 6 r classification machine-learning

我使用naiveBayes(e1071 http://en.wikibooks.org/wiki/Data_Mining_Algorithms_In_R/Classification/Na%C3%AFve_Bayes)对我的数据集进行分类(Classification class:"class"0/1).这是我做的:

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])
Run Code Online (Sandbox Code Playgroud)

分类器不起作用,这是我得到的:

> 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
Run Code Online (Sandbox Code Playgroud)

我试图使用包含数据集(虹膜),一切正常.我的做法有什么问题?

And*_*rej 7

确保将类变量视为因子; 即

nb.classifier <- naiveBayes(as.factor(class) ~ ., data = arrhythmia.training)
Run Code Online (Sandbox Code Playgroud)

顺便说一句,您不需要从预测调用中排除类变量.