MNIST Digits - Cosine Metric

70,000 imagesMLembeddingscosine

A TMAP of the full MNIST dataset (70,000 handwritten digits, 784-dimensional pixel vectors) using cosine metric via USearch. The tree naturally clusters digits by shape, and paths between morphologically similar digits (3 to 8, 4 to 9, 7 to 1, 5 to 6) reveal the intermediate forms that connect them. Demonstrates dense metric support - the same API works for binary fingerprints (Jaccard), dense embeddings (cosine/euclidean), and precomputed distances.

MNIST Digits - Cosine MetricOpen full page
Loading interactive demo…

How it was made

generate.pypython
from sklearn.datasets import fetch_openml
from tmap import TMAP

mnist = fetch_openml("mnist_784", version=1, as_frame=False)
X = mnist.data.astype(np.float32)
labels = mnist.target.astype(int)

model = TMAP(metric="cosine", n_neighbors=20, seed=42).fit(X)

# Trace paths between similar digits
for a, b in [(3, 8), (4, 9), (7, 1), (5, 6)]:
    idx_a = np.where(labels == a)[0][0]
    idx_b = np.where(labels == b)[0][0]
    path = model.path(idx_a, idx_b)
    path_labels = labels[path]
    print(f"Path {a}->{b}: {len(path)} nodes")

model.to_html("mnist.html", color=labels)