当前位置:网站首页>R语言做文本挖掘 Part4文本分类
R语言做文本挖掘 Part4文本分类
2022-07-06 12:55:00 【全栈程序员站长】
大家好,又见面了,我是全栈君。
Part4文本分类
Part3文本聚类提到过。与聚类分类的简单差异。
那么,我们需要理清训练集的分类,有明白分类的文本;測试集,能够就用训练集来替代。预測集,就是未分类的文本。是分类方法最后的应用实现。
1. 数据准备
训练集准备是一个非常繁琐的功能,临时没发现什么省力的办法,依据文本内容去手动整理。这里还是使用的某品牌的官微数据,依据微博内容。我将它微博的主要内容分为了:促销资讯(promotion)、产品推介(product)、公益信息(publicWelfare)、生活鸡汤(life)、时尚资讯(fashionNews)、影视娱乐(showbiz)。每一个分类有20-50条数据。例如以下可看到训练集下每一个分类的文本数目,训练集分类名为中文也没问题。
训练集为hlzj.train,后面也会被用作測试集。
预測集就是Part2里面的hlzj。
> hlzj.train <-read.csv(“hlzj_train.csv”,header=T,stringsAsFactors=F)
> length(hlzj.train)
[1] 2
> table(hlzj.train$type)
fashionNews life product
27 34 38
promotion publicWelfare showbiz
45 22 36
> length(hlzj)
[1] 1639
2. 分词处理
训练集、測试集、预測集都须要做分词处理后才干进行兴许的分类过程。
这里不再具体说明,过程类似于Part2中讲到的。
训练集做完分词后hlzjTrainTemp。之前对hlzj文件做过分词处理后是hlzjTemp。
然后分别将hlzjTrainTemp和hlzjTemp去除停词。
> library(Rwordseg)
加载须要的程辑包:rJava
# Version: 0.2-1
> hlzjTrainTemp <- gsub(“[0-90123456789 < > ~]”,””,hlzj.train$text)
> hlzjTrainTemp <-segmentCN(hlzjTrainTemp)
> hlzjTrainTemp2 <-lapply(hlzjTrainTemp,removeStopWords,stopwords)
>hlzjTemp2 <-lapply(hlzjTemp,removeStopWords,stopwords)
3. 得到矩阵
在Part3中讲到了。做聚类时要先将文本转换为矩阵,做分类相同须要这个过程。用到tm软件包。先将训练集和预測集去除停词后的结果合并为hlzjAll,记住前202(1:202)条数据是训练集,后1639(203:1841)条是预測集。获取hlzjAll的语料库,而且得到文档-词条矩阵。将其转换为普通矩阵。
> hlzjAll <- character(0)
> hlzjAll[1:202] <- hlzjTrainTemp2
> hlzjAll[203:1841] <- hlzjTemp2
> length(hlzjAll)
[1] 1841
> corpusAll <-Corpus(VectorSource(hlzjAll))
> (hlzjAll.dtm <-DocumentTermMatrix(corpusAll,control=list(wordLengths = c(2,Inf))))
<<DocumentTermMatrix(documents: 1841, terms: 10973)>>
Non-/sparse entries: 33663/20167630
Sparsity : 100%
Maximal term length: 47
Weighting : term frequency (tf)
> dtmAll_matrix <-as.matrix(hlzjAll.dtm)
4. 分类
用到knn算法(K近邻算法)。这个算法在class软件包里。
矩阵的前202行数据是训练集,已经有分类了,后面的1639条数据没有分类。要依据训练集得到分类模型再为其做分类的预測。
将分类后的结果和原微博放在一起。用fix()查看,能够看到分类结果,效果还是挺明显的。
> rownames(dtmAll_matrix)[1:202] <-hlzj.train$type
> rownames(dtmAll_matrix)[203:1841]<- c(“”)
> train <- dtmAll_matrix[1:202,]
> predict <-dtmAll_matrix[203:1841,]
> trainClass <-as.factor(rownames(train))
> library(class)
> hlzj_knnClassify <-knn(train,predict,trainClass)
> length(hlzj_knnClassify)
[1] 1639
> hlzj_knnClassify[1:10]
[1] product product product promotion product fashionNews life
[8] product product fashionNews
Levels: fashionNews life productpromotion publicWelfare showbiz
> table(hlzj_knnClassify)
hlzj_knnClassify
fashionNews life product promotion publicWelfare showbiz
40 869 88 535 28 79
> hlzj.knnResult <-list(type=hlzj_knnClassify,text=hlzj)
> hlzj.knnResult <-as.data.frame(hlzj.knnResult)
> fix(hlzj.knnResult)
Knn分类算法算是最简单的一种。后面尝试使用神经网络算法(nnet())、支持向量机算法(svm())、随机森林算法(randomForest())时。都出现了电脑内存不够的问题,我的电脑是4G的,看内存监控时能看到最高使用达到3.92G。
看样子要换台给力点的电脑了╮(╯▽╰)╭
在硬件条件能达到时,应该实现分类没有问题。相关的算法能够用:??方法名,的方式来查看其说明文档。
5. 分类效果
上面没有讲到測试的过程,对上面的样例来说,就是knn前两个參数都用train,由于使用数据集同样。所以得到的结果也是正确率能达到100%。在训练集比較多的情况下。能够将其随机按7:3或者是8:2分配成两部分,前者做训练后者做測试就好。这里就不再细述了。
在分类效果不理想的情况下。改进分类效果须要丰富训练集。让训练集特征尽量明显。这个在实际问题是一个非常繁琐却不能敷衍的过程。
有什么能够改进的地方欢迎更正,转载请注明出处,谢谢!
版权声明:本文博主原创文章,博客,未经同意不得转载。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/117093.html原文链接:https://javaforall.cn
边栏推荐
- 审稿人dis整个研究方向已经不仅仅是在审我的稿子了怎么办?
- R語言可視化兩個以上的分類(類別)變量之間的關系、使用vcd包中的Mosaic函數創建馬賽克圖( Mosaic plots)、分別可視化兩個、三個、四個分類變量的關系的馬賽克圖
- el-table表格——获取单击的是第几行和第几列 & 表格排序之el-table与sort-change、el-table-column与sort-method & 清除排序-clearSort
- js中,字符串和数组互转(二)——数组转为字符串的方法
- Web开发小妙招:巧用ThreadLocal规避层层传值
- 新型数据库、多维表格平台盘点 Notion、FlowUs、Airtable、SeaTable、维格表 Vika、飞书多维表格、黑帕云、织信 Informat、语雀
- User defined current limiting annotation
- What are RDB and AOF
- PHP saves session data to MySQL database
- @PathVariable
猜你喜欢
PHP saves session data to MySQL database
【微信小程序】運行機制和更新機制
Interviewer: what is the internal implementation of ordered collection in redis?
Opencv learning example code 3.2.3 image binarization
New database, multidimensional table platform inventory note, flowus, airtable, seatable, Vig table Vika, Feishu multidimensional table, heipayun, Zhixin information, YuQue
Aiko ai Frontier promotion (7.6)
Swagger UI教程 API 文档神器
Study notes of grain Mall - phase I: Project Introduction
爱可可AI前沿推介(7.6)
039. (2.8) thoughts in the ward
随机推荐
Spark SQL chasing Wife Series (initial understanding)
什么是RDB和AOF
C language games - minesweeping
2017 8th Blue Bridge Cup group a provincial tournament
Aike AI frontier promotion (7.6)
Mécanisme de fonctionnement et de mise à jour de [Widget Wechat]
Taylor series fast Fourier transform (FFT)
动态切换数据源
Activiti global process monitors activitieventlistener to monitor different types of events, which is very convenient without configuring task monitoring in acitivit
ICML 2022 | Flowformer: 任务通用的线性复杂度Transformer
Web开发小妙招:巧用ThreadLocal规避层层传值
ICML 2022 | flowformer: task generic linear complexity transformer
Interviewer: what is the internal implementation of ordered collection in redis?
审稿人dis整个研究方向已经不仅仅是在审我的稿子了怎么办?
新型数据库、多维表格平台盘点 Notion、FlowUs、Airtable、SeaTable、维格表 Vika、飞书多维表格、黑帕云、织信 Informat、语雀
Redis insert data garbled solution
20220211 failure - maximum amount of data supported by mongodb
Reviewer dis's whole research direction is not just reviewing my manuscript. What should I do?
基于STM32单片机设计的红外测温仪(带人脸检测)
el-table表格——获取单击的是第几行和第几列 & 表格排序之el-table与sort-change、el-table-column与sort-method & 清除排序-clearSort