t-SNE (t-Distributed Stochastic Neighbor Embedding) › Example
Machine Learning from ScratchAll chapters t-SNE (t-Distributed Stochastic Neighbor Embedding) › ExampleOne section · Sections in this chapter4 / 8 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
Let's work through a concrete example using a small dataset to demonstrate how t-SNE calculations work step by step. We'll use a simple 2D dataset with 8 points divided into two clusters, showing exactly how the formulas are applied in practice.
# Create a simple 2D dataset for demonstration
X_example = np.array(
[[1, 1], [1, 2], [2, 1], [2, 2], [5, 5], [5, 6], [6, 5], [6, 6]]
)
y_example = np.array([0, 0, 0, 0, 1, 1, 1, 1])Original 2D data: [[1 1] [1 2] [2 1] [2 2] [5 5] [5 6] [6 5] [6 6]] Labels: [0 0 0 0 1 1 1 1]
Step 1: Calculate pairwise distances
First, we compute the squared Euclidean distance for all pairs of points.
# Calculate pairwise distances manually
n = X_example.shape[0]
distances = np.zeros((n, n))
for i in range(n):
for j in range(n):
distances[i, j] = np.sum((X_example[i] - X_example[j]) ** 2)Pairwise squared distances: [[ 0. 1. 1. 2. 32. 41. 41. 50.] [ 1. 0. 2. 1. 25. 32. 34. 41.] [ 1. 2. 0. 1. 25. 34. 32. 41.] [ 2. 1. 1. 0. 18. 25. 25. 32.] [32. 25. 25. 18. 0. 1. 1. 2.] [41. 32. 34. 25. 1. 0. 2. 1.] [41. 34. 32. 25. 1. 2. 0. 1.] [50. 41. 41. 32. 2. 1. 1. 0.]]
Step 2: Convert distances to probabilities
Next, we apply the Gaussian kernel formula to convert distances into conditional probabilities. For demonstration, we'll calculate (the probability that point 0 picks each other point as a neighbor):
We use for simplicity (in practice, is determined by the perplexity parameter).
# Calculate conditional probabilities for one point (point 0)
sigma = 1.0 # Simplified - in practice, this is determined by perplexity
i = 0
# Calculate conditional probabilities p_{j|i}
p_ji = np.zeros(n)
for j in range(n):
if i != j:
p_ji[j] = np.exp(-distances[i, j] / (2 * sigma**2))
# Normalize
p_ji[i] = 0 # Set self-probability to 0
p_ji = p_ji / np.sum(p_ji)Conditional probabilities p_{j|0} (using sigma = 1.0):
p_{0|0} = 0.0000
p_{1|0} = 0.3837
p_{2|0} = 0.3837
p_{3|0} = 0.2327
p_{4|0} = 0.0000
p_{5|0} = 0.0000
p_{6|0} = 0.0000
p_{7|0} = 0.0000Notice how points 1, 2, and 3 (which are close to point 0 in the first cluster) have higher probabilities, while points 4-7 (in the distant second cluster) have probabilities close to zero.
Step 3: Apply complete t-SNE algorithm
Now we apply the full t-SNE algorithm, which computes all pairwise probabilities, symmetrizes them, creates a low-dimensional representation, and iteratively optimizes the positions to minimize the KL divergence.
# Apply t-SNE to our example data
tsne_example = TSNE(n_components=2, random_state=42, perplexity=2)
X_tsne_example = tsne_example.fit_transform(X_example)t-SNE result (low-dimensional coordinates): [[ -95.73 48.14] [-100.53 43.68] [-100.19 52.94] [-104.99 48.48] [ 44.72 -55.92] [ 38.18 -55.52] [ 45.11 -49.37] [ 38.57 -48.98]]

