t-SNE and UMAP for visualization

t-SNE and UMAP for Visualization: See the Shape PCA Can’t Show You

Picture a crowded room full of people from ten different friend groups. Now picture trying to describe that room using only a single photograph. You would capture some of the picture. However, you would lose most of the friendships, the little clusters, and the quiet corners where similar people gathered together.

That is exactly what happens when you compress data with PCA alone. PCA gives you a flat, honest photograph. It keeps the overall spread intact. Yet it cannot bend around curves, and it cannot preserve tight local friendships the way your eyes naturally would. That is where t-SNE and UMAP for visualization step in, and that is the exact gap this episode of the Intelevo series closes.

This article follows Episode 81, t-SNE & UMAP for Visualization, from the Intelevo YouTube channel. If you have not watched it yet, the video walks through every idea below with visuals, animations, and a live code demo. Consider this article your companion — the place to pause, re-read, and take notes at your own pace.

Why PCA Alone Isn’t Enough

In Episode 80, you learned how PCA compresses many correlated features into a small set of components. PCA finds the direction of maximum spread and rotates your data toward it. As a result, you get fewer columns, and you keep most of the signal.

However, PCA has one quiet limitation. It only draws straight lines through your data. Consequently, if your data curves, twists, or clusters in complicated ways, a straight-line projection misses that shape entirely. Two points can sit close together in the real, high-dimensional world, yet land far apart after PCA squashes everything flat.

This is precisely the problem t-SNE and UMAP solve. Instead of preserving overall spread, they preserve local neighborhoods. In other words, if two points were close before, these tools work hard to keep them close afterward too, even if that means bending the space to make it happen.

The Big Idea: A Party Planner, Not a Photographer

Here is the analogy from the video, and it sticks. PCA behaves like a camera. It finds one flat angle and takes a single honest photograph of your data. t-SNE, on the other hand, behaves like a party planner.

A party planner does not care about exact distances between guests. Instead, a party planner cares about one thing only: who was standing next to whom. Once the planner knows the real friend groups, they rebuild an entirely new room. In that new room, the same friends end up standing together again, even though the room itself is a different shape.

Translate that back to data. Every extra feature in your dataset adds another hallway to the party. Consequently, nobody can see the whole gathering at once from any single angle. So, instead of taking one photograph, t-SNE walks around the whole party, notices who stands near whom, and then redraws a brand-new, smaller room in 2D. In that new room, those same friend groups reappear, clearly and visibly.

This trade-off matters. t-SNE and UMAP sacrifice exact distance in exchange for genuine company. Near points stay near. Far points simply mean “not part of this group” — nothing more precise than that.

One Sentence That Covers the Whole Idea

If you remember nothing else from this article, remember this: t-SNE and UMAP take points that sit close together in many dimensions, and they place those same points close together in two dimensions, even if that means warping distances elsewhere.

Three ideas flow directly from that one sentence.

First, these tools preserve true neighbors. Even along curved or twisted structure, genuine neighbors stay neighbors. A straight-line method like PCA would flatten that curve and lose it.

Second, these tools distort on purpose. Global distance and cluster size get warped deliberately. Therefore, you should never read too much into how far apart two clusters land on the final plot.

Third, these tools reveal structure that PCA hides. Clusters that stayed completely invisible in a flat PCA plot often pop out clearly, simply because the space is finally allowed to bend.

Meet the Dataset: A Hidden Grouping

Let’s bring back the housing dataset from Episode 80. It has six familiar columns: square footage, bedrooms, bathrooms, garage size, lot size, and age.

This time, add one twist. Suppose these houses actually come from four different neighborhoods. However, that label was never given to the model. In a plain spreadsheet, this grouping stays completely invisible. Rows and columns alone cannot show you a hidden cluster.

So, the goal becomes clear: flatten this six-column dataset down to two dimensions, and see whether that hidden neighborhood grouping appears naturally, all on its own. Recall that PCA compressed these same six columns in the previous episode. Yet PCA blurred any neighborhood grouping, because it optimizes for overall spread, not for local neighborhoods.

The Pipeline: Four Steps, Every Single Time

t-SNE always follows the same four-step shape, regardless of the dataset.

Step one: measure closeness. For every pair of points, t-SNE measures how close they sit in the original high-dimensional space.

Step two: convert to probability. Next, t-SNE turns those raw distances into a probability. Essentially, it asks: how likely is this point to be that other point’s neighbor?

Step three: start from randomness. Then, t-SNE scatters every point randomly across a blank 2D page. This random scatter becomes the rough starting layout.

Step four: nudge until it matches. Finally, t-SNE repeatedly nudges points closer together or further apart. It keeps adjusting until the 2D neighbor probabilities match the original high-dimensional probabilities as closely as possible.

In short, picture an iterative tug-of-war. Points pull toward their true neighbors, and simultaneously, they drift away from strangers. Round after round, the picture sharpens.

Seeing It in Code: Running t-SNE

Theory only goes so far, so let’s write actual code. First, import TSNE from sklearn.manifold, and run it on your already-scaled features.

from sklearn.manifold import TSNE

tsne = TSNE(n_components=2, perplexity=30,
            random_state=42)
X_tsne = tsne.fit_transform(X_scaled)

Notice something important here. Unlike PCA, there is no separate .transform() step. Instead, fit_transform runs the entire tug-of-war process in one call and hands you 2D coordinates directly.

The perplexity parameter deserves a closer look. Think of it as roughly “how many close friends each point considers” while building its neighbor list. Small datasets suit a small perplexity, somewhere between 5 and 15. Larger datasets, meanwhile, usually work better with a perplexity between 30 and 50.

Once you have your 2D coordinates, plot them and color by the true neighborhood label, purely to check your work.

import matplotlib.pyplot as plt

plt.scatter(X_tsne[:, 0], X_tsne[:, 1],
            c=neighborhood, cmap="tab10")
plt.xlabel("t-SNE 1")
plt.ylabel("t-SNE 2")

Here is a subtlety worth remembering. The axes, “t-SNE 1” and “t-SNE 2,” carry no inherent meaning of their own. Only relative position matters — in other words, who ends up near whom on the page. Additionally, if you rerun this with a different random_state, the picture might rotate or flip entirely. Even so, the groupings themselves should stay stable.

Trying UMAP: A Faster Companion Tool

UMAP solves a similar problem, though it takes a slightly different mathematical path to get there. Fortunately, the code looks just as approachable.

import umap

reducer = umap.UMAP(n_neighbors=15, min_dist=0.1,
                     random_state=42)
X_umap = reducer.fit_transform(X_scaled)

Here, n_neighbors plays a role similar to t-SNE’s perplexity. It defines how many nearby points count as “local” for any given point. Meanwhile, min_dist controls how tightly points can pack together inside a cluster on the final page.

In practice, UMAP typically runs noticeably faster than t-SNE. Additionally, it tends to preserve more of the global layout between clusters, which brings us to a direct comparison between the two tools.

The Math in One Idea

You do not need heavy math to use these tools well. Even so, one clean idea sits underneath everything, and it helps to name it.

Three quantities matter here. First, P represents high-dimensional closeness. For every pair of points, P is a probability that one point is the other’s neighbor, based on their real distance in the original space.

Second, Q represents the same kind of probability, except measured from the current 2D layout — the exact layout currently being adjusted.

Third, the entire algorithm’s job reduces to one simple goal: nudge the 2D layout, step by step, until Q lines up with P as closely as possible. Formally, this process minimizes the KL divergence between P and Q, written as minimize KL(P ‖ Q).

That’s genuinely it. No new arithmetic to memorize. Just remember one phrase: compare neighbor-probabilities, then adjust until they agree.

t-SNE vs. UMAP: Which Tool Should You Reach For?

Both tools share the same underlying goal. However, they differ in three practical ways worth knowing before your next project.

Speed. UMAP scales comfortably to much larger datasets. Consequently, it typically runs noticeably faster than t-SNE, especially as your row count grows into the tens of thousands.

Global shape. UMAP tends to preserve more of the overall layout between clusters. t-SNE, in contrast, focuses almost entirely on local neighbors and cares less about the bigger picture.

Best use case. Reach for t-SNE when you want a polished, publication-style cluster plot. Reach for UMAP instead when you need speed, or when you plan to feed that 2D output into another downstream model.

Why This Actually Matters

A 2D map you can genuinely look at often reveals things a table of numbers never will. Here are four concrete reasons this skill pays off in real projects.

First, it confirms hidden groups. Clusters that appear in the plot either back up, or directly challenge, any labels or segments you already suspected existed.

Second, it flags mislabeled data. A point sitting inside the wrong-colored cluster deserves a second look. Frequently, that point turns out to be mislabeled.

Third, it guides feature work. Seeing which features separate your clusters points you directly toward what to engineer or collect next.

Fourth, it explains models to others. A 2D picture communicates “the model found real groups” far better than a table of abstract components ever could, especially to non-technical stakeholders.

Three Mistakes That Quietly Mislead

Both t-SNE and UMAP will run happily, even when you misread their output. Watch for these three traps.

Trusting between-cluster distance. The gap between two clusters on the page tells you nothing reliable about how genuinely different those groups are. Do not read too much into it.

Trusting cluster size. A cluster’s visual size on the plot mostly reflects the algorithm’s settings, not the true number of real-world points it represents.

Skipping standardization. Unscaled features distort neighbor-finding here, exactly as they did back in PCA. Therefore, always scale your features first, every single time, before running either tool.

What You’ll Remember Tomorrow

Let’s bring everything together into one clean recap.

t-SNE and UMAP both do one thing, at their core: they keep true neighbors close together on a page, even if that page bends the truth elsewhere to make it happen.

Standardize first, since scale still distorts neighbor-finding. Local distance stays trustworthy; between-cluster distance and cluster size do not. The perplexity parameter in t-SNE and the n_neighbors parameter in UMAP both roughly mean the same thing: how many close friends per point. Finally, as a simple rule of thumb, reach for UMAP when you need speed and structure at scale, and reach for t-SNE when you want the tightest possible local detail.

Frequently Asked Questions

Is t-SNE better than UMAP? Neither tool is universally better. t-SNE often produces slightly tighter, more polished local clusters for smaller datasets. UMAP, however, runs faster and scales better to large datasets, while also preserving more global structure.

Can I use t-SNE or UMAP as a preprocessing step before another model? Yes, though carefully. UMAP is generally the safer choice for this, since it preserves more global structure. Even so, treat the 2D output as a visualization and exploration tool first, not a guaranteed performance booster for every downstream model.

Do I need to standardize my data before t-SNE or UMAP? Yes, always. Both tools measure distance directly. Consequently, unscaled features with large numeric ranges will dominate the neighbor-finding process, exactly like they would with PCA.

Why does my t-SNE plot look different every time I run it? t-SNE starts from a random initial layout. Therefore, without a fixed random_state, each run can produce a rotated or flipped version of the same underlying structure. Setting random_state=42, or any fixed number, keeps your results reproducible.

Coming Up Next: Episode 82

t-SNE and UMAP both find structure while staying completely blind to labels. Next time, in Episode 82, we shift direction. We explore Linear Discriminant Analysis, a technique that leans directly on known classes. Instead of guessing at hidden groups, LDA uses your labels to find the exact axes that keep those groups as far apart as possible.

Watch the full video walkthrough on the Intelevo YouTube channel for live visuals, animations, and a complete code demo of everything covered above. Subscribe so you don’t miss Episode 82, and drop a comment with any questions along the way.

Leave a Comment

Your email address will not be published. Required fields are marked *