r"""
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.
"""

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()
