当前位置: 首页 > 面试题库 >

具有主题模型的LDA,如何查看不同文档属于哪些主题?

姬翰林
2023-03-14
问题内容

我正在使用topicmodels包中的LDA,并且已经在大约30.000个文档上运行了LDA,获得了30个主题,并且获得了主题的前10个字,它们看起来非常好。但是我想看看哪些文档最有可能属于哪个主题,该怎么办?

myCorpus <- Corpus(VectorSource(userbios$bio))
docs <- userbios$twitter_id
myCorpus <- tm_map(myCorpus, tolower)
myCorpus <- tm_map(myCorpus, removePunctuation)
myCorpus <- tm_map(myCorpus, removeNumbers)
removeURL <- function(x) gsub("http[[:alnum:]]*", "", x)
myCorpus <- tm_map(myCorpus, removeURL)
myStopwords <- c("twitter", "tweets", "tweet", "tweeting", "account")

# remove stopwords from corpus
myCorpus <- tm_map(myCorpus, removeWords, stopwords('english'))
myCorpus <- tm_map(myCorpus, removeWords, myStopwords)


# stem words
# require(rJava) # needed for stemming function 
# library(Snowball) # also needed for stemming function 
# a <- tm_map(myCorpus, stemDocument, language = "english")

myDtm <- DocumentTermMatrix(myCorpus, control = list(wordLengths=c(2,Inf), weighting=weightTf))
myDtm2 <- removeSparseTerms(myDtm, sparse=0.85)
dtm <- myDtm2

library(topicmodels)

rowTotals <- apply(dtm, 1, sum)
dtm2 <- dtm[rowTotals>0]
dim(dtm2)
dtm_LDA <- LDA(dtm2, 30)

问题答案:

如何使用内置数据集。这将向您显示哪些文档属于哪个主题的可能性最高。

library(topicmodels)
data("AssociatedPress", package = "topicmodels")

k <- 5 # set number of topics
# generate model
lda <- LDA(AssociatedPress[1:20,], control = list(alpha = 0.1), k)
# now we have a topic model with 20 docs and five topics

# make a data frame with topics as cols, docs as rows and
# cell values as posterior topic distribution for each document
gammaDF <- as.data.frame(lda@gamma) 
names(gammaDF) <- c(1:k)
# inspect...
gammaDF
              1            2            3            4            5
1  8.979807e-05 8.979807e-05 9.996408e-01 8.979807e-05 8.979807e-05
2  8.714836e-05 8.714836e-05 8.714836e-05 8.714836e-05 9.996514e-01
3  9.261396e-05 9.996295e-01 9.261396e-05 9.261396e-05 9.261396e-05
4  9.995437e-01 1.140774e-04 1.140774e-04 1.140774e-04 1.140774e-04
5  3.573528e-04 3.573528e-04 9.985706e-01 3.573528e-04 3.573528e-04
6  5.610659e-05 5.610659e-05 5.610659e-05 5.610659e-05 9.997756e-01
7  9.994345e-01 1.413820e-04 1.413820e-04 1.413820e-04 1.413820e-04
8  4.286702e-04 4.286702e-04 4.286702e-04 9.982853e-01 4.286702e-04
9  3.319338e-03 3.319338e-03 9.867226e-01 3.319338e-03 3.319338e-03
10 2.034781e-04 2.034781e-04 9.991861e-01 2.034781e-04 2.034781e-04
11 4.810342e-04 9.980759e-01 4.810342e-04 4.810342e-04 4.810342e-04
12 2.651256e-04 9.989395e-01 2.651256e-04 2.651256e-04 2.651256e-04
13 1.430945e-04 1.430945e-04 1.430945e-04 9.994276e-01 1.430945e-04
14 8.402940e-04 8.402940e-04 8.402940e-04 9.966388e-01 8.402940e-04
15 8.404830e-05 9.996638e-01 8.404830e-05 8.404830e-05 8.404830e-05
16 1.903630e-04 9.992385e-01 1.903630e-04 1.903630e-04 1.903630e-04
17 1.297372e-04 1.297372e-04 9.994811e-01 1.297372e-04 1.297372e-04
18 6.906241e-05 6.906241e-05 6.906241e-05 9.997238e-01 6.906241e-05
19 1.242780e-04 1.242780e-04 1.242780e-04 1.242780e-04 9.995029e-01
20 9.997361e-01 6.597684e-05 6.597684e-05 6.597684e-05 6.597684e-05


# Now for each doc, find just the top-ranked topic   
toptopics <- as.data.frame(cbind(document = row.names(gammaDF), 
  topic = apply(gammaDF,1,function(x) names(gammaDF)[which(x==max(x))])))
# inspect...
toptopics   
       document topic
1         1     2
2         2     5
3         3     1
4         4     4
5         5     4
6         6     5
7         7     2
8         8     4
9         9     1
10       10     2
11       11     3
12       12     1
13       13     1
14       14     2
15       15     1
16       16     4
17       17     4
18       18     3
19       19     4
20       20     3

那是你想做的吗?
此答案的提示:https : //stat.ethz.ch/pipermail/r-help/2010-August/247706.html



 类似资料:
  • 问题内容: 我能够运行gensim的LDA代码,并获得各自关键词的前10个主题。 现在,我想更进一步,通过查看将哪些文档归类到每个主题中,来了解LDA算法的准确性。gensim LDA有可能吗? 基本上我想做这样的事情,但是在python中并使用gensim。 具有主题模型的LDA,如何查看不同文档属于哪些主题? 问题答案: 使用主题的概率,您可以尝试设置一些阈值并将其用作聚类基线,但是我敢肯定,

  • 我不熟悉主题建模/潜在Dirichlet分配,并且难以理解如何将该概念应用于我的数据集(或者它是否是正确的方法)。 我有少量的文学文本(小说),并希望使用LDA提取一些一般主题。 我正在使用Python中的模块以及一些特性。为了进行测试,我将我的原始文本(仅6篇)分成30个块,每个块有1000个单词。然后,我将块转换为文档术语矩阵,并运行该算法。这就是代码(尽管我认为这与问题无关): 然而,结果与

  • 本文向大家介绍spring 有哪些主要模块?相关面试题,主要包含被问及spring 有哪些主要模块?时的应答技巧和注意事项,需要的朋友参考一下 spring core:框架的最基础部分,提供 ioc 和依赖注入特性。 spring context:构建于 core 封装包基础上的 context 封装包,提供了一种框架式的对象访问方法。 spring dao:Data Access Object

  • 通过sysconstraints只能看到一个{tab_id}_{id}字符串. 无法确认这个主键用了哪些列

  • 潜在狄利克雷分配(LDA)是一个主题模型,用于查找一组文档背后的潜在变量(主题)。我使用python gensim包,有两个问题: > 我打印出每个主题最频繁的单词(我尝试了10,20,50个主题),发现单词的分布非常“平坦”:意味着即使是最频繁的单词也只有1%的概率... 大多数主题都是相似的:这意味着每个主题中最常用的单词重叠很多,并且主题中的高频词几乎共享同一组单词。。。 我想问题可能是因为

  • 我们正在使用带有Datatorrent的Apache kafka来处理消息。有没有办法从shell命令行检查kafka主题中没有消息? 谢啦