0

我有一个来自图书馆的 LDA_Gibbs 主题模型topicmodels。我还有一个 LDAvis 交互式可视化。

我的问题是;LDA 对象和 LDAvis 中的主题顺序不同。

我想让一个映射到另一个(不在乎哪个)。到目前为止我没有工作的方法:

ldavis_data <- fromJSON(json_lda)
topic_order <- ldavis_data$topic.order
lda@gamma[order(topic_order), ]
lda@beta[, order(topic_order)]

受到这个 github 问题的启发——虽然是一个不同的主题模型包

然而,这完全破坏了我的 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

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

4

2 回答 2

0

我认为order()是问题所在,而且我认为您在需要对行进行排序时尝试对列进行排序,反之亦然。假设您已经从 topicmodels 方法创建了一个不错的 LDAvis,这应该可以让它们同步:

ldavis_data <- fromJSON(json_lda)
topic_order <- ldavis_data$topic.order
lda@gamma[,topic_order]
lda@beta[topic_order,] 

此外,如果您希望使用 topicmodels 包生成的模型在 LDAvis 中显示 phi 和 theta 数据,您可以执行以下操作:

lda_posterior <- posterior(lda)
lda_theta <- lda_posterior $topics[,topic_order]
lda_phi <- lda_posterior $terms[topic_order,]

于 2022-01-10T04:39:53.530 回答
0

重新发布我的编辑作为答案,但我还不倾向于接受它。

我得到了我想要的结果,当然;但不是想要的。


几乎找到了一个合理的权宜之计:我的进一步分析依赖于tidy.LDA()tidytext 中的函数,因此我可以像这样添加主题术语映射的正确顺序:


# 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

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

于 2020-10-05T17:37:59.693 回答