如何在以下 LDA 模型中预测评论的主题?
How to predict the topic of a comment in the following LDA model?
你好我正在尝试对几小段文本的主题进行建模,语料库由来自社交网页的评论组成,我有以下结构首先是一个包含文档的列表如下:
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有什么方法可以自动给出topic的数量,在此先感谢支持。
首先,您选择研究的主题数量等于 n_topics (= 30)。
你得到的预测向量是一个 (30,) 形数组。每个分量代表评论属于 i-th 个主题的概率。
记住LDA不是排他性的,一个文档可以属于多个class。
例如在这里,我可以说您的评论属于 2 个不同的 classes,概率为 0.86 和 0.59
你好我正在尝试对几小段文本的主题进行建模,语料库由来自社交网页的评论组成,我有以下结构首先是一个包含文档的列表如下:
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有什么方法可以自动给出topic的数量,在此先感谢支持。
首先,您选择研究的主题数量等于 n_topics (= 30)。 你得到的预测向量是一个 (30,) 形数组。每个分量代表评论属于 i-th 个主题的概率。
记住LDA不是排他性的,一个文档可以属于多个class。 例如在这里,我可以说您的评论属于 2 个不同的 classes,概率为 0.86 和 0.59