Note
Go to the end to download the full example code or to run this example in your browser via Binder.
PCA, Sammon mapping and MDS#
The distance-based maps differ in which distances they care about. PCA (or classical MDS on Euclidean distances) preserves the large-scale variance, metric MDS minimizes the raw squared error of all distances, Sammon’s mapping divides each error by the input distance and therefore favors local structure, and non-metric MDS only keeps the rank order of the dissimilarities. On the standardized Wine data all four give a similar picture with overlapping cultivars.

PCA {'sammon_stress': 0.147, 'mds_stress': 0.128, 'residual_variance': 0.329, 'auc_rnx': 0.375, 'trustworthiness@10': 0.888, 'continuity@10': 0.941}
metric MDS {'sammon_stress': 0.063, 'mds_stress': 0.051, 'residual_variance': 0.184, 'auc_rnx': 0.434, 'trustworthiness@10': 0.918, 'continuity@10': 0.945}
Sammon mapping {'sammon_stress': 0.062, 'mds_stress': 0.053, 'residual_variance': 0.206, 'auc_rnx': 0.42, 'trustworthiness@10': 0.909, 'continuity@10': 0.94}
non-metric MDS {'sammon_stress': 0.072, 'mds_stress': 0.057, 'residual_variance': 0.149, 'auc_rnx': 0.431, 'trustworthiness@10': 0.916, 'continuity@10': 0.945}
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA
from graphclust import MetricMDS, NonMetricMDS, SammonMapping, metrics
from graphclust.datasets import load_wine
from graphclust.plotting import plot_embedding
X, y = load_wine()
X = (X - X.mean(0)) / X.std(0)
# [snippet:maps]
maps = {
"PCA": PCA(2).fit_transform(X),
"metric MDS": MetricMDS(random_state=0).fit_transform(X),
"Sammon mapping": SammonMapping().fit_transform(X),
"non-metric MDS": NonMetricMDS(random_state=0).fit_transform(X),
}
# [/snippet:maps]
fig, axes = plt.subplots(1, 4, figsize=(14, 3.6))
for ax, (name, Y) in zip(axes, maps.items()):
q = metrics.mapping_quality(X, Y, k=(10,))
plot_embedding(Y, y, ax=ax, title=f"{name}\nSammon {q['sammon_stress']:.3f}, T(10) {q['trustworthiness@10']:.3f}")
print(name, {k: round(v, 3) for k, v in q.items()})
plt.tight_layout()
Total running time of the script: (0 minutes 0.276 seconds)