简体   繁体   中英

Gensim 1.0.1 on Python 3.5 TypeError: object of type 'map' has no len()?

I installed Python 3.5 using Anaconda and gensim 1.0.1 (supports Python 3) using pip. I got the following error when running gensim:

Exception in thread Thread-61:
Traceback (most recent call last):
  File "/Users/mac/anaconda/lib/python3.5/threading.py", line 914, in _bootstrap_inner
    self.run()
  File "/Users/mac/anaconda/lib/python3.5/threading.py", line 862, in run
    self._target(*self._args, **self._kwargs)
  File "/Users/mac/anaconda/lib/python3.5/site-packages/gensim/models/word2vec.py", line 838, in job_producer
    sentence_length = self._raw_word_count([sentence])
  File "/Users/mac/anaconda/lib/python3.5/site-packages/gensim/models/word2vec.py", line 755, in _raw_word_count
    return sum(len(sentence) for sentence in job)
  File "/Users/mac/anaconda/lib/python3.5/site-packages/gensim/models/word2vec.py", line 755, in <genexpr>
    return sum(len(sentence) for sentence in job)
TypeError: object of type 'map' has no len()

The code causing this error is from node2vec . I am porting it to Python 3 but got this error.

I know that in Python 3, len(map) causes error, does it mean Gensim 1.0.1 does not support Python 3 although pip website says it supports? Or are there some hidden settings?

Anyone has any idea what is wrong? Thanks.

Gensim supports Python 3, of course. It is your (or node2vec's) responsibility to supply Word2Vec() with an iterable of your sentences.

In this case, you have to pass it an iterable that contains walks - where each walk is a list of vertices:

walks = [list(map(str, walk)) for walk in walks] # convert each vertex id to a string
model = Word2Vec(walks, ...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM