簡體   English   中英

主題建模 - 在 sklearn 中運行 LDA:如何計算 Wordcloud?

[英]Topic modeling - run LDA in sklearn : how to compute the Wordcloud?

我在sklearn 中訓練了我的 LDA 模型來構建主題模型,但不知道如何為每個獲得的主題計算關鍵字 Wordcloud?

這是我的 LDA 模型:

vectorizer = CountVectorizer(analyzer='word',       
                         min_df=3,                        
                         max_df=6000,
                         stop_words='english',             
                         lowercase=False,                   
                         token_pattern ='[a-zA-Z0-9]{3,}' 
                         max_features=50000,             
                        )
data_vectorized = vectorizer.fit_transform(data_lemmatized) # data_lemmatized is all my processed document text

best_lda_model = LatentDirichletAllocation(batch_size=128, doc_topic_prior=0.1,
                      evaluate_every=-1, learning_decay=0.7,
                      learning_method='online', learning_offset=10.0,
                      max_doc_update_iter=100, max_iter=10,
                      mean_change_tol=0.001, n_components=10, n_jobs=None,
                      perp_tol=0.1, random_state=None, topic_word_prior=0.1,
                      total_samples=1000000.0, verbose=0)

lda_output = best_lda_model.transform(data_vectorized)

我知道best_lda_model.components_給出了主題詞的權重... vectorizer.get_feature_names()給出了每個主題詞匯表中的所有詞...

提前謝謝了!

您必須遍歷模型“components_”,其大小為 [n_components, n_features],因此第一個維度包含主題,第二個維度包含詞匯表中每個單詞的分數。 因此,您首先需要找到與主題最相關的詞的索引,然后使用使用 get_features_names() 定義的“vocab”詞典,您可以檢索這些詞。

import numpy as np

# define vocabulary to get words names 
vocab = vectorizer.get_feature_names()

# dictionary to store words for each topic and number of words per topic to retrive 
words = {}
n_top_words = 10

for topic, component in enumerate(model.components_):

    # need [::-1] to sort the array in descending order
    indices = np.argsort(component)[::-1][:n_top_words]

    # store the words most relevant to the topic
    words[topic] = [vocab[i] for i in indices]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM