




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
《数据挖掘试验》数据处理试验报告试验目的:1、把握数据的处理方法及其实现。2、把握Apriori关联分析。试验设备与环境:计算机,RRStduio试验内容:1、对C题评论数据进展数据预处理〔酒店和景点其一,包括缺失值统计及其处理、分类统计、频率占比分析。21处理好的数据分词,生成词云图。3Apriori算法的使用,对酒店〔或景点〕的数据分词进展关联分析。试验设计过程及分析:1、对C题评论数据进展数据预处理〔酒店和景点其一计及其处理、分类统计、频率占比分析。#代码8-4#设置工作名目setwd(“D:/YSY/数据分析方法案例讲解程序“)#去重,去除完全重复的数据meidi_reviews<-read.csv(“./酒店评论〔样例数据〕.csv“,stringsAsFactors=FALSE)meidi_reviews<-unique(meidi_reviews[,c(2,3)]) #对评论内容去重,reviews<-meidi_reviews$评论详情#代码8-5#去除去除英文字母、数字等reviews<-gsub(“[a-zA-Z0-9]“,““,reviews)#由于评论主要为京东美的电热水器的评论,因此去除这些词语reviews<-gsub(“酒店“,““,reviews)reviews<-gsub(“广告“,““,reviews)reviews<-gsub(“房间“,““,reviews)reviews<-gsub(“前台“,““,reviews)reviews<-gsub(“早餐“,““,reviews)reviews<-gsub(“效劳“,““,reviews)#代码8-6#分词library(jiebaR) #Version:0.9.1cutter<-worker(type=“tag“,stop_word=“./stoplist.txt“)seg_word<-listfor(iin1:length(reviews)){seg_word[[i]]<-segment(reviews[i],cutter)}head(seg_word,40)#将词语转为数据框形式,一列是词,一列是词语所在的句子ID,最终一列是词语在该句子的位置n_word<-sapply(seg_word,length) #每个词条的词个数index<-rep(1:length(seg_word),n_word) #每个词条有多少个词就复制多少次type<-rep(meidi_reviews$评论详情,n_word)nature<-unlist(sapply(seg_word,names))result<-data.frame(index,unlist(seg_word),nature,type)colnames(result)<-c(“id“,“word“,“nature“,“type“)head(result)#将每个词在每个词条的位置标记出来n_word<-sapply(split(result,result$id),nrow)index_word<-sapply(n_word,seq_len)index_word<-unlist(index_word)result$index_word<-index_wordhead(result)#代码8-7#提取含知名词类的评论数据is_n<-subset(result,grepl(“n“,result$nature),“id“)result<-result[result$id%in%is_n$id,]1、21处理好的数据分词,生成词云图。#代码8-8#绘制词云#查看分词效果,最快捷的方式是绘制词云install.packages(“wordcloud2“)library(wordcloud2) #Version:0.2.0# 统计词频word.frep<-table(result$word)word.frep<-sort(word.frep,decreasing=TRUE)word.frep<-data.frame(word.frep)head(word.frep)wordcloud2(word.frep[1:100,],color=“random-dark“)write.csv(result,“./酒店评论〔样例数据〕.csv“,s=FALSE)3Apriori算法的使用,对酒店〔或景点〕的数据分词进展关联分析。#载入分词结果word<-read.csv(“data/Word.csv“,stringsAsFactors=FALSE)#情感词定位#情感词定位#读入正面、负面情感评价词posment<-read.table(“./data/正面评价词语〔中文〕.txt“)negment<-read.table(“./data/负面评价词语〔中文〕.txt“)pos.emotion<-read.table(“./data/正面情感词语〔中文〕.txt“)neg.emotion<-read.table(“./data/负面情感词语〔中文〕.txt“)positive<-rbind(posment,pos.emotion)negative<-rbind(negment,neg.emotion)#查看正负面情感词表是否有一样的词语,假设有则依据状况将其删除sameWord<-intersect(positive[,1],negative[,1])positive<-data.frame(setdiff(positive[,1],sameWord))negative<-data.frame(setdiff(negative[,1],sameWord))#给正面、负面词语赋权重,正面词语为1,负面为-1positive$weight<-rep(1,length(positive))colnames(positive)<-c(“word“,“weight“)negative$weight<-rep(-1,length(negative))colnames(negative)<-c(“word“,“weight“)#将正面、负面词语合并posneg<-rbind(positive,negative)head(posneg,20)#将分词结果与正负面情感词表合并,定位情感词install.packages(“plyr“)library(plyr)data.posneg<-join(word,posneg,by=“word“,match=“first“)head(data.posneg)#依据情感词前是否有否认词或双层否认词对情感值进展修正#载入否认词表notdict<-read.table(“./data/not.csv“,stringsAsFactors=FALSE)notdict$weight<-rep(-1,length(notdict))#处理否认修饰词data.posneg$amend_weight<-data.posneg$weightonly_inclination<-data.posneg[!is.na(data.posneg$weight),] #只保存有情感值的词语index<-as.numeric(s(only_inclination)) #词语对应整个文档的位置for(iin1:nrow(only_inclination)){#提取第i个情感词所在的评论review<-data.posneg[which(data.posneg$id==only_inclination[i,]$id),]#i个情感值在该文档的位置affective<-only_inclination[i,]$index_wordif(affective==2){ #假设情感词的位置是某个文档的其次个词#假设情感词前的一个词在否认词表内消灭则求出个数<-sum(review$word[affective-1]%in%notdict[,1])#假设求出的和为奇数,认为该词为相反的情感值if(a.1==1)data.posneg$amend_weight[index[i]]<--data.posneg$weight[index[i]]}elseif(affective>=3){<-sum(review$word[affective-c(1,2)]%in%notdict[,1])if(a.2==1)data.posneg$amend_weight[index[i]]<--data.posneg$weight[index[i]]}}#更只保存情感值的数据#只保存有情感值的词语only_inclination<-data.posneg[!is.na(data.posneg$amend_weight),]index<-as.numeric(s(only_inclination))head(only_inclination)#计算每条评论的情感值meidi.posneg<-aggregate(only_inclination$amend_weight,by=list(only_inclination$id),sum)head(meidi.posneg)colnames(meidi.posneg)<-c(“id“,“weight“)meidi.posneg<-meidi.posneg[-which(meidi.posneg$weight==0),]meidi.posneg$a_type<-rep(NA,nrow(meidi.posneg))meidi.posneg$a_type[which(meidi.posneg$weight>0)]<-“pos“meidi.posneg$a_type[which(meidi.posneg$weight<0)]<-“neg“head(meidi.posneg)result<-join(meidi.posneg,word[,c(1,4)],by=“id“,type=“left“,match=“first“)head(result)#计算情感分析的准确率Confusion_matrix<-table(result$type,result$a_type)Confusion_matrix(Confusion_matrix[1,1]+Confusion_matrix[2,2])/sum(Confusion_matrix)#提取正负面评论信息head(meidi.posneg)ind.neg<-subset(meidi.posneg,meidi.posneg$weight<0,select=c(“id“))ind.pos<-subset(meidi.posneg,meidi.posneg$weight>0,select=c(“id“))negdata<-word[word$id%in%ind.neg$id,]posdata<-word[word$id%in%ind.pos$id,]head(negdata)head(posdata)write.csv(negdata,“./data/negdata.csv“,s=FALSE)write.csv(posdata,“./data/posdata.csv“,s=FALSE)install.packages(“tm“)library(tm)#载入情感分析后的数据posdata<-read.csv(“./data/posdata.csv“,stringsAsFactors=FALSE)negdata<-read.csv(“./data/negdata.csv“,stringsAsFactors=FALSE)#构建语料库library(NLP)library(tm)#Version:0.7-1pos.corpus<-Corpus(VectorSource(posdata$word))neg.corpus<-Corpus(VectorSource(negdata$word))#词条-文档关系矩阵pos.gxjz<-DocumentTermMatrix(pos.corpus,control=list(wordLengths=c(1,Inf),bounds=list(global=5,Inf),removeNumbers=TRUE))neg.gxjz<-DocumentTermMatrix(neg.corpus,control=list(wordLengths=c(1,Inf),bounds=list(global=5,Inf),removeNumbers=TRUE))#代码8-13#构造主题间余弦相像度函数install.packages(“topicmodels“)library(topicmodels)lda.k<-function(gxjz){#初始化平均余弦相像度mean_similarity<-cmean_similarity[1]=1#循环生成主题并计算主题间相像度for(iin2:10){control<-list(burnin=500,iter=1000,keep=100)Gibbs<-LDA(gxjz,k=i,method=“Gibbs“,control=control)term<-terms(Gibbs,50) #提取主题词#构造词频向量word<-as.vector(term) #列出全部词freq<-table(word) #统计词频unique_word<-names(freq)mat<-matrix(rep(0,i*length(unique_word)), #行数为主题数,列数为词nrow=i,ncol=length(unique_word))colnames(mat)<-unique_word#生成词频向量for(kin1:i){for(tin1:50){mat[k,grep(term[t,k],unique_word)]<-mat[k,grep(term[t,k],unique_word)]+1}}p<-combn(c(1:i),2)l<-ncol(p)top_similarity<-cfor(jin1:l){#计算余弦相像度x<-mat[p[,j][1],]y<-mat[p[,j][2],]top_similarity[j]<-sum(x*
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- -天津市五区县重点校2024-2025学年高二上学期1月期末考试 化学试题(解析版)
- 2021年云南、贵州二级建造师机考《建设工程施工管理》真题汇编
- 风电场风机检修规程
- 苹果平板如何制作
- 2025年咖啡师职业技能测试卷:咖啡饮品口感与品质提升策略试题
- 2025年FRM金融风险管理师考试风险投资与风险管理试卷
- 2025年征信行业自律管理征信数据质量试题库
- 2025年成人高考《语文》语言逻辑训练题库实战演练
- 2025年安全生产法规考试题库:法律法规解读与真题解析
- 人力资源配置合理化方案书
- 《病例随访汇报》课件
- 2025江苏省沿海开发集团限公司招聘23人高频重点提升(共500题)附带答案详解
- 2024年09月2024华夏金融租赁有限公司校园招聘笔试历年参考题库附带答案详解
- 锂电池技术研发生产合同
- 【MOOC期末】《英美文学里的生态》(北京林业大学)期末中国大学慕课MOOC答案
- 眼镜定配工(初级)理论知识要素细目表
- 重晶石采购合同范例
- 《肺细胞病理学》课件
- 2024年共青团入团考试题库及答案
- 2019北师大版高中英语单词表全7册
- 中国心力衰竭诊断和治疗指南2024解读(完整版)
评论
0/150
提交评论