Sub topic 18
A reproducible workflow for real data
This chapter assumes you know basic Python and NumPy, scikit-learn's fit/transform interface, and the t-SNE and UMAP parameters from earlier chapters. It puts them into one repeatable pipeline. The goal is a plot you can regenerate and defend, not just one that looks good.
The workflow in order
- Clean and scale. Remove constant columns and handle missing values. Standardise features that sit on different scales, because Euclidean distance lets large-scale features dominate.
- Reduce with PCA first. Keeping about 50 components removes noise and speeds up the neighbour search. It rarely changes the local structure.
- Fit a PCA baseline. If t-SNE or UMAP shows nothing that a 2-D PCA plot does not, be suspicious of the extra structure.
- Embed with fixed seeds. Use
random_stateand write down every parameter. - Vary seeds and parameters. Keep only the structure that survives.
- Score. Use trustworthiness to compare runs with a number.
A complete pipeline
This script runs steps 1 to 6 on the digits data. The 8x8 pixel features share one scale, but the scaler shows where that step belongs.
import numpy as np
from sklearn.datasets import load_digits
from sklearn.decomposition import PCA
from sklearn.manifold import TSNE, trustworthiness
from sklearn.preprocessing import StandardScaler
import umap # installed as the umap-learn package
X, y = load_digits(return_X_y=True)
# Steps 1-2: drop constant columns, scale, then reduce with PCA.
X = X[:, X.std(axis=0) > 0]
X = StandardScaler().fit_transform(X)
X50 = PCA(n_components=50, random_state=0).fit_transform(X)
# Step 3: linear baseline.
baseline = PCA(n_components=2, random_state=0).fit_transform(X50)
print(f"PCA baseline: {trustworthiness(X50, baseline, n_neighbors=10):.3f}")
# Steps 4-6: several seeds per method, scored on the same input.
for seed in range(3):
tsne = TSNE(perplexity=30, init="pca", learning_rate="auto",
random_state=seed).fit_transform(X50)
um = umap.UMAP(n_neighbors=15, min_dist=0.1,
random_state=seed).fit_transform(X50)
print(f"seed {seed}: "
f"t-SNE {trustworthiness(X50, tsne, n_neighbors=10):.3f}, "
f"UMAP {trustworthiness(X50, um, n_neighbors=10):.3f}")Scores for the same method should be close across seeds. If they swing widely, the layout is not stable and you should not read fine detail from it.
Common errors
- Fitting the scaler or PCA on test data. For new points, fit on the training split only. UMAP offers
transformfor unseen points. scikit-learn'sTSNEdoes not. - Setting
random_stateand assuming parallel speed. In UMAP, a fixed seed turns off some parallelism, so runs are slower. This is the price of reproducibility. - Judging from one run. A single plot can show clusters that disappear under another seed or perplexity.
- Skipping the record. Save the library versions, seeds, parameters and preprocessing steps with the figure. Results can differ between package versions.
Exercise
You run UMAP on a dataset with random_state=None and get three clear clusters. A colleague reruns it and sees four. What should you change in your workflow, and what should you check next?
Show answer
Set random_state and record it, then rerun with several seeds and a few n_neighbors values. Score each run with trustworthiness. Treat only the clusters that appear in every run as reliable, and compare them with a PCA baseline.