数据分析与挖掘 - R语言:贝叶斯分类算法(案例三)
2016-05-25 14:04 猎手家园 阅读(7746) 评论(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是一个数字类型的数据框。
欢迎关注我的公众号:云栖语,不一样的研发视界。

云栖语微信公众号:change-1978
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术