首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在sklearn中获取NMF的主题频率

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

Stack Overflow用户
提问于 2018-08-04 00:23:01
回答 1查看 160关注 0票数 1

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

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2018-08-09 16:45:51

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

代码语言:javascript
复制
H = nmf.fit_transform(vec_matrix)

H是形状为(n_documents,n_topics)的矩阵。每行表示一个文档向量(在主题空间中)。在这个向量中,您可以找到每个主题所具有的权重(翻译为主题重要性)。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51676677

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档