title: How to efficiently transform a sparse word embedding matrix to gensim KeyedVectors object
body: <p>I have a large sparse word embedding matrix that is trained from sklearn tfidf which has nothing to do with the Gensim word2vec. </p>
  
  <p>It is very similar to: <a href="https://stackoverflow.com/questions/47959639/gensim-word2vec-transfer-learning-from-a-non-gensim-model">gensim Word2vec transfer learning (from a non-gensim model)</a> and <a href="https://stackoverflow.com/questions/46297740/how-to-turn-embeddings-loaded-in-a-pandas-dataframe-into-a-gensim-model">How to turn embeddings loaded in a Pandas DataFrame into a Gensim model?</a></p>
  
  <p>However, given that the matrix is very sparse, I would like to store them more memore efficiently and reload them by gensim KeyedVecotrs. Or create the KeyedVectors instance without saving the sparse matrix and then directly save the gensim word2vec object. </p>
  
  <p>Thanks. </p>
  
  <hr>
  
  <p>follow-up:</p>
  
  <p>I end up successfully doing in this way:</p>
  
  <pre><code>from collections import Counter, OrderedDict
  import gensim
  wc = Counter(corpus) # corpus = list of tokens in my documents
  vocab = OrderedDict()
  # building vocab for gensim
  for idx, word in enumerate(words):
      tmp = gensim.models.keyedvectors.Vocab()
      tmp.index = idx
      tmp.count = wc[word]
      vocab[word] = tmp
  w2v = gensim.models.keyedvectors.KeyedVectors()
  w2v.index2word = words
  w2v.vector_size = Xsparse.shape[1]
  w2v.syn0 = Xsparse.toarray()  # have to make the matrix dense here
  w2v.vocab = vocab
  w2v.save(...)
  </code></pre>
  
  <p>However, I have to make the Xsparse word embedding matrix dense. Not sure if there is any more efficient way. Thanks for any answer.</p>