Using PCA to help visualize Word-Embeddings — Sklearn, Matplotlib

Quick, simple write up on using PCA to reduce word-embedding dimensions down to 2D so we visualize them in a scatter plot.

1. Setup

import matplotlib.pyplot as plt
import gensim.downloader as api
from sklearn.decomposition import PCA

2. Pull Embeddings

embeddings = [ transformer.wv[term] for term in terms ]

3. Run PCA

pca = PCA(n_components=2)
data = pca.fit_transform(embeddings).transpose()

4. Visualize

fig, ax = plt.subplots(figsize=(15, 8))

Focused on generating original, compelling, short stories through the use of Artificial Intelligence.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store