top_topics Gensim NameError: global name 'np' is not defined

1.8k Views Asked by At

I am running (from Gensim)

top_topics(corpus, num_topics=5, num_words=20)

I get the error:

for topic in lda.top_topics(corpus=corpus, num_topics=5, num_words=20):
File "/Library/Python/2.7/site-packages/gensim/models/ldamodel.py", line 760, in top_topics
bestn = np.argsort(topic)[::-1][:num_words]
NameError: global name 'np' is not defined

I thought this was odd and I can see the file (indeed it has import numpy but not import numpy as np).

After adding import numpy as np and trying to run it again:

  File "/Library/Python/2.7/site-packages/gensim/models/ldamodel.py", line 772, in top_topics
    if len(list(ifilter(lambda x: x[0] == id,corpus[document]))) > 0:

NameError: global name 'ifilter' is not defined

Which was fixed by changing from itertools import chain to from itertools import chain, ifilter. The module then works perfectly.

So, I guess my question is whether this was an error specific to my system (is there some kind of 'import all' python trick that doesn't work for me?).

1

There are 1 best solutions below

0
On

Try upgrade your numpy, if you are using pip use pip install --upgrade numpy

for safety, also try to upgrade you gensim