数据科学与大数据分析项目练习-6在数据集Cora上应用文本分析

这篇博客介绍了如何在Cora数据集上应用Latent Dirichlet Allocation (LDA)进行文本分析。通过安装和加载必要的包,如ggplot2和lda,对文档和词汇表数据进行处理。使用lda.collapsed.gibbs.sampler函数进行主题建模,并展示了如何获取主题中的顶部单词和文档中主题的比例。最后,通过可视化展示了每个文档中不同主题的占比情况。
摘要由CSDN通过智能技术生成

在数据集Cora上应用文本分析


首先还是要安装并加载需要使用的包

require("ggplot2")
install.packages("reshape2")
install.packages("lda")


require("reshape2")
require("lda")
#加载文档和词汇表数据(cora.documents)
data(cora.documents)
data(cora.vocab)
# 当前/活动主题将自动应用于您绘制的每个绘图
theme_set(theme_bw())

加载得到的cora documents 如下所示
在这里插入图片描述


# 显示的主题集群数量
K <- 10

# 要显示的文档数量
N <- 9

result <- lda.collapsed.gibbs.sampler(cora.documents,
                                      K,  
                                      cora.vocab,
                                      25,  
                                      0.1,
                                      0.1,
                                      compute.log.likelihood=TRUE) 

得到的result如图:
在这里插入图片描述


# 获取集群中的顶部单词
top.words <- top.topic.words(result$topics, 5, by.score=TRUE)

# 构建主题的比例
topic.props <- t(result$document_sums) / colSums(result$document_sums)

document.samples <- sample(1:dim(topic.props)[1], N)
topic.props <- topic.props[document.samples,]

topic.props[is.na(topic.props)] <-  1 / K

colnames(topic.props) <- apply(top.words, 2, paste, collapse=" ")

topic.props.df <- melt(cbind(data.frame(topic.props),
                             document=factor(1:N)),
                       variable.name="topic",
                       id.vars = "document")  

qplot(topic, value*100, fill=topic, stat="identity",
      ylab="proportion (%)", data=topic.props.df,
      geom="histogram") +
  theme(axis.text.x = element_text(angle=0, hjust=1, size=12)) +
  coord_flip() +
  facet_wrap(~ document, ncol=3)

最终我们得到了下图:
在这里插入图片描述
参考书目

  1. Data Science and Big Data Analytics: Discovering, Analyzing, Visualizing and Presenting Data, EMC Education Services, John Wiley & Sons, 27 Jan. 2015
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值