t-SNE: A Neighbourhood Map, Not a Map of Everything
A thousand coordinates are difficult to draw
A message can be represented by hundreds of word features or embedding coordinates. An embedding is a numeric description of a message. You may want a two-dimensional plot to inspect which messages look similar, even though the original data is much larger.
t-SNE, short for t-distributed Stochastic Neighbour Embedding, builds a visual arrangement that tries to keep local neighbours near each other. It is mainly an exploration tool. A beautiful plot is not proof that natural categories exist.
Start with a neighbourhood, not the full map
For one message, suppose the original-space distances to four others are 0.2, 0.3, 2.0, and 3.0. The first two are plausible neighbours. A useful visual map should try to place those two nearby, even if it cannot preserve every faraway distance.
1distances = {"message_A": 0.2, "message_B": 0.3, "message_C": 2.0, "message_D": 3.0}
2nearest = sorted(distances, key=distances.get)[:2]
3print("Two nearest messages:", nearest)Two nearest messages: ['message_A', 'message_B']
This code identifies neighbours only; it is not t-SNE. It isolates the question the visualisation should answer: which rows have similar local surroundings?
How the visual fit works
The method turns original-space similarities into neighbour probabilities. It defines another set of similarities between plotted points, then moves those plotted points to reduce disagreement between the two sets.
Its low-dimensional similarity uses a heavy-tailed Student t distribution. Compared with a narrow bell shape, the heavier tail gives distant plotted points more room and helps reduce crowding. You do not need the probability formula to interpret the result responsibly.
Inspect nearby records
Look up the original messages represented by nearby dots. These local relationships are the main reading of the map.
Do not read gaps as a ruler
- ✓ Check local neighbours.
- ✓ Compare seeds and perplexities.
- × Do not name axes as real features.
- × Do not rank cluster sizes by island area.
Schematic only. A bigger gap in the layout is not a calibrated original-space distance or proof of separate classes.
Perplexity sets a neighbourhood scale
Perplexity controls an effective neighbourhood size in the similarity calculation. It is not a cluster count and not an exact number of neighbours retained for every row. Low and high settings can reveal different arrangements.
Key Points
- Perplexity must be smaller than the number of samples in scikit-learn.
- Try several reasonable values rather than one attractive picture.
- Compare local membership, not just the shape of the whole plot.
- Keep the random seed and data representation in the experiment record.
Fit a small real embedding
This cell fits eight rows with three numeric features into two coordinates. The toy groups make it easy to see the intended input structure. We print the shape and numerical validity rather than exact coordinates, which can vary across library versions.
1import numpy as np
2from sklearn.manifold import TSNE
3
4X = np.array([
5 [0.0, 0.0, 0.0], [0.1, 0.0, 0.1],
6 [0.0, 0.1, 0.0], [0.1, 0.1, 0.1],
7 [3.0, 3.0, 3.0], [3.1, 3.0, 3.1],
8 [3.0, 3.1, 3.0], [3.1, 3.1, 3.1],
9])
10embedding = TSNE(
11 n_components=2, perplexity=2, init="random",
12 learning_rate="auto", random_state=7, max_iter=1000,
13).fit_transform(X)
14print("Input shape:", X.shape)
15print("Map shape:", embedding.shape)
16print("All coordinates finite:", bool(np.isfinite(embedding).all()))Input shape: (8, 3) Map shape: (8, 2) All coordinates finite: True
max_iter is the current scikit-learn argument for the optimisation limit. Real exploration normally plots the returned coordinates and inspects the actual messages or records associated with nearby points.
Compare runs before believing the islands
The optimisation depends on initialisation and settings. A new seed can rotate, shift, or rearrange islands. A good inspection checks whether local relationships survive several settings, not whether every coordinate stays identical.
You can compare neighbourhoods in the original and plotted spaces. Trustworthiness is one metric that checks whether points that look like neighbours in the map were genuinely near in the input. Even that score is not proof of useful clusters.
Coordinates are not features with a fixed meaning
The x-axis is not “positivity” and the y-axis is not “customer value” unless you have separate evidence for that interpretation. They are fitted layout coordinates. Rotation and reflection do not change the underlying neighbourhood story.
Key Points
- Do not compare coordinate values across separately fitted maps.
- Island area does not reliably report original group density.
- Distances between islands do not reliably preserve global distances.
- Colouring by known labels can help inspection but does not make the fitting supervised.
A plotted island is not a discovered cluster label
t-SNE can make continuous data look separated. It can also change visible gaps when you change perplexity. Running a clustering algorithm on the two-dimensional coordinates may group layout artefacts rather than original data structure.
If you need clusters, evaluate clustering in the original or a justified transformed representation. Use the t-SNE chart to generate questions and inspect samples, not as the only proof for naming groups.
PCA and t-SNE serve different purposes
Key Points
- PCA is a linear transformation with explained variance and ordinary transform for new rows.
- t-SNE is primarily a nonlinear visual arrangement for the fitted collection.
- scikit-learn TSNE has fit_transform but no ordinary transform for future rows.
- PCA can first reduce a large numeric representation before visual exploration, but that preprocessing also needs inspection.
- For a prediction pipeline, validate a proper representation rather than reusing a descriptive map by default.
Quick check
What does t-SNE mainly try to preserve?
It tries to preserve local neighbour relationships when arranging high-dimensional rows into a low-dimensional visual map.
Does perplexity set the number of clusters?
No. It controls an effective neighbourhood scale in the similarity calculation.
Why should distant islands not be read as exact original distances?
t-SNE prioritises local similarities, and the fitted layout can distort global distances, sizes, and gaps.
Why is the ordinary scikit-learn t-SNE estimator awkward as a production feature transform?
It fits a layout for the supplied rows and has no ordinary transform for unseen rows. Separately fitted layouts do not share consistent coordinates.
Use the map to ask better questions
Key Points
- Inspect neighbours and original examples together.
- Record representation, perplexity, seed, and optimisation settings.
- Treat visual islands as hypotheses, not confirmed classes.
- Next, anomaly detection flags unusual rows for investigation.
Theia
Can you explain this concept?
Practise t-SNE: A Neighbourhood Map, Not a Map of Everything with realistic interview questions. Choose spoken or typed answers, get feedback on your reasoning, then return to the material that needs work.
Sign in to practise this conceptThe session stays focused on this lesson. Your selected interview level sets the bar.