0
我正在嘗試使用NaiveBayes與使用e1071軟件包的Iris DataSet進行一些分類,事實證明,我無法在Rpart中進行預測。如何解決這個問題,我可以建立一個基於p4的混淆矩陣,我試圖預測。R中的NaiveBayes無法預測
> 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"
你應該指定你正在使用的軟件包。我假設e1071? – Dason
是的,我正在使用e1071包裝! –