【发布时间】:2021-01-20 13:07:31
【问题描述】:
我有一个来自 topicmodels 库的 LDA_Gibbs 主题模型。
我还有一个 LDAvis 交互式可视化。
我的问题是; LDA 对象和 LDAvis 中的主题顺序不同。
我想让一个映射到另一个(不在乎哪个)。 到目前为止我没有工作的方法:
ldavis_data <- fromJSON(json_lda)
topic_order <- ldavis_data$topic.order
lda@gamma[order(topic_order), ]
lda@beta[, order(topic_order)]
inspired by this github issue - a different topic model package though
然而,这完全破坏了我的 LDA 对象。
没有 reprex/MWE(还没有;我可以链接一个 .rds 文件)——但是 glimpse(lda) 的输出:
<snip>
..@ beta :num [1:45, 1:333...]
..@ gamma :num [1:111..., 1:45]
</snip>
现在,我手动将 ldavis 主题映射到 LDA() 对象并行。
---- 编辑----
我找到了一个合理的权宜之计,几乎: 我的进一步分析依赖于 tidytext 中的 tidy.LDA 函数,因此我可以像这样添加主题词映射的正确顺序:
# terms to topics
tidy(lda, matrix = "beta") %>%
# probably unnecessary, but make sure we're in topic order
arrange(topic) %>%
# turn topics into a factor, with levels according to new order
mutate(topic = factor(topic, levels = topic_order) %>%
# group by new factor order
group_by(topic) %>%
# make the current group id the current topic
mutate(topic = cur_group_id()) %>%
# dont forget! had me scratching my head for a few minutes
ungroup
# documents to topics
tidy(lda, matrix = "gamma") %>%
arrange(topic) %>%
mutate(topic = factor(topic, levels = topic_order) %>%
group_by(topic) %>%
mutate(topic = cur_group_id()) %>%
ungroup
是的,也适用于文档映射。现在将它们折叠成一个函数;)
【问题讨论】:
标签: r lda topicmodels