Python-LDAトピックモデル
潜在ディリクレ配分(Latent Dirichlet Allocation)
ラベルなしのテキストにトピックを割り当てます。
教師なし学習になります。
Sklearn
import pandas as pd
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.decomposition import LatentDirichletAllocation
#==== LDAトピックモデル ====
#n_componentsでトピック数を指定する
lda = LatentDirichletAllocation(n_components=10,
random_state=2020,
learning_method='online')
lda = lda.fit(X)
X_topics = lda.transform(X)
#==== トピックとトップワード ====
n_top_words = 5
feature_names = X.columns
for topic_idx, topic in enumerate(lda.components_):
print(f"Topic: {topic_idx+1}")
print(" ".join([feature_names[i] for i in topic.argsort()[:-n_top_words - 1: -1]]))
