Learn how PCA and t-SNE draw word embeddings in two dimensions while preserving only part of the original neighbourhoods.
Theo wants to show a small word-vector model on one slide. A scatterplot feels like a map, and maps invite stories.
This picture needs a warning label. Each word begins as 50 numbers. A two-dimensional drawing can keep some nearby neighbours and lose others.
Word embedding visualization draws word vectors as points by projecting many numeric dimensions into two. The plot is a question to check, not a finding about topics, clusters, or meaning.
TipWhat you will learn
This lesson shows how to:
verify the pinned inaugural word2vec model before loading it;
L2-normalize rows so Euclidean projection distances match cosine neighbours;
select a stated subset of frequent words;
compare PCA with t-SNE using 10-nearest-neighbour preservation;
check two t-SNE seeds instead of trusting one run; and
avoid reading clusters, gaps, sizes, and axis directions as evidence.
Load and verify the pinned model
The model comes from lesson 61. It is deliberately small: the metadata records 134,419 cleaned training tokens and a 50-dimensional skip-gram word2vec fit. In lesson 61, even the easy pair freedom / liberty landed below the median of all pairwise similarities, so this lesson measures whether the picture keeps the model’s neighbours. It does not claim the model learned reliable meaning.
The fingerprint check fails closed if the binary changes. Rendering does not train, download, or update a model.
Choose the plotted words
The full saved vocabulary has 2,709 rows, including the trainer’s </s> marker. This page plots the 300 most frequent training words that also have vectors. Changing the subset can change the projection.
kable( subset_summary,col.names =c("Subset check", "Value"),caption ="Projection subset for this lesson",row.names =FALSE)
Projection subset for this lesson
Subset check
Value
plotted words
300
minimum cleaned count
49
maximum cleaned count
10003
L2 normalization makes each row length equal to 1. On unit-length rows, Euclidean distance ranks neighbours the same way as cosine similarity, the measure used in lesson 61. The count column is an approximate training count rebuilt from the cleaned paragraphs with tidytext’s tokenizer, not a byte-for-byte count from word2vec’s internal splitter.
Project with PCA
PCA, short for principal component analysis, finds directions with the most spread in the normalized vectors. The table below reports how much variance the first two components hold. The rest is invisible in this plot.
Figure 1: PCA projection shown as one panel per anchor word. In each panel, faint dots are the other 289 plotted words, squares are that anchor’s 10 nearest neighbours in the full 50-dimensional vectors, and the large triangle is the anchor word.
PCA axes can flip sign across software builds, so the code fixes the signs with named anchor words before plotting. This lesson does not interpret left, right, up, or down. Each anchor word gets its own panel in both projection plots. The facet strip names the anchor, the large triangle marks the anchor, and squares mark all 10 of its nearest neighbours in the full vector space.
Project with t-SNE
t-SNE is a stochastic method that tries to keep local neighbours together in the drawing. It can make groups and gaps that are not stable evidence. Here, perplexity 30 is a t-SNE setting for roughly how many neighbours each point considers; it is unrelated to the language-model perplexity in lessons 63 and 64. Theta 0.5 trades a little accuracy for speed, and 750 iterations is the number of optimization updates. The run also uses seed 7801, no initial PCA step, no extra normalization, and one thread.
Figure 2: t-SNE projection shown as one panel per anchor word, using seed 7801. In each panel, faint dots are the other 289 plotted words, squares are that anchor’s 10 nearest neighbours in the full 50-dimensional vectors, and the large triangle is the anchor word.
The panels are landmarks so the two pictures can be matched by eye. They are not cluster names. Squares mark all 10 full-space neighbours; where they land shows whether the projection kept them near the anchor. The table below lists every anchor word.
Measure neighbour preservation
A 10-nearest-neighbour preservation score asks: for each word, what share of its 10 nearest neighbours in the full 50-dimensional normalized vectors also appear among its 10 nearest neighbours in the two-dimensional drawing? Chance is 10 divided by 299, or 3.3%.
Neighbour preservation for two-dimensional projections
Comparison
Average 10-neighbour overlap
PCA versus full-space neighbours
17.5%
t-SNE seed 7801 versus full-space neighbours
43.1%
t-SNE seed 7802 versus full-space neighbours
42.6%
t-SNE seed 7801 versus t-SNE seed 7802
50.8%
chance level for a random 10-word set
3.3%
The measured result is modest but useful. PCA keeps about 17.5% of each word’s 10 neighbours. The plotted t-SNE run keeps about 43.1%, and the second t-SNE seed keeps about 42.6%. The two t-SNE runs agree with each other on about 50.8% of neighbours. That is well above chance, but it is not perfect stability.
Inspect labelled words as rows
The table below is the text alternative for the labelled points. It reports neighbours from the full vector space and from the two plotted projections, without asking the reader to infer them from point positions.
neighbour_table <-tibble(word = label_words) |>rowwise() |>mutate(full_space_neighbours =paste(full_space_neighbours[word, 1:5], collapse =", "),pca_neighbours =paste(pca_neighbours[word, 1:5], collapse =", "),tsne_neighbours =paste(tsne_neighbours_7801[word, 1:5], collapse =", ") ) |>ungroup()kable( neighbour_table,col.names =c("Labelled word","Nearest in 50 dimensions","Nearest in PCA plot","Nearest in t-SNE plot" ),caption ="Neighbour lists for the labelled words in the two projection figures",row.names =FALSE)
Neighbour lists for the labelled words in the two projection figures
authority, executive, president, constitutional, congress
executive, duty, constitutional, united, proper
states, executive, authority, laws, powers
war
progress, still, force, old, yet
good, end, seek, greater, men
yet, made, end, progress, through
america
democracy, history, world, yet, hope
we, done, us, work, nothing
history, democracy, experience, way, spirit
If a nearby label looks interesting, the next step is to inspect the underlying texts or the full-space neighbour table. The picture alone is not evidence that the words form a topic.
What this does not show
The t-SNE plot does not give meaningful cluster sizes, gap sizes, group positions, or axis directions. PCA directions are mathematical summaries, but their signs can flip, and this plot still leaves most variance out.
The preservation scores are about the projection’s fidelity to this small model, not about fidelity to meaning. Small-corpus embeddings can change their neighbour lists when trained again, which is why this model is pinned. The same t-SNE seed can also draw a different layout on another computer because low-level numerical details can differ.
What to remember
Each plotted point began as a 50-number word vector.
Rows were L2-normalized before PCA and t-SNE so neighbours match cosine geometry.
PCA’s first two components explain about 23.9% of variance here.
t-SNE preserves more local neighbours than PCA in this run, but two seeds still disagree on many neighbours.
Do not interpret clusters, gaps, sizes, positions, or axis directions as findings.
Keep the scatterplot beside the preservation table. The table says what the picture kept.