Created
July 11, 2017 10:39
-
-
Save conormm/ca0cdf78fa7a91fdacf500ff4dff0645 to your computer and use it in GitHub Desktop.
Extracting word vectors from spaCy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import spacy | |
from sklearn.decomposition import PCA | |
nlp = spacy.load("en") | |
animals = "dog cat hamster lion tiger elephant cheetah monkey gorilla antelope rabbit mouse rat zoo home pet fluffy wild domesticated" | |
animal_tokens = nlp(animals) | |
animal_vectors = np.vstack([word.vector for word in animal_tokens if word.has_vector]) | |
pca = PCA(n_components=2) | |
animal_vecs_transformed = pca.fit_transform(animal_vectors) | |
animal_vecs_transformed = np.c_[animals.split(), animal_vecs_transformed] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment