bays朴素贝叶斯文本挖掘Chinese

简介: bays朴素贝叶斯文本挖掘Chinese

1 数据准备

##read and clean
sms1<-readLines("sms_labelled.txt",encoding="UTF-8")

2 数据预览

num<-nchar(sms1)
type<-substr(sms1,1,1)#the first character is email type
text<-substr(sms1,3,max(num))#the second character is blank
smsd<-data.frame(type,text,num,stringsAsFactors = F) #Record the number of characters per line

在这里插入图片描述
发现数据第一个字符是短信类型,第二个字符是空格,于是对我们求的y和文本x信息进行提取,num是查看每行数据有多少个字符

index0<-order(num,decreasing = T)#Returns a subscript index sorted by num 

排序后返会的是下标

3 数据清洗

#tapply  The data is grouped and calculated according to the second parameter

index0<-tapply(index0,type[index0],function(x) x[1:1000])#select the first 1000 items by group 

index0<-c(index0$`0`,index0$`1`)#Group by 0 and 1 to form a subscript vector

text0<-text[index0] #Text sorted by  the first two thousand characters

type0<-type[index0] #Type sorted by  the first two thousand characters

class<-factor(type0,labels=c("ham","spam")) # 0 and 1 forward message types

由于原始文本行数太多,这里分别提取了垃圾和非垃圾邮件的前1000条数据

##clean and segment words
text1<-gsub("[^\u4E00-\u9FA5]"," ",text0) #Clean non Chinese characters

去除其中的非中文字符

library(jiebaR)
library(tm)
##self build stopwords
work=worker(bylines = T,stop_word = "stop.txt") #Stop word text
text2<-segment(text1,work) # segment text
text2<-lapply(text2,function(x) x[nchar(x)>1]) #Filter text with more than 1 characters

加载包,依靠停用词列表删除多余的停用词

在这里插入图片描述

textC<-tm_map(textC,function(x) removeWords(x,"我们"))#remove "我们"
inspect(textC)

4 设置测试集和训练集

##split
set.seed(12)
index1<-sample(1:length(class),length(class)*0.9) #Divide test set and training set
text_tr<-textC[index1]
text_te<-textC[-index1]
inspect(text_tr)

5 词云预览

##wordcloud
library(wordcloud)
index2<-class[index1]=="spam" #select spam
pal<-brewer.pal(6,"Dark2") # wordcloud color
par(family='STKaiti')
wordcloud(text_tr,max.words=40,random.order=F,scale=c(4,0.5),colors=pal)
wordcloud(text_tr,max.words =150, scale=c(4,0.5), 
          random.order=F, colors = pal,family="STKaiti")
wordcloud(text_tr[index2],max.words =150, scale=c(4,0.5), 
          random.order=F, colors = pal,family="STKaiti")
wordcloud(text_tr[!index2],max.words =150, scale=c(4,0.5), 
          random.order=F, colors = pal,family="STKaiti")
        

6 生成模型数据

##freq words
text_trdtm<-DocumentTermMatrix(text_tr)#Corpus generated word document matrix
inspect(text_trdtm)

ncol(text_trdtm)# Number of columns in word docment matrix
dict<-findFreqTerms(text_trdtm,5)# Words appearing above 5 are considered as high_frequency words

text_tedtm<-DocumentTermMatrix(text_te,list(dictionary=dict)) #select column

train<-apply(text_trdtm[,dict],2,function(x) ifelse(x,"yes","no"))# 1 and 0 to yes and no

test<-apply(text_tedtm,2,function(x) ifelse(x,"yes","no"))

inspect(test)

将出现在5次以上的词设为高频词,过滤掉出现次数低于5次的词语

7 建模与模型预测

##model
library(e1071)
library(gmodels)
sms<-naiveBayes(train,class[index1],laplace = 1)
testpre<-predict(sms,test,type="class")
CrossTable(testpre,class[-index1],prop.chisq = F,prop.t=F,dnn=c("actual","preedicted"))

在这里插入图片描述

目录
相关文章
|
前端开发 JavaScript 开发者
水墨代码:前端川的诞生——在夏日阴雨中启航
【前端川】网站于农历五月初一(2024年6月6日)上线,融合水墨画与现代前端技术,呈现独特的水墨代码美学。创建者陈川分享技术心得与实战经验,网站特色包括水墨风格界面、技术深度解析及实战案例。在夏日雨中启航,"前端川"致力于为开发者提供灵感与帮助,探索前端技术的无限可能。
532 17
|
编译器 C语言
关于合法浮点数、转义字符、八进制、十六进制、字符常量的判断
关于合法浮点数、转义字符、八进制、十六进制、字符常量的判断
|
机器学习/深度学习 人工智能 算法
阿里公开自研AI集群细节:64个GPU,百万分类训练速度提升4倍
从节点架构到网络架构,再到通信算法,阿里巴巴把自研的高性能AI集群技术细节写成了论文,并对外公布。
阿里公开自研AI集群细节:64个GPU,百万分类训练速度提升4倍
|
12月前
|
安全 小程序 Java
微信支付全流程实战指南
本文从底层逻辑到实战代码,完整覆盖了微信支付Native/JSAPI支付、异步回调、退款、对账等核心能力。在实际项目中,需结合业务场景补充异常监控、资金告警、日志审计等能力,进一步保障支付系统的稳定性和资金安全。
1248 6
|
自然语言处理 前端开发
收藏级5款免费wordpress主题分享
本文分享了5款值得收藏的免费WordPress主题:1. OceanWP,现代多功能主题,适合电商;2. Divi,高级主题带页面构建器,功能丰富;3. Breakthrough Pro,基于Genesis框架的企业主题;4. Neve,时尚主题支持多语言;5. Ultra,商业主题搭配Themify构建器。每款主题均附有预览图,方便选择。更多主题可访问http://ztmao.com。
782 1
|
开发工具 git
如何操作github,gitee,gitcode三个git平台建立镜像仓库机制,这样便于维护项目只需要维护一个平台仓库地址的即可-优雅草央千澈
如何操作github,gitee,gitcode三个git平台建立镜像仓库机制,这样便于维护项目只需要维护一个平台仓库地址的即可-优雅草央千澈
2360 69
如何操作github,gitee,gitcode三个git平台建立镜像仓库机制,这样便于维护项目只需要维护一个平台仓库地址的即可-优雅草央千澈
|
XML 编解码 开发工具
多路RTSP转RTMP推送方案的两个选择
RTSP转RTMP模块设计,可以用ffmpeg直接命令行转发,也可以用方案二的非常成熟的转发设计,ffmpeg转发,需要有一定的代码基础,有问题的话,bug修复需要对底层逻辑非常了解才行,方案二,技术成熟,二次开发难度不大,很容易集成到自己现有系统
753 2
|
设计模式 安全 Java
Java编程中的单例模式实现与应用
【8月更文挑战第31天】在Java的世界里,单例模式是构建高效且资源友好应用的基石之一。本文将深入浅出地介绍如何通过单例模式确保类只有一个实例,并提供一个全局访问点。我们将探索多种实现方法,包括懒汉式、饿汉式和双重校验锁,同时也会讨论单例模式在多线程环境下的表现。无论你是Java新手还是资深开发者,这篇文章都将为你打开一扇理解并有效应用单例模式的大门。
|
索引
leetcode题解:26.删除有序数组重复项
leetcode题解:26.删除有序数组重复项
179 0