Sub topic 17
Measuring embedding quality
This chapter assumes you can run the scikit-learn fit_transform pattern, know what a k-nearest-neighbour set is, and have already produced PCA, t-SNE and UMAP plots of the digits data. A plot can look convincing and still be misleading. A number lets you compare runs, methods and parameter settings.
What a neighbour-based score measures
Neighbour embeddings promise to keep local neighbourhoods. So the natural test is to take each point, find its k nearest neighbours in the original space, and check whether they are still its neighbours in the 2-D map.
Two failure types exist:
- Intrusions are points that are close in the map but were far apart in the original space. These are false neighbours.
- Extrusions are points that were close originally but are far apart in the map. These are torn neighbourhoods.
Trustworthiness penalises intrusions. Continuity penalises extrusions. Both range from 0 to 1, and higher is better. scikit-learn provides trustworthiness. It has no built-in continuity function, but you get it by swapping the roles of the two spaces, as shown below.
Computing the scores
The script below compares PCA and t-SNE on the digits data.
from sklearn.datasets import load_digits
from sklearn.decomposition import PCA
from sklearn.manifold import TSNE, trustworthiness
X, y = load_digits(return_X_y=True)
Z_pca = PCA(n_components=2, random_state=0).fit_transform(X)
Z_tsne = TSNE(init="pca", learning_rate="auto",
random_state=0).fit_transform(X)
for name, Z in [("PCA", Z_pca), ("t-SNE", Z_tsne)]:
t = trustworthiness(X, Z, n_neighbors=10)
c = trustworthiness(Z, X, n_neighbors=10) # continuity
print(f"{name}: trustworthiness = {t:.3f}, continuity = {c:.3f}")The second call passes the map as the "original" space and the data as the "embedding". That swap computes continuity. Expect t-SNE to score noticeably higher than PCA on trustworthiness, because PCA squeezes 64 dimensions into two and creates many false neighbours.
Using the score well
A single number is easy to over-trust. Keep these rules in mind:
- Fix
n_neighborswhen comparing. Scores at k = 5 and k = 50 answer different questions. Small k tests fine local structure, and large k tests broader layout. - Compare like with like. Use the same data, the same preprocessing and the same k for every run.
- Repeat across seeds. Run each method with several
random_statevalues and look at the spread. If the score varies more between seeds than between parameter settings, the difference you see is noise. - Do not pick the highest score blindly. These metrics reward local fidelity. They say little about whether cluster sizes or the distances between clusters mean anything.
from sklearn.manifold import TSNE, trustworthiness
from sklearn.datasets import load_digits
import numpy as np
X, _ = load_digits(return_X_y=True)
for perp in [5, 30, 100]:
scores = []
for seed in range(3):
Z = TSNE(perplexity=perp, init="pca", learning_rate="auto",
random_state=seed).fit_transform(X)
scores.append(trustworthiness(X, Z, n_neighbors=10))
print(f"perplexity {perp}: mean {np.mean(scores):.3f}, std {np.std(scores):.3f}")Exercise
A t-SNE map has high trustworthiness but low continuity. What kind of error does it make, and what does it look like in the plot?
Show answer
It has few intrusions but many extrusions. Points that were close in the original space have been pulled apart, so one true neighbourhood is split into several separate clumps in the map.