我可以手动重新排序LDA_Gibbs主题模型吗

问题描述

我有一个来自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

是的,也适用于文档映射。现在将它们折叠为一个函数;)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)