title: Matching words and vectors in gensim Word2Vec model
body: <p>I have had the <a href="https://radimrehurek.com/gensim/" rel="noreferrer">gensim</a> <a href="https://radimrehurek.com/gensim/models/word2vec.html#gensim.models.word2vec.Word2Vec" rel="noreferrer">Word2Vec</a> implementation compute some word embeddings for me. Everything went quite fantastically as far as I can tell; now I am clustering the word vectors created, hoping to get some semantic groupings.</p>
  <p>As a next step, I would like to look at the words (rather than the vectors) contained in each cluster. I.e. if I have the vector of embeddings <code>[x, y, z]</code>, I would like to find out which actual word this vector represents. I can get the words/Vocab items by calling <code>model.vocab</code> and the word vectors through <code>model.syn0</code>. But I could not find a location where these are explicitly matched.</p>
  <p>This was more complicated than I expected and I feel I might be missing the obvious way of doing it. Any help is appreciated!</p>
  <h3>Problem:</h3>
  <p>Match words to embedding vectors created by <code>Word2Vec ()</code> -- how do I do it?</p>
  <h3>My approach:</h3>
  <p>After creating the model (code below*), I would now like to match the indexes assigned to each word (during the <code>build_vocab()</code> phase) to the vector matrix outputted as <code>model.syn0</code>.
  Thus</p>
  <pre><code>for i in range (0, newmod.syn0.shape[0]): #iterate over all words in model
      print i
      word= [k for k in newmod.vocab if newmod.vocab[k].__dict__['index']==i] #get the word out of the internal dicationary by its index
      wordvector= newmod.syn0[i] #get the vector with the corresponding index
      print wordvector == newmod[word] #testing: compare result of looking up the word in the model -- this prints True
  </code></pre>
  <ul>
  <li><p>Is there a better way of doing this, e.g. by feeding the vector into the model to match the word?</p>
  </li>
  <li><p>Does this even get me correct results?</p>
  </li>
  </ul>
  <p>*My code to create the word vectors:</p>
  <pre><code>model = Word2Vec(size=1000, min_count=5, workers=4, sg=1)
          
  model.build_vocab(sentencefeeder(folderlist)) #sentencefeeder puts out sentences as lists of strings
  
  model.save(&quot;newmodel&quot;)
  </code></pre>
  <p>I found <a href="https://stackoverflow.com/questions/35914287/word2vec-how-to-get-words-from-vectors">this question</a> which is similar but has not really been answered.</p>