Sub topic 3
UMAP Parameters, Comparisons, and Reliable Evaluation
Assume you know Python, NumPy, feature vectors, Euclidean distance, and scikit-learn’s fit and transform pattern. UMAP’s plot is controlled mainly by n_neighbors, min_dist, and metric.
UMAP parameters
n_neighborscontrols the neighbourhood scale used to build UMAP’s graph. Small values, such as 5, emphasise local detail and may split a continuous structure. Larger values, such as 50, preserve broader relationships and often produce smoother groups.min_distcontrols how closely points may pack together in the embedding. A small value creates dense, compact groups. A larger value leaves more space within groups.metricdefines similarity in the original feature space. Use"euclidean"for ordinary numeric measurements. For text-like vectors, cosine distance may be more appropriate.
import umap
from sklearn.datasets import load_digits
from sklearn.preprocessing import StandardScaler
X, y = load_digits(return_X_y=True)
X = StandardScaler().fit_transform(X)
embedding = umap.UMAP(
n_neighbors=30,
min_dist=0.2,
metric="euclidean",
random_state=0,
).fit_transform(X)t-SNE versus UMAP
Both methods preserve neighbourhoods, but they optimise different models. t-SNE converts pairwise distances into probabilities and strongly prioritises local relationships. UMAP builds a weighted nearest-neighbour graph and lays out that graph. UMAP usually trains faster, supports transform for new points, and can preserve more broad structure. t-SNE often gives clearer local separation but does not directly provide a standard out-of-sample transform.
Reading embeddings without fooling yourself
A cluster in two dimensions is evidence of similarity under the chosen preprocessing, metric, and parameters. It is not proof of a natural class. Do not interpret cluster area as population size, empty space as meaningful distance, or the distance between clusters as globally accurate. Run several seeds and parameter settings. Colour by labels only for evaluation, not to create the embedding.
Measuring embedding quality
Trustworthiness measures how many neighbours in the embedding were not neighbours in the original space. Higher values are better, but the score depends on n_neighbors and does not measure every useful property.
| Method | Trustworthiness |
|---|---|
| UMAP | 0.91 |
A reproducible workflow
- Remove leakage and choose features using training data only.
- Handle missing values and scale features when their units differ.
- Choose a meaningful metric.
- Record parameters, software versions, and
random_state. - Compare PCA, t-SNE, and UMAP across several settings.
- Report plots together with trustworthiness and task-specific checks.
Exercise
What change usually makes UMAP focus on larger-scale structure?
Show answer
Increase n_neighbors; this builds the graph from broader neighbourhoods.