資料分析與挖掘 - R語言:貝葉斯分類演算法(案例三)

獵手家園發表於2016-05-25

案例三比較簡單,不需要自己寫公式演算法,使用了R自帶的naiveBayes函式。

 

程式碼如下:

> library(e1071)
> classifier<-naiveBayes(iris[,1:4], iris[,5]) #或寫成下面形式,都可以。 > classifier<- naiveBayes(Species ~ ., data = iris) #其中Species是類別變數 #預測 > predict(classifier, iris[1, -5])

預測結果為:

[1] setosa
Levels: setosa versicolor virginica

和原資料一樣!

 

*********************************這裡是分割線**************************************

我們再拿這個方法來預測一下案例一中的樣本。

#樣本資料集:
mydata <- matrix(c("sunny","hot","high","weak","no",  
                 "sunny","hot","high","strong","no",  
                 "overcast","hot","high","weak","yes",  
                 "rain","mild","high","weak","yes",  
                 "rain","cool","normal","weak","yes",  
                 "rain","cool","normal","strong","no",  
                 "overcast","cool","normal","strong","yes",  
                 "sunny","mild","high","weak","no",  
                 "sunny","cool","normal","weak","yes",  
                 "rain","mild","normal","weak","yes",  
                 "sunny","mild","normal","strong","yes",  
                 "overcast","mild","high","strong","yes",  
                 "overcast","hot","normal","weak","yes",  
                 "rain","mild","high","strong","no"), byrow = TRUE, nrow=14, ncol=5)

#添加列名:
colnames(mydata) <-  c("outlook","temperature","humidity","wind","playtennis")

#貝葉斯演算法:
m<-naiveBayes(mydata[,1:4], mydata[,5]) 
#或使用下面的方法
m<- naiveBayes(playtennis ~ ., data = mydata)    
#報錯:Error in sum(x) : invalid 'type' (character) of argument 無效的型別,只能是數字? #建立預測資料集: new_data = data.frame(outlook="rain", temperature="cool", humidity="normal", wind="strong", playtennis="so") #預測: predict(m, new_data)

在使用naiveBayes函式時報錯:Error in sum(x) : invalid 'type' (character) of argument

我們看一下官方文件,對data有這樣一句描述:

data  Either a data frame of predictors (categorical and/or numeric) or a contingency table.

data是一個數字型別的資料框。

 

相關文章