【问题标题】:Name topics in lda topic modeling based on beta values在基于 beta 值的 lda 主题建模中命名主题
【发布时间】:2021-05-06 03:26:55
【问题描述】:

我目前正在尝试为我必须写的论文开发代码。我想进行基于 LDA 的主题建模。我在 GitHub 上找到了一些代码存款,并且能够将它们组合起来并在必要时对其进行微调。 现在我想添加一些东西,在分配给相应主题的最高 beta 值的单词之后命名每个识别的主题。 有任何想法吗?这是我第一次编写任何代码,因此我的专业知识非常有限。

这是我要插入“命名部分”的代码部分:

# get the top ten terms for each topic
  top_terms <- topics  %>% 
    group_by(topic) %>% # treat each topic as a different group
    top_n(10, beta) %>% # get top 10 words
    ungroup() %>% 
    arrange(topic, -beta) # arrange words in descending informativeness
   
# plot the top ten terms for each topic in order
    top_terms %>%
      mutate(term = reorder(term, beta)) %>% # sort terms by beta value 
      ggplot(aes(term, beta, fill = factor(topic))) + # plot beta by theme
      geom_col(show.legend = FALSE) + # as bar plot
      facet_wrap(~ topic, scales = "free") + # separate plot for each topic
      labs(x = NULL, y = "Beta") + # no x label, change y label 
      coord_flip() # turn bars sideways

我尝试将它插入到这部分代码中,但没有成功。 我找到了这个:R topic modeling: lda model labeling function,但这对我不起作用,或者我没有得到它。

我不能透露更多的代码,因为里面有一些有意义的数据,但仍然非常感谢社区的一些专业知识。

最好的问候并保持安全

注意:它说top_terms 是一个小标题。我试图想出一些我头顶的数据。 top_terms 中的数据结构就是这样

主题词测试版

(int)  (chr)  (dbl)
1   book    0,9876 
1   page    0,9765
1   chapter 0,9654
2   sports  0,8765
2   soccer  0,8654
2   champions   0,8543
3   music   0,9543
3   song    0,8678
3   artist  0,7231
4   movie   0,9846
4   cinema  0,9647
4   cast    0,8878

【问题讨论】:

  • 如果您有敏感数据,是否可以生成与top_terms 数据具有相同结构(列名和类型)的虚拟数据集?
  • 请告诉我,这是否可以作为top_terms 的虚拟数据集。我不确定

标签: r ggplot2 lda


【解决方案1】:

您可以在数据中创建一个额外的列,在按主题分组后,采用具有最高 beta 的术语的名称。

suppressPackageStartupMessages({
  library(ggplot2)
  library(tibble)
  library(dplyr)
})

# Just replicating example data
top_terms <- tibble(
  topic = rep(1:4, each = 3),
  term = c("book", "page", "chapter", 
           "sports", "soccer", "champions", 
           "music", "song", "artist",
           "movie", "cinema", "cast"),
  beta = c(0.9876, 0.9765, 0.9654,
           0.8765, 0.8654, 0.8543,
           0.9543, 0.8678, 0.7231,
           0.9846, 0.9647, 0.8878)
) 

top_terms %>%
  group_by(topic) %>%
  mutate(top_term = term[which.max(beta)]) %>%
  ggplot(aes(term, beta, fill = factor(topic))) +
  geom_col(show.legend = FALSE) +
  facet_wrap(~ top_term, scales = "free") +
  labs(x = NULL, y = "Beta") +
  coord_flip()

reprex package (v1.0.0) 于 2021-05-05 创建

【讨论】:

  • 非常感谢。那成功了。正是我想要的。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-06-09
  • 1970-01-01
  • 1970-01-01
  • 2018-03-01
  • 2016-02-27
  • 1970-01-01
  • 2012-03-25
相关资源
最近更新 更多