朴素贝叶斯
1联合概率分布
p(x,y)=p(y)P(x|y)  或者p(A交B)=p(A)xp(B)  p(A交B)不容易求,假设条件独立拆分成两个事件的乘积
2基本假设条件独立性
3利用贝叶斯定理 p(y|x)=P(x,y)/p(x)=p(y)P(x|y)/sum(y-i)[p(y)P(x|y)]
y=max p(y)P(x|y)
贝叶斯决策理论要求计算两个概率p1(x,y),p2(x, y):
如果p1(x,y) > p2 (x, y) , 那么属于类别1
如果p2(x, y) > pl(x, y) , 那么属于类别2
拉普拉斯估计--防止概率值为0
每一个似然函数 分子+1对分母加上分子中加上1的总数
在朴素贝叶斯使用数值特征采用数值特征离散化,找见数据分布分割点切分
1分割词-去大小写-去字母?-去停用词-去多余空格符合
2统计每个词在每条短信中出现的频率
3创建频率矩阵 行是短信中词是否出现出现可以是yes no  列是每个词  label可以是字符串
library(e1071)
sms_classifier <- naiveBayes(sms_train, sms_train_labels)
sms_test_pred <- predict(sms_classifier, sms_test)
例子 iris[,-5]不含第5项
data(iris)
m <- naiveBayes(iris[,-5], iris[,5])
m
table(predict(m, iris), iris[,5])

另一个朴素贝叶斯包 klaR  NaiveBayes()

朴素贝叶斯分类器通常有两种实现方式:一种基于贝努利模型实现, 一种基于多项式模型实现
这里采用前一种实现方式。该实现方式中并不考虑词在文档中出现的次数, 只考虑出不出现, 
因此在这个意义上相当于假设词是等权重的

Python版实现
http://blog.csdn.net/q383700092/article/details/51773364
R语言版调用函数
http://blog.csdn.net/q383700092/article/details/51774069
MapReduce简化实现版
http://blog.csdn.net/q383700092/article/details/51778765
spark版
后续添加

垃圾短信识别

# read the sms data into the sms data frame
sms_raw <- read.csv("sms_spam.csv", stringsAsFactors = FALSE)# examine the structure of the sms data 垃圾邮件标记为spam非垃圾ham 结构type+text
str(sms_raw)# convert spam/ham to factor.字符串分类标签转换成因子比较好
sms_raw$type <- factor(sms_raw$type)# examine the type variable more carefully
str(sms_raw$type)
table(sms_raw$type)# build a corpus using the text mining (tm) package
#tm文本挖掘包
library(tm)
sms_corpus <- VCorpus(VectorSource(sms_raw$text))#创建语料库 VCorpus存储R文本文档# examine the sms corpus
print(sms_corpus)
inspect(sms_corpus[1:2]) #查看1-2个语料库内容as.character(sms_corpus[[1]])
lapply(sms_corpus[1:2], as.character)# clean up the corpus using tm_map()字母转换成小写
#sms_corpus_clean <- tm_map(sms_corpus, content_transformer(tolower))
# show the difference between sms_corpus and corpus_clean
#as.character(sms_corpus[[1]])
#as.character(sms_corpus_clean[[1]])sms_corpus_clean <- tm_map(sms_corpus, removeNumbers) # remove numbers去掉数字
sms_corpus_clean <- tm_map(sms_corpus_clean, content_transformer(tolower)) #字母转换成小写
sms_corpus_clean <- tm_map(sms_corpus_clean, removeWords, stopwords()) # remove stop words去掉停用词
sms_corpus_clean <- tm_map(sms_corpus_clean, removePunctuation) # remove punctuation去掉标点# tip: create a custom function to replace (rather than remove) punctuation
removePunctuation("hello...world")
replacePunctuation <- function(x) { gsub("[[:punct:]]+", " ", x) }
replacePunctuation("hello...world")# illustration of word stemming
library(SnowballC)
wordStem(c("learn", "learned", "learning", "learns"))sms_corpus_clean <- tm_map(sms_corpus_clean, stemDocument)sms_corpus_clean <- tm_map(sms_corpus_clean, stripWhitespace) # eliminate unneeded whitespace去掉多余的空格# examine the final clean corpus
lapply(sms_corpus[1:3], as.character)
lapply(sms_corpus_clean[1:3], as.character)# create a document-term sparse matrix创建一个稀疏矩阵
sms_dtm <- DocumentTermMatrix(sms_corpus_clean)# alternative solution: create a document-term sparse matrix directly from the SMS corpus
#直接从语料库创建一个稀疏矩阵
sms_dtm2 <- DocumentTermMatrix(sms_corpus, control = list(tolower = TRUE,removeNumbers = TRUE,stopwords = TRUE,removePunctuation = TRUE,stemming = TRUE
))# alternative solution: using custom stop words function ensures identical result
# 使用自定义的停用词
sms_dtm3 <- DocumentTermMatrix(sms_corpus, control = list(tolower = TRUE,removeNumbers = TRUE,stopwords = function(x) { removeWords(x, stopwords()) },removePunctuation = TRUE,stemming = TRUE
))# compare the result
sms_dtm
sms_dtm2
sms_dtm3# creating training and test datasets
sms_dtm_train <- sms_dtm[1:4169, ]
sms_dtm_test  <- sms_dtm[4170:5559, ]# also save the labels
sms_train_labels <- sms_raw[1:4169, ]$type
sms_test_labels  <- sms_raw[4170:5559, ]$type# check that the proportion of spam is similar查看类别比例
prop.table(table(sms_train_labels))
prop.table(table(sms_test_labels))# word cloud visualization词云可视化
library(wordcloud)
#从语料库直接创建词云
wordcloud(sms_corpus_clean, min.freq = 50, random.order = FALSE)# subset the training data into spam and ham groups
spam <- subset(sms_raw, type == "spam")
ham  <- subset(sms_raw, type == "ham")wordcloud(spam$text, max.words = 40, scale = c(3, 0.5))
wordcloud(ham$text, max.words = 40, scale = c(3, 0.5))sms_dtm_freq_train <- removeSparseTerms(sms_dtm_train, 0.999)
sms_dtm_freq_train# indicator features for frequent words找出不少于5条短信的单词--减少特征
findFreqTerms(sms_dtm_train, 5)# save frequently-appearing terms to a character vector
sms_freq_words <- findFreqTerms(sms_dtm_train, 5)
str(sms_freq_words)# create DTMs with only the frequent terms
sms_dtm_freq_train <- sms_dtm_train[ , sms_freq_words]
sms_dtm_freq_test <- sms_dtm_test[ , sms_freq_words]# convert counts to a factor转为成yes no
convert_counts <- function(x) {x <- ifelse(x > 0, "Yes", "No")
}# apply() convert_counts() to columns of train/test data MARGIN = 2 2是列1是行
sms_train <- apply(sms_dtm_freq_train, MARGIN = 2, convert_counts)
sms_test  <- apply(sms_dtm_freq_test, MARGIN = 2, convert_counts)## Step 3: Training a model on the data ----训练
library(e1071)
sms_classifier <- naiveBayes(sms_train, sms_train_labels)## Step 4: Evaluating model performance ----预测
sms_test_pred <- predict(sms_classifier, sms_test)
#----评估
library(gmodels)
CrossTable(sms_test_pred, sms_test_labels,prop.chisq = FALSE, prop.t = FALSE, prop.r = FALSE,dnn = c('predicted', 'actual'))## Step 5: Improving model performance ----加入拉普拉斯估计laplace = 1
sms_classifier2 <- naiveBayes(sms_train, sms_train_labels, laplace = 1)
sms_test_pred2 <- predict(sms_classifier2, sms_test)
CrossTable(sms_test_pred2, sms_test_labels,prop.chisq = FALSE, prop.t = FALSE, prop.r = FALSE,dnn = c('predicted', 'actual'))

机器学习与R之朴素贝叶斯分类器相关推荐

  1. 【一起入门MachineLearning】中科院机器学习第3课-朴素贝叶斯分类器

    专栏介绍:本栏目为 "2021秋季中国科学院大学周晓飞老师的机器学习" 课程记录,不仅仅是课程笔记噢- 每周上两小节课,每周更新两篇博客,如果感兴趣的话,就和我一起入门Machin ...

  2. 机器学习实验 - 朴素贝叶斯分类器

    目录 一.报告摘要 1.1 实验要求 1.2 实验思路 1.3 实验结论 二.实验内容 2.1 方法介绍 2.2 实验细节 2.2.1 实验环境 2.2.2 实验过程 2.2.3 实验与理论内容的不同 ...

  3. 机器学习实战 朴素贝叶斯分类器

    基于概率论的分类方法: 朴素贝叶斯 我的微信公众号: s406205391; 欢迎大家一起学习,一起进步!!! k-近邻算法和决策树会给出"该数据属于哪一类"的明确回答.不过,分类 ...

  4. 机器学习算法 - 朴素贝叶斯分类器

    ​ 一.算法简介 1.1 背景 监督学习分为生成模型 (generative model) 与判别模型 (discriminative model) 判别模型:SVM, LR, KNN, NN, CR ...

  5. 机器学习(3)高斯判别分析朴素贝叶斯分类器

    判别模型与生成模型 判别模型 判别模型是对观测数据进行直接分类,常见的判别模型有逻辑回归和感知机算法等.此模型仅对数据进行分类,并不能具象化或者量化数据本身的分布状态,因此也无法根据分类生成可观测的图 ...

  6. 机器学习:朴素贝叶斯分类器,决策函数向量化处理,mask使用技巧

    文章目录 前面实现的朴素贝叶斯分类器,决策函数是非向量化的: 借助于numpy向量化处理,相当于并行计算,注意mask使用技巧,用途较广: 前面实现的朴素贝叶斯分类器,决策函数是非向量化的: 前面提到 ...

  7. sklearn朴素贝叶斯分类器_python机器学习:方法链和朴素贝叶斯分类器

    今天我们在学习朴素贝叶斯分类器之前,我们先来总结下前面经常用到的内容,方法链:在scikit-learn中所有模型的fit方法返回的都是self.我们用一行代码初始化模型并拟合,对应代码如下:logr ...

  8. python机器学习案例系列教程——文档分类器,朴素贝叶斯分类器,费舍尔分类器

    全栈工程师开发手册 (作者:栾鹏) python数据挖掘系列教程 github地址:https://github.com/626626cdllp/data-mining/tree/master/Bay ...

  9. 《机器学习实战》-04 朴素贝叶斯

    说明: 作业的所有代码都要基于Python3 学习大纲:https://blog.csdn.net/qq_34243930/article/details/84669684 (所有计划均在学习大纲里) ...

最新文章

  1. Linux的19 个装B的命令,记得搂一遍!!!
  2. Grails示例程序-导出Excel文档
  3. LF 和 CRLF 区别
  4. U-Mail邮件系统客户无需担心OpenSSL心脏出血漏洞
  5. 数学--数论--HDU-2698 Maximum Multiple(规律)
  6. 大型网站系统与Java中间件实践pdf
  7. 鼠标移动到ul图片会摆动_我们可以从摆动时序分析中学到的三件事
  8. 前端学习(1994)vue之电商管理系统电商系统之通过编程导航跳转到商品导航界面
  9. Hollis原创|深入分析Java的编译原理
  10. 论述计算机硬件结构的理解论文,论述对汇编语言教学内容和方法及特点的认识与思考...
  11. Luogu P1041 [2003NOIP提高组]传染病控制
  12. Atitit 上传进度的实现与原理 目录 1.1. 前端 1 1.2. 读取进度 1 1.3. 后端 定时注入进度 1 1.1.前端                         wind
  13. NATS 分布式消息队列系统
  14. linux下的lib文件
  15. error C2664: 'atof' : cannot convert parameter 1 from 'int' to 'const char *'
  16. 多路径路由算法选择(2)——回顾传统的动态路由协议(RIP、OSPF、BGP、IGRP、EIGRP、IS-IS)
  17. 生产者消费者问题——管程法
  18. IDEA+Java+SSM+Mysql+JSP实现Web宠物商城系统
  19. Openstack基础架构
  20. 先验概率与后验概率是什么

热门文章

  1. linux下下载基因组程序,从 NCBI 批量下载基因组的方法
  2. Coursera-Neural Networks by Geoffrey Hinton
  3. android自定义键盘遮挡,android中键盘遮挡了dialog里的内容怎么处理
  4. 阿里云后台测试短信模板
  5. 如何做好用户故事地图?
  6. @Column注解解析
  7. 未来想象计算机图片儿童版,未来世界儿童画画大全绘画作品欣赏
  8. 五面阿里拿下飞猪事业部offer,面试题附答案
  9. CSS图片底部默认边距三种解决办法
  10. 国内计算机视觉与机器学习研究团队