1

我现在使用 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
4

1 回答 1

1

如果您的意思是每个文档中每个主题的频率,那么:

H = nmf.fit_transform(vec_matrix)

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

于 2018-08-09T08:45:50.563 回答