【问题标题】:How to predict the topic of a comment in the following LDA model?如何在以下 LDA 模型中预测评论的主题?
【发布时间】:2017-05-07 15:38:13
【问题描述】:

您好,我正在尝试对几个小文本的主题进行模型,语料库是由社交网页中的 cmets 组成的,我有以下结构,首先是一个包含以下文档的列表:

listComments = ["I like the post", "I hate to use this smartphoneee","iPhone 7 now has the best performance and battery life :)",...]


tfidf_vectorizer = TfidfVectorizer(min_df=10,ngram_range=(1,3),analyzer='word')
tfidf = tfidf_vectorizer.fit_transform(listComments)

我使用 tfidf 生成具有该参数的模型,然后我使用 LDA 如下:

#Using Latent Dirichlet Allocation
n_topics = 30
n_top_words = 20
lda = LatentDirichletAllocation(n_topics=n_topics,
                                learning_method='online',
                                learning_offset=50.,
                                random_state=0)

lda.fit(tfidf)
def print_top_words(model, feature_names, n_top_words):
    for topic_idx, topic in enumerate(model.components_):
        print("Topic #%d:" % topic_idx)
        print(" ".join([feature_names[i]
                        for i in topic.argsort()[:-n_top_words - 1:-1]]))
    print()
print("\nTopics in LDA model:")
tf_feature_names = tfidf_vectorizer.get_feature_names()
print_top_words(lda, tf_feature_names, n_top_words)
y_pred = lda.fit_transform(tfidf)

然后我保存了两个模型 tfidf 和 LDA 来开发以下实验,给出一个新的注释我用相同的模型对其进行矢量化

comment = ['the car is blue']

x = tdf.transform(comment)

y = lda.transform(x)

print("this is the prediction",y)

我得到了:

this is the prediction [[ 0.03333333  0.03333333  0.03333333  0.03333333  0.03333333  0.03333333
   0.03333333  0.03333333  0.03333333  0.03333333  0.03333333  0.03333333
   0.03333333  0.03333333  0.03333333  0.03333333  0.59419197  0.03333333
   0.03333333  0.03333333  0.03333333  0.03333333  0.03333333  0.03333333
   0.03333333  0.03333333  0.03333333  0.86124492  0.03333333  0.03333333]]

我不明白我正在研究的这个向量,我不确定,但我相信它是由成为 n_topics 一部分的概率组成的,我使用的意思是 30,对于这种情况,我的新评论会有更多属于具有较高分量的主题的概率,但这不是很直接,我的主要问题是我是否需要构造一个方法来给出这个变换的较高分量的索引来分类一个向量,或者 LDA 是否有一些方法自动给出题目编号,先谢谢支持。

【问题讨论】:

    标签: scikit-learn lda


    【解决方案1】:

    首先,您选择查看数量等于 n_topics (= 30) 的主题。 你得到的预测向量是一个 (30,) 形状的数组。每个分量代表评论属于第 i 个主题的概率。

    请记住,LDA 不是独占的,一个文档可以属于多个类。 例如在这里,我可以说您的评论属于 2 个不同的类别,概率分别为 0.86 和 0.59

    【讨论】:

    • 非常感谢您的支持
    猜你喜欢
    • 2015-12-26
    • 2013-04-13
    • 2012-03-25
    • 1970-01-01
    • 2012-05-16
    • 2012-09-13
    • 2021-05-12
    • 2018-05-26
    • 2020-08-05
    相关资源
    最近更新 更多