python - 如何查找 SkLearn 模型的 LSA 和 LDA 的一致性分数?

标签 python text nlp text-mining topic-modeling

我想比较 LSA 和 LDA 模型的一致性分数。

LSA模型

lsa_model = TruncatedSVD(n_components=20, algorithm='randomized', n_iter=40, random_state=5000)

lsa_top=lsa_model.fit_transform(vect_text)

LDA模型

lda_model=LatentDirichletAllocation(n_components=20,learning_method='online',random_state=42,max_iter=1) 

有人可以帮我计算这两个模型的相干分数吗?

提前谢谢您!

最佳答案

我使用 sklearn TfidfVectorizer 结合 TruncatedSVD 来查找最适合我的语料库的主题。找不到 TruncatedSVD 的内置一致性,不得不实现我自己的。代码基于这篇文章:

http://qpleple.com/topic-coherence-to-evaluate-topic-models/

我决定坚持使用麻省大学的内在衡量标准,因为它相对容易实现。支持方式有:

def get_umass_score(dt_matrix, i, j):
    zo_matrix = (dt_matrix > 0).astype(int)
    col_i, col_j = zo_matrix[:, i], zo_matrix[:, j]
    col_ij = col_i + col_j
    col_ij = (col_ij == 2).astype(int)    
    Di, Dij = col_i.sum(), col_ij.sum()    
    return math.log((Dij + 1) / Di)

def get_topic_coherence(dt_matrix, topic, n_top_words):
    indexed_topic = zip(topic, range(0, len(topic)))
    topic_top = sorted(indexed_topic, key=lambda x: 1 - x[0])[0:n_top_words]
    coherence = 0
    for j_index in range(0, len(topic_top)):
        for i_index in range(0, j_index - 1):
            i = topic_top[i_index][1]
            j = topic_top[j_index][1]
            coherence += get_umass_score(dt_matrix, i, j)
    return coherence

def get_average_topic_coherence(dt_matrix, topics, n_top_words):
    total_coherence = 0
    for i in range(0, len(topics)):
        total_coherence += get_topic_coherence(dt_matrix, topics[i], n_top_words)
    return total_coherence / len(topics)

用法:

for n_topics in range(5, 1000, 50):
    svd = TruncatedSVD(n_components=n_topics, n_iter=7, random_state=42)
    svd.fit(tfidf_matrix)
    avg_coherence = get_average_topic_coherence(tfidf_matrix, svd.components_, 10)
    print(str(n_topics) + " " + str(avg_coherence))

输出:

5 -72.44478726897546
55 -86.18040144608892
105 -88.9175058514422
155 -90.3841147807378
205 -91.83948259181923
255 -92.01751480271953 < best
305 -90.73603639282118
355 -89.85740639388695
405 -89.41916273620417
455 -87.66472648614531
505 -85.06725618307024
555 -81.1419066684933
605 -77.03963739283286
655 -73.04509144854593
705 -69.84849596265884
755 -68.01357538571055
805 -67.48039395600706
855 -67.53091204608572
905 -67.23467504644942
955 -66.86079451952988

麻省大学的一致性越低越好。就我而言,255 个主题最适合我的语料库。我使用了 10 个与主题最相关的单词 - 你可以使用你的号码。您将得到不同的数字,但主题(SVD 组件)的最佳数量通常是相同的。

我使用的是 TF-IDF 向量,但这种一致性应该适用于任何基于术语频率的方法(例如 BOW)

关于python - 如何查找 SkLearn 模型的 LSA 和 LDA 的一致性分数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69730428/

相关文章:

python - Flask View 引发 TypeError : 'bool' object is not callable

python - 使用python并行输入

python - Selenium 的 GeckoDriver 不在全局路径中

html - 如何不将 <ul> 中的多行文本与元素符号对齐,而是与上面的文本对齐?

delphi - 为什么 Synopse 连字代码给出的结果与 TeX 的结果不同?

python - NLP 句子意义提取方法

python - 在 Flask 中渲染具有相同文件名的动态变化图像

python - 如何根据 Python 中的部分匹配从文本中删除子字符串?

python - 是否有用于将 RTF 转换为纯文本的 Python 模块?

python - Spacy - nlp.pipe() 返回生成器