r"""
Hybrid MST--GG on the Iris data
===============================

On the four-dimensional Iris measurements the tree cuts separate *setosa*
right away (a long edge), while *versicolor* and *virginica* touch and are
split by the volume criterion. A minimal cluster size of ten samples
keeps the volume criterion from splitting off a handful of border samples.
The result is displayed by classical MDS of the scaled data.
"""

import matplotlib.pyplot as plt

from graphclust import HybridMSTGG, classical_mds
from graphclust._utils import pairwise_distances
from graphclust.datasets import load_iris, minmax_scale
from graphclust.plotting import plot_partition
from graphclust.validity import clustering_rate

X, y = load_iris()

# [snippet:iris]
model = HybridMSTGG(max_clusters=3, lambda_=2.0, min_cluster_size=10).fit(X)
print("tree only :", clustering_rate(y, model.tree_labels_))
print("tree + GG :", clustering_rate(y, model.labels_))
# [/snippet:iris]
print([s["criterion"] for s in model.split_history_])

Y = classical_mds(pairwise_distances(minmax_scale(X)), 2)
fig, axes = plt.subplots(1, 2, figsize=(10, 4.2))
plot_partition(Y, y, ax=axes[0], title="species")
plot_partition(Y, model.labels_, ax=axes[1], title=f"hybrid MST-GG ({100 * clustering_rate(y, model.labels_):.1f}% correct)")
for ax in axes:
    ax.set_xticks([])
    ax.set_yticks([])
plt.tight_layout()
