我怎么知道这个词进入哪个主题?

时间:2019-02-14 06:22:32

标签: python-3.x machine-learning nlp

此代码可以正常工作,但我想知道主题名称,而不是主题:0和主题:1,我怎么知道这个词来自哪个主题?

for index, topic in lda_model.show_topics(formatted=False, num_words= 30):
        print('Topic: {} \nWords: {}'.format(idx, [w[0] for w in topic]))

这是输出

Topic: 0 
Words: ['associate', 'incident', 'time', 'task', 'pain', 'amcare', 'work', 'ppe', 'train', 'proper', 'report', 'standard', 'pmv', 'level', 'perform', 'wear', 'date', 'factor', 'overtime', 'location', 'area', 'yes', 'new', 'treatment', 'start', 'stretch', 'assign', 'condition', 'participate', 'environmental']
Topic: 1 
Words: ['work', 'associate', 'cage', 'aid', 'shift', 'leave', 'area', 'eye', 'incident', 'aider', 'hit', 'pit', 'manager', 'return', 'start', 'continue', 'pick', 'call', 'come', 'right', 'take', 'report', 'lead', 'break', 'paramedic', 'receive', 'get', 'inform', 'room', 'head']

我想要“主题名称”而不是主题:0

Topic: 0 
Words: ['associate', 'incident', 'time', 'task', 'pain', 'amcare', 'work', 'ppe', 'train', 'proper', 'report', 'standard', 'pmv', 'level', 'perform', 'wear', 'date', 'factor', 'overtime', 'location', 'area', 'yes', 'new', 'treatment', 'start', 'stretch', 'assign', 'condition', 'participate', 'environmental']
Topic: 1 
Words: ['work', 'associate', 'cage', 'aid', 'shift', 'leave', 'area', 'eye', 'incident', 'aider', 'hit', 'pit', 'manager', 'return', 'start', 'continue', 'pick', 'call', 'come', 'right', 'take', 'report', 'lead', 'break', 'paramedic', 'receive', 'get', 'inform', 'room', 'head']

2 个答案:

答案 0 :(得分:0)

这可能有效(未经测试)

for index, topic in lda_model.show_topics(formatted=False, num_words= 30):
        print('Topic: {} \nWords: {}'.format(lda_model.print_topic(index), [w[0] for w in topic]))

答案 1 :(得分:0)

尝试像这样将Formatted参数更改为True

for index, topic in lda_model.show_topics(formatted=True, num_words= 30):
        print('Topic: {} \nWords: {}'.format(topic[0], [w[0] for w in topic[1]]))

您也可以查看文档以获取更多信息: https://radimrehurek.com/gensim/models/ldamodel.html