Search code examples
python-2.7gensimword2vec

use a.all() or a.any() error while trying to use gensim word2vec


I've been trying to run an example of how t use word2vec from the gensim library of python but I keep getting this error

    ValueError: The truth value of an array with more than one element is   ambiguous. Use a.any() or a.all()

This is my code, it's just a simple example :

    from gensim.models import Word2Vec
    sentences = [['first', 'sentence'], ['second', 'sentence']]
    # train word2vec on the two sentences
    model = Word2Vec(sentences, min_count=1)

Note: I've made sure that gensim is installed with all its dependencies.


Solution

  • I had the same exact problem too, what i did was installing python-dev package then re-installing gensim, somehow that worked, i'm on ubuntu so this is what i did :

    sudo apt-get install python-dev
    sudo pip uninstall gensim
    sudo pip install gensim
    

    when i run this :

    model = gensim.models.Word2Vec(sentences=listSentence,min_count=2,window=3,size=20,workers=1)
    print model['Brasil']
    

    it worked and i got the result vector :

    [-0.01635483  0.02224622 -0.01865266  0.02168317 -0.01231722 -0.0207897
     -0.0014509   0.00264822 -0.01889374 -0.02109174 -0.00244757  0.00024959
     -0.00898884 -0.01826199 -0.01361686 -0.01770178 -0.02431025 -0.01903439
     -0.00775641  0.02353667]