Skip to content

Instantly share code, notes, and snippets.

@sksq96
Forked from tylerneylon/wordvec_example.py
Created January 13, 2018 00:25
Show Gist options
  • Save sksq96/0ac1e143cdc5e46678309ffd14ce8450 to your computer and use it in GitHub Desktop.
Save sksq96/0ac1e143cdc5e46678309ffd14ce8450 to your computer and use it in GitHub Desktop.
Quick reference on how to work with pre-trained word2vec vectors in Python.
# wordvec_example.py
#
# This file shows one way to work with word2vec data in Python.
#
# Setup:
#
# 1. Install gensim:
#
# pip install gensim
#
# 2. Download
#
# Use your favorite download tool (eg curl, wget, your browser) to d/l from:
# https://drive.google.com/file/d/0B7XkCwpI5KDYNlNUTTlSS21pQmM/edit?usp=sharing
#
# That ought to give you a file called GoogleNews-vectors-negative300.bin, which
# you can put anywhere you like; this code assumes it lives in '~/Downloads'.
#
import gensim
filepath = '~/Downloads/GoogleNews-vectors-negative300.bin'
model = gensim.models.KeyedVectors.load_word2vec_format(filepath, binary=True)
# As an example, print out words similar to 'chicken':
print(model.most_similar(positive=['chicken']))
# Docs covering some methods of `model` are here:
# https://radimrehurek.com/gensim/models/keyedvectors.html#gensim.models.keyedvectors.EuclideanKeyedVectors
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment