R中的NaiveBayes无法预测

时间:2017-02-07 19:04:39

标签: r classification naivebayes

我正在尝试使用带有e1071软件包的NarisBayes和Iris DataSet进行一些分类,事实证明我无法像Rpart一样进行预测。如何解决这个问题,以便我可以根据我想要预测的p4建立一个混淆矩阵。

> table(iris$Species) 

    setosa versicolor  virginica 
        50         50         50 
> head(iris)
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2  setosa
2          4.9         3.0          1.4         0.2  setosa
3          4.7         3.2          1.3         0.2  setosa
4          4.6         3.1          1.5         0.2  setosa
5          5.0         3.6          1.4         0.2  setosa
6          5.4         3.9          1.7         0.4  setosa
> set.seed(9850) 
> g<-runif(nrow(iris))
> irisr <- iris[order(g),] 
> m4 <- naiveBayes(Species~., data=irisr[1:100,], method="class")
> m4

Naive Bayes Classifier for Discrete Predictors

Call:
naiveBayes.default(x = X, y = Y, laplace = laplace, method = "class")

A-priori probabilities:
Y
    setosa versicolor  virginica 
      0.34       0.35       0.31 

Conditional probabilities:
            Sepal.Length
Y                [,1]      [,2]
  setosa     5.000000 0.3797926
  versicolor 5.857143 0.4545752
  virginica  6.658065 0.7017949

            Sepal.Width
Y                [,1]      [,2]
  setosa     3.461765 0.4221264
  versicolor 2.717143 0.3212894
  virginica  2.990323 0.3543584

            Petal.Length
Y                [,1]      [,2]
  setosa     1.450000 0.1619016
  versicolor 4.197143 0.4273624
  virginica  5.600000 0.6066300

            Petal.Width
Y                 [,1]      [,2]
  setosa     0.2558824 0.1159707
  versicolor 1.2800000 0.1827889
  virginica  2.0000000 0.2708013
> summary (m4)
        Length Class  Mode     
apriori 3      table  numeric  
tables  4      -none- list     
levels  3      -none- character
call    5      -none- call     
> p4<- predict(m4, irisr[101:150,], type="class")
#Gives me this error ! How can I solve this ?
Error in UseMethod("predict") : 
  no applicable method for 'predict' applied to an object of class "naiveBayes"

1 个答案:

答案 0 :(得分:0)

我找到了答案!

> m4 <- naiveBayes(as.factor(Species)~., data=irisr[1:100,], method="class")
> p4<-predict(m4, irisr[101:150,]) 
> p4
 [1] virginica  setosa     versicolor virginica  versicolor
 [6] setosa     setosa     versicolor versicolor versicolor
[11] versicolor versicolor virginica  setosa     versicolor
[16] virginica  virginica  virginica  versicolor virginica 
[21] setosa     virginica  virginica  setosa     virginica 
[26] setosa     setosa     versicolor setosa     versicolor
[31] setosa     virginica  virginica  virginica  setosa    
[36] virginica  versicolor virginica  setosa     setosa    
[41] virginica  setosa     virginica  virginica  virginica 
[46] setosa     virginica  virginica  virginica  setosa    
Levels: setosa versicolor virginica
> table(irisr[101:150,5], predicted=p4)
            predicted
             setosa versicolor virginica
  setosa         16          0         0
  versicolor      0         11         4
  virginica       0          1        18