Search code examples
pythonnlpgensimword2vec

Python gensim create word2vec model from vectors (in ndarray)


I have a ndarray with words and their corresponding vector (with the size of 100 per word). For example:

Computer 0.11 0.41 ... 0.56
Ball     0.31 0.87 ... 0.32

And so on.

I want to create a word2vec model from it:

model = load_from_ndarray(arr)

How can it be done? I saw

KeyedVectors

but it only takes file and not array


Solution

  • from gensim.models import KeyedVectors
    words = myarray[:,0]
    vectors = myarray[:,1:]
    model = KeyedVectors(vectors.shape[1])
    model.add(words, vectors)
    

    if you want you can then save it

    model.save('mymodel')
    

    and later just load it

    model = KeyedVectors.load('mymodel')