【发布时间】: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