gensim show_topics get topic 1

gensim show_topics get topic

# based on the answer given by user in the source link

from gensim.models import LdaModel

# lda = LdaModel(...)
# ... after training ...
x = lda.show_topics(num_topics=12, num_words=5, formatted=False)

# tp[0]: topic
# tp[1]: weight of the word for the corresponding topic
topics_words = [(tp[0], tp[1]) for tp in x]

Here is what the above code is Doing:
1. x is a list of tuples, where each tuple is a topic and its corresponding weights.
2. The first element of each tuple is a list of tuples, where each tuple is a word and its corresponding weight.
3. The second element of each tuple is a list of weights.
4. The first element of each tuple in the first element of each tuple is a word.
5. The second element of each tuple in the first element of each tuple is a weight.

Similar Posts