pythonvectorgensimword2vec

How to get a dump of all vectors from a gensim W2V model?


Using a KeyedVectors object, I can get the W2V vector, given a word, like so.

from gensim.models import KeyedVectors

model = KeyedVectors.load('vectors.kv')
model.get_vector('example')  # output => [0.12, 0.41, ..., 0.92]

How can I do the same, for every term (key) contained in the model?

Note that this doesn't have to be a KeyedVectors object, it could alternatively be a Word2Vec object.

EDIT - thanks to gojomo:

vector_dct = {}
for word in kv_model.index2word: 
    vector_dct[word] = kv_model.get_vector(word)

df = pd.DataFrame(vector_dct).T

Solution

  • for word in kv_model.index_to_key:  # was kv_model.index2word pre-gensim-4.0.0, when Q 1st asked
        kv_model.get_vector(word)