日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

数据分析与挖掘 - R语言:贝叶斯分类算法(案例三)

發布時間:2025/7/14 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 数据分析与挖掘 - 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是一個數字類型的數據框。

?

總結

以上是生活随笔為你收集整理的数据分析与挖掘 - R语言:贝叶斯分类算法(案例三)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。