pas*_*ion 4 python machine-learning lda topic-modeling
我正在使用此处的示例对文本数据执行LDA :我的问题是:
我如何知道哪些文档对应于哪个主题?
换句话说,例如,谈论主题1的文件是什么?
这是我的步骤:
n_features = 1000
n_topics = 8
n_top_words = 20
Run Code Online (Sandbox Code Playgroud)
我逐行阅读我的文本文件:
with open('dataset.txt', 'r') as data_file:
input_lines = [line.strip() for line in data_file.readlines()]
mydata = [line for line in input_lines]
Run Code Online (Sandbox Code Playgroud)
打印主题的功能:
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()
Run Code Online (Sandbox Code Playgroud)
对数据进行矢量化:
tf_vectorizer = CountVectorizer(max_df=0.95, min_df=2, token_pattern='\\b\\w{2,}\\w+\\b',
max_features=n_features,
stop_words='english')
tf = tf_vectorizer.fit_transform(mydata)
Run Code Online (Sandbox Code Playgroud)
初始化LDA:
lda = LatentDirichletAllocation(n_topics=3, max_iter=5,
learning_method='online',
learning_offset=50.,
random_state=0)
Run Code Online (Sandbox Code Playgroud)
在tf数据上运行LDA:
lda.fit(tf)
Run Code Online (Sandbox Code Playgroud)
使用上述功能打印结果:
print("\nTopics in LDA model:")
tf_feature_names = tf_vectorizer.get_feature_names()
print_top_words(lda, tf_feature_names, n_top_words)
Run Code Online (Sandbox Code Playgroud)
印刷品的输出是:
Topics in LDA model:
Topic #0:
solar road body lamp power battery energy beacon
Topic #1:
skin cosmetic hair extract dermatological aging production active
Topic #2:
cosmetic oil water agent block emulsion ingredients mixture
Run Code Online (Sandbox Code Playgroud)
Mar*_*cel 14
您需要对数据进行转换:
doc_topic = lda.transform(tf)
Run Code Online (Sandbox Code Playgroud)
并列出文档及其最高分主题,如下所示:
for n in range(doc_topic.shape[0]):
topic_most_pr = doc_topic[n].argmax()
print("doc: {} topic: {}\n".format(n,topic_most_pr))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3273 次 |
| 最近记录: |