如何在sklearn中获得NMF主题的频率

时间:2018-08-03 16:23:01

标签: scikit-learn nmf

我现在正在使用NMF生成主题。我的代码如下所示。但是,我不知道如何获得每个主题的频率。有谁能帮助我吗?谢谢!

def fit_tfidf(documents):
    tfidf = TfidfVectorizer(input = 'content', stop_words = 'english', 
use_idf = True, ngram_range = NGRAM_RANGE,lowercase = True, max_features =  MAX_FEATURES, min_df = 1 )
    tfidf_matrix = tfidf.fit_transform(documents.values).toarray()
    tfidf_feature_names = np.array(tfidf.get_feature_names())
    tfidf_reverse_lookup = {word: idx for idx, word in enumerate(tfidf_feature_names)}
    return tfidf_matrix, tfidf_reverse_lookup, tfidf_feature_names

def vectorization(documments):
    if VECTORIZER == 'tfidf':
        vec_matrix, vec_reverse_lookup, vec_feature_names = fit_tfidf(documents) 
    if VECTORIZER == 'bow':
        vec_matrix, vec_reverse_lookup, vec_feature_names = fit_bow(documents)
    return vec_matrix, vec_reverse_lookup, vec_feature_names

def nmf_model(vec_matrix, vec_reverse_lookup, vec_feature_names, NUM_TOPICS):
    topic_words = []
    nmf = NMF(n_components = NUM_TOPICS, random_state=3).fit(vec_matrix)
    for topic in nmf.components_:
        word_idx = np.argsort(topic)[::-1][0:N_TOPIC_WORDS]
        topic_words.append([vec_feature_names[i] for i in word_idx])
    return topic_words

1 个答案:

答案 0 :(得分:0)

如果您是指每个文档中每个主题的出现频率,那么:

H = nmf.fit_transform(vec_matrix)

H是形状的矩阵(n_documents,n_topics)。每行代表一个文档向量(在主题空间中)。在此向量中,您可以找到每个主题的权重(转换为主题重要性)。