Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 316
okuoub Avatar asked Jan 25 '26 08:01

okuoub


1 Answers

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')
like image 160
o17t H1H' S'k Avatar answered Jan 27 '26 22:01

o17t H1H' S'k