Sub topic 6
t-SNE step 1: turning distances into probabilities
This chapter assumes you know Euclidean distance, basic probability (distributions and normalisation), and the scikit-learn fit/transform interface. It covers only the first step of t-SNE: building the high-dimensional affinities. Perplexity, the low-dimensional kernel and the objective come in later chapters.
The idea
A neighbour embedding needs a precise definition of "neighbour". t-SNE does not use a hard cutoff such as "the 15 nearest points". It uses a soft one. For each point, it turns distances to all other points into a probability distribution over who its neighbours are. Near points get high probability and far points get almost none.
From distance to conditional probability
For a point xi, t-SNE places a Gaussian centred on it. The probability that xi picks x_j as a neighbour is:
p(j|i) = exp(-‖xi − xj‖² / 2σi²) / Σ{k≠i} exp(-‖xi − xk‖² / 2σ_i²)
The denominator makes the probabilities for point i sum to 1. The bandwidth σi is chosen separately for each point. In a dense region σi is small, and in a sparse region it is large. This lets every point have a similar effective number of neighbours, whatever the local density. The next chapter shows how perplexity sets σ_i.
Symmetrising
p(j|i) and p(i|j) generally differ, because σi and σj differ. t-SNE combines them into one joint probability per pair:
p_ij = (p(j|i) + p(i|j)) / 2n
Dividing by 2n makes all p_ij sum to 1 over the whole dataset. It also guarantees that every point contributes at least about 1/2n of probability mass in total. An outlier therefore still has some pull on the layout.
A small worked example
The code below computes the conditional and joint probabilities for a toy dataset. It uses a fixed σ for all points to keep the example short. Real t-SNE tunes each σ_i.
import numpy as np
from sklearn.metrics import pairwise_distances
X = np.array([[0.0, 0.0], [0.5, 0.0], [0.0, 0.6], [5.0, 5.0]])
sigma = 1.0
D2 = pairwise_distances(X, metric="sqeuclidean")
K = np.exp(-D2 / (2 * sigma**2))
np.fill_diagonal(K, 0.0) # a point is not its own neighbour
P_cond = K / K.sum(axis=1, keepdims=True) # row i holds p(j|i)
P = (P_cond + P_cond.T) / (2 * len(X)) # joint p_ij
print(np.round(P_cond, 3))
print("sum of P:", P.sum())The first three points sit close together, so they give each other nearly all of their probability. The fourth point is far away. Its row of P_cond still sums to 1, so it hands that mass to its nearest neighbours, even though they are far. This is why a per-point σ matters, and why symmetrising is needed.
Common errors
- Using the same σ everywhere. In data with varying density, sparse regions would see almost no neighbours, and dense regions would see too many.
- Forgetting that rows sum to 1. p(j|i) is a distribution for each i. It is not a similarity matrix that is symmetric by default.
- Unscaled features. Distances drive everything, so a feature with a large range dominates the affinities. Standardise first when features use different units.
Exercise
Why does t-SNE divide the sum p(j|i) + p(i|j) by 2n instead of by 2?
Show answer
Dividing by 2n makes all the joint probabilities p_ij add up to 1 across the whole dataset, so they form a single probability distribution over pairs of points. Dividing by 2 would only average the two conditionals, leaving a total of n.