Sub topic 14
UMAP parameters: n_neighbors, min_dist and metric
This chapter assumes you can run scikit-learn code, know what a k-nearest-neighbour graph is, and have read the previous two chapters on how UMAP builds and lays out its graph. Three parameters do most of the work: n_neighbors, min_dist and metric.
n_neighbors: how local is "local"?
n_neighbors sets how many neighbours each point uses when UMAP builds its weighted graph. It plays a role similar to t-SNE's perplexity.
- Small values (2 to 10) make UMAP focus on very local structure. You often get many small, fragmented clusters, and some of them are noise.
- Large values (50 to 200) make UMAP take in more of the global shape. Fine detail blurs, but the relative arrangement of large groups is usually more stable.
- The default of 15 is a reasonable first choice.
min_dist: how tightly points pack
min_dist is the smallest distance allowed between points in the 2-D layout. It does not change which points are neighbours. It only changes how the layout is drawn.
- Small values (0.0 to 0.1) give dense clumps. This suits clustering, where you want groups to stand out.
- Large values (0.5 to 0.99) spread points out. This suits viewing the overall shape, because points inside a group stay visible.
metric: what "close" means
metric is the distance used in the original space. The default is "euclidean". Use "cosine" for text vectors and embeddings, where direction matters more than length. Other options include "manhattan" and "correlation". A wrong metric changes the neighbour graph itself, so no other setting can repair it.
import umap
from sklearn.datasets import load_digits
import matplotlib.pyplot as plt
X, y = load_digits(return_X_y=True)
settings = [(5, 0.1), (15, 0.1), (50, 0.1), (15, 0.8)]
fig, axes = plt.subplots(1, 4, figsize=(20, 5))
for ax, (nn, md) in zip(axes, settings):
Z = umap.UMAP(n_neighbors=nn, min_dist=md, metric="euclidean",
random_state=0).fit_transform(X)
ax.scatter(Z[:, 0], Z[:, 1], c=y, cmap="tab10", s=6)
ax.set_title(f"n_neighbors={nn}, min_dist={md}")
plt.show()The first three panels change only n_neighbors. The last changes only min_dist, so you can see the two effects separately.
Common errors
- Reading
min_distas a clustering setting. It changes appearance, not the neighbour graph. - Tuning
n_neighborsby eye until the clusters "look right". Change one parameter at a time and check several values. - Skipping feature scaling when features have different units. Euclidean distance is dominated by the largest-scale features.
- Setting
random_stateand assuming the result is the only valid one. It makes the run reproducible, but other seeds can look different.
Exercise
You embed sentence vectors with UMAP and get poor neighbour quality. Which parameter should you check first, and what would you change it to? Which parameter would you raise to see overall shape rather than small clusters?
Show answer
Check metric first and change it from the default "euclidean" to "cosine", because text embeddings are compared by direction. To emphasise overall shape, raise n_neighbors (for example from 15 to 100). Raising min_dist only spreads the points out and does not change the neighbour graph.