【问题标题】:Can I manually reorder a LDA_Gibbs topicmodel我可以手动重新排序 LDA_Gibbs 主题模型吗
【发布时间】: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


    【解决方案1】:

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

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


    我找到了一个合理的权宜之计,几乎: 我的进一步分析依赖于 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
    

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

    【讨论】:

      【解决方案2】:

      我认为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,]
      
      

      【讨论】:

        猜你喜欢
        • 2021-05-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-24
        • 2023-04-04
        • 2017-12-20
        • 1970-01-01
        • 2012-11-12
        相关资源
        最近更新 更多