1

Following the documentation of ?gensim.models.ldamodel, I want to train an ldamodel and (from this SO answer create a worcloud from it). I am using the following code from both sources:

from gensim.test.utils import common_texts
from gensim.corpora.dictionary import Dictionary
import gensim
import matplotlib.pyplot as plt
from wordcloud import WordCloud

common_dictionary = Dictionary(common_texts) # create corpus
common_corpus = [common_dictionary.doc2bow(text) for text in common_texts]

lda = gensim.models.LdaModel(common_corpus, num_topics=10) # train model on corpus
for t in range(lda.num_topics):
    plt.figure()
    plt.imshow(WordCloud().fit_words(lda.show_topic(t, 200)))
    plt.axis("off")
    plt.title("Topic #" + str(t))
    plt.show()

However, I get an AttributeError: 'list' object has no attribute 'items' on the line plt.imshow(...)

Can someone help me out here? (Answers to similar questions have not been working for me and I am trying to compile a minimal pipeline with this.)

user456789
  • 331
  • 1
  • 3
  • 9

1 Answers1

4

From the docs, the method WordCloud.fit_words() expects a dictionary as input.

Your error seems to highlight that it's looking for an attribute 'items', typically an attribute of dictionaries, but instead finds a list object.

So the problem is: lda.show_topic(t, 200) returns a list instead of a dictionary. Use dict() to cast it!

Finally:

plt.imshow(WordCloud().fit_words(dict(lda.show_topic(t, 200))))
arnaud
  • 3,293
  • 1
  • 10
  • 27