Как я могу увидеть все документы по теме в LDA?

1

Я использую LDA, чтобы узнать темы отличного текста. Мне удалось распечатать темы, но я хотел бы напечатать каждый текст с вашей темой.

Данные:

it very hot outside summer
there are not many flowers in winter
in the winter we eat hot food
in the summer we go to the sea
in winter we used many clothes
in summer we are on vacation
winter and summer are two seasons of the year

Я попытался использовать sklearn, и я могу печатать темы, но я хотел бы напечатать все фразы, принадлежащие каждой теме

from sklearn.feature_extraction.text import CountVectorizer
from sklearn.decomposition import LatentDirichletAllocation
import numpy as np
import pandas

dataset = pandas.read_csv('data.csv', encoding = 'utf-8')
comments = dataset['comments']
comments_list = comments.values.tolist()

vect = CountVectorizer()
X = vect.fit_transform(comments_list)

lda = LatentDirichletAllocation(n_topics = 2, learning_method = "batch", max_iter = 25, random_state = 0)

document_topics = lda.fit_transform(X)

sorting = np.argsort(lda.components_, axis = 1)[:, ::-1]
feature_names = np.array(vect.get_feature_names())

docs = np.argsort(comments_list[:, 1])[::-1]
for i in docs[:4]:
    print(' '.join(i) + '\n')

Хорошая производительность:

Topic 1
it very hot outside summer
in the summer we go to the sea
in summer we are on vacation
winter and summer are two seasons of the year

Topic 2
there are not many flowers in winter
in the winter we eat hot food
in winter we used many clothes
winter and summer are two seasons of the year
  • 2
    У вас есть документы и для каждого документа document_topic. Так что просто переберите вашу переменную document_topics и сохраните тему и индексы, например, со словарем.
  • 0
    Спасибо @Norhther, так что я должен сделать: для меня в document_topics ?
Показать ещё 2 комментария
Теги:
python-3.x
scikit-learn
topic-modeling
lda

1 ответ

1
Лучший ответ

Если вы хотите распечатать документы, вам необходимо указать их.

print(" ".join(comments_list[i].split(",")[:2]) + "\n")

Ещё вопросы

Сообщество Overcoder
Наверх
Меню