NameError: global name 'np' is not defined

752 views
Skip to first unread message

Vlad S.

unread,
May 13, 2015, 7:54:15 AM5/13/15
to gen...@googlegroups.com

top_topics of LDA's model return:

>>> lda.top_topics(tfidf_corpus)[:10]

Traceback (most recent call last):
  File "create_tfidf_mm_corpus.py", line 85, in <module>
    lda.top_topics(tfidf_corpus)[:10]
  File "/usr/local/lib/python2.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

Is this a known bug?

Christopher S. Corley

unread,
May 13, 2015, 11:36:15 AM5/13/15
to gensim

Hi,

Thanks for reporting! Yes, it's known and is fixed. Merge mistake :)

Sorry you ran into this problem. A temp fix might be to import numpy as np in your script.

Chris.

--
You received this message because you are subscribed to the Google Groups "gensim" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gensim+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Vlad S.

unread,
May 13, 2015, 3:22:46 PM5/13/15
to gen...@googlegroups.com
Thanks Chris. And, yeah, unfortunately importing as np doesn't help.

Christopher S. Corley

unread,
May 13, 2015, 5:12:55 PM5/13/15
to gensim
Ah, if it's urgent for your work, you could `import numpy as np` in /usr/local/lib/python2.7/site-packages/gensim/models/ldamodel.py directly :)

Radim Řehůřek

unread,
May 13, 2015, 5:18:42 PM5/13/15
to gen...@googlegroups.com, csco...@gmail.com
Importing the ldamodel module and doing `ldamodel.np = ldamodel.numpy` should work too.

Like Chris said, this has already been fixed -- the next release will not have this error.

Best,
Radim

> To unsubscribe from this group and stop receiving emails from it, send an email to gensim+unsubscribe@googlegroups.com.

Takatsugu Nokubi

unread,
Jun 1, 2015, 12:45:49 AM6/1/15
to gen...@googlegroups.com, csco...@gmail.com
I had a same issue and another problem like this on gensim-0.11.1.
I need to add the following on python2.7:

import itertools
gensim.models.ldamodel.ifilter = itertools.ifilter

I think it requires another solution on python3 because it do not have itertools.ifilter.

Then I tried to use LdaModel.top_topics(), but sometimes it causes KeyError:

2015-06-01 22:42:22,530 : INFO : loading LsiModel object from /home/knok/text/topic/lda/chatter.lsi.projection

Traceback (most recent call last):
  File "./lda.py", line 61, in <module>
    topics = lda.top_topics([vec])
  File "/usr/local/lib/python2.7/dist-packages/gensim-0.11.1_1-py2.7-linux-x86_64.egg/gensim/models/ldamodel.py", line 779, in top_topics
    doc_frequency_m = len(doc_word_list[word_m])
KeyError: 847

So I added a patch like the following:
--- ldamodel.py.old     2015-06-01 22:40:03.484170877 +0900
+++ ldamodel.py 2015-06-01 22:42:06.016170862 +0900
@@ -776,9 +776,14 @@
         for topic in xrange(len(topics)):
             topic_coherence_sum = 0.0
             for word_m in topics[topic][1:]:
-                doc_frequency_m = len(doc_word_list[word_m])
-                m_set = set(doc_word_list[word_m])
+               x = doc_word_list.get(word_m, None)
+               if x is None:
+                       continue
+                doc_frequency_m = len(x)
+                m_set = set(x)
                 for word_l in topics[topic][:-1]:
+                   if doc_word_list.get(word_l, None) is None:
+                       continue
                     l_set = set(doc_word_list[word_l])
                     co_doc_frequency = len(m_set.intersection(l_set))
                     topic_coherence_sum += numpy.log(
 
Is this way a correct solution? 
Reply all
Reply to author
Forward
0 new messages