• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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

2016-05-25 14:04  猎手家园  阅读(7154)  评论(0编辑  收藏  举报

案例三比较简单,不需要自己写公式算法,使用了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是一个数字类型的数据框。

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap