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


案例三比較簡單,不需要自己寫公式算法,使用了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是一個數字類型的數據框。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM