2

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.)

1
  • How does lda.show_topic(t, 200) look like?
    – arnaud
    Commented Apr 2, 2020 at 14:43

1 Answer 1

6

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))))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.