python - LDA gensim实现,两个不同文档之间的距离

标签 python probability gensim

编辑:我在这里发现了一个有趣的问题。 This link表明 gensim 在训练和推理步骤中都使用了随机性。所以这里建议的是设置一个固定的种子,以便每次都能得到相同的结果。但是,为什么我对每个主题的概率都相同?

我想做的是为每个推特用户找到她的主题,并根据主题的相似性计算推特用户之间的相似度。是否有可能为 gensim 中的每个用户计算相同的主题,或者我是否必须计算主题字典并对每个用户主题进行聚类?

一般来说,基于 gensim 中的主题模型提取来比较两个 Twitter 用户的最佳方法是什么?我的代码如下:

   def preprocess(id): #Returns user word list (or list of user tweet)

        user_list =  user_corpus(id, 'user_'+str(id)+'.txt')
        documents = []
        for line in open('user_'+str(id)+'.txt'):
                 documents.append(line)
        #remove stop words
        lines = [line.rstrip() for line in open('stoplist.txt')]
        stoplist= set(lines)  
        texts = [[word for word in document.lower().split() if word not in stoplist]
                   for document in documents]
        # remove words that appear only once
        all_tokens = sum(texts, [])
        tokens_once = set(word for word in set(all_tokens) if all_tokens.count(word) < 3)
        texts = [[word for word in text if word not in tokens_once]
                   for text in texts]
        words = []
        for text in texts:
            for word in text:
                words.append(word)

        return words


    words1 = preprocess(14937173)
    words2 = preprocess(15386966)
    #Load the trained model
    lda = ldamodel.LdaModel.load('tmp/fashion1.lda')
    dictionary = corpora.Dictionary.load('tmp/fashion1.dict') #Load the trained dict

    corpus = [dictionary.doc2bow(words1)]
    tfidf = models.TfidfModel(corpus)
    corpus_tfidf = tfidf[corpus]
    corpus_lda = lda[corpus_tfidf]

    list1 = []
    for item in corpus_lda:
      list1.append(item)

    print lda.show_topic(0)
    corpus2 = [dictionary.doc2bow(words2)]
    tfidf2 = models.TfidfModel(corpus2)
    corpus_tfidf2 = tfidf2[corpus2]
    corpus_lda2 = lda[corpus_tfidf2]

    list2 = []
    for it in corpus_lda2:
      list2.append(it)

    print corpus_lda.show_topic(0)  

返回用户语料库的主题概率(当使用用户词列表作为语料库时):

 [(0, 0.10000000000000002), (1, 0.10000000000000002), (2, 0.10000000000000002),
  (3, 0.10000000000000002), (4, 0.10000000000000002), (5, 0.10000000000000002),
  (6, 0.10000000000000002), (7, 0.10000000000000002), (8, 0.10000000000000002),
  (9, 0.10000000000000002)]

在我使用用户推文列表的情况下,我会返回每条推文的计算主题。

问题 2:以下是否有意义:使用之前计算的 LDA 模型,使用多个 Twitter 用户训练 LDA 模型并为每个用户(使用每个用户语料库)计算主题?

在提供的示例中,list[0] 返回概率为 0.1 的主题分布。 基本上,每一行文本对应一条不同的推文。如果我用 corpus = [dictionary.doc2bow(text) for text in texts] 计算语料库,它会分别给出每条推文的概率。另一方面,如果我像示例一样使用 corpus = [dictionary.doc2bow(words)],我将只将所有用户词作为语料库。在第二种情况下,gensim 为所有主题返回相同的概率。因此,对于这两个用户,我得到了相同的主题分布。

用户文本语料库应该是单词列表还是句子列表(推文列表)?

关于twitterRank approach中七和、翁建树的执行情况在第 264 页,它说:我们将各个推特用户发布的推文汇总到一个大文档中。因此,每个文档对应一个 twitterer。好吧,我很困惑,如果文档是所有用户的推文,那么语料库应该包含什么??

最佳答案

根据官方文档 Latent Dirichlet Allocation,LDA 是从词袋计数到低维主题空间的转换。

可以在TFIDF之上使用LSI,但不能使用LDA。 如果你在 LDA 上使用 TFIDF,那么它会生成几乎相同的每个主题,你可以打印并查看。

另见 https://radimrehurek.com/gensim/tut2.html .

关于python - LDA gensim实现,两个不同文档之间的距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24430238/

相关文章:

python - 如何有效地在屏幕上准确地绘制 N 个点?

python - 如何根据具体参数分配多个key :value dicts into an already established dictionary,

python - 将变音符号或修饰符变音符号 Unicode 字符转换为 'combining form'

algorithm - 随机算法概率最大化

python - 如何在不重新启动的情况下调试 PyQt 应用程序?

Java - 生成随机数字簇

python - 如何在Python中使用随机哈希函数?

gensim - 有没有办法从 KeyedVectors 词汇表中删除单词?

python - 属性错误: 'LdaModel' object has no attribute 'minimum_phi_value'

python - 在新的 M1 芯片 Mac 上安装 Gensim