.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/05_graph/plot_01_mst_clustering.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code or to run this example in your browser via Binder. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_05_graph_plot_01_mst_clustering.py: Minimal spanning tree clustering: cutting, splitting, and Gath–Geva refinement ============================================================================== Prototype-based algorithms describe a cluster by one geometric object and fail when the groups have arbitrary shapes. Graph-theoretic methods start instead from the *minimal spanning tree* (MST) of the data and obtain clusters by removing edges. Zahn's classical criteria for choosing these edges compare an edge with the mean edge length of the whole tree (global threshold) or with the edges in its own neighborhood (local inconsistency). Both find well-separated groups, but a sparse *chain* of points between two groups keeps them together: the chaining effect of single linkage. The hybrid MST–GG algorithm of Vathy-Fogarassy, Feil, and Abonyi adds a third criterion. A subtree is split at the edge that minimizes the total *fuzzy hypervolume* :math:`\sum_j\sqrt{\det\mathbf{F}_j}` of the resulting parts, which breaks chains and bends because a union of two compact groups has a far larger volume than its parts. Finally, the crisp tree partition initializes Gath–Geva clustering, which is too sensitive to its starting point to be run from a random partition on such data but refines a good start into a fuzzy, parametric description (centers, covariances, memberships). .. GENERATED FROM PYTHON SOURCE LINES 25-36 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from sklearn.metrics import adjusted_rand_score from fuzzyclust import GG from fuzzyclust.graph import MSTClustering, make_chained_blobs, plot_mst from fuzzyclust.plotting import PALETTE, plot_clusters, plot_covariance_ellipses, plot_membership_contours X, y = make_chained_blobs(n_samples_per_blob=120, n_chain=25, random_state=0) blob = y < 2 # the chain points (label 2) have no "true" cluster .. GENERATED FROM PYTHON SOURCE LINES 37-46 The tree and Zahn's criteria: the chaining effect ------------------------------------------------- With the splitting phase disabled (``min_gain=1.0`` can never be reached), the estimator is the classical MST clusterer. Without any cut, the tree is one cluster; the local inconsistency criterion flags edges that are much longer than their neighbors. Along the chain every edge is about as long as the next one, so at most one edge near one of the blobs is flagged and the rest of the chain stays attached to the other blob; the groups are not separated from the chain. .. GENERATED FROM PYTHON SOURCE LINES 46-61 .. code-block:: Python # [snippet:tree] tree_only = MSTClustering(threshold=None, inconsistency=None, min_gain=1.0, refine=None).fit(X) zahn = MSTClustering(inconsistency=2.5, n_hops=2, min_gain=1.0, refine=None).fit(X) print("tree only :", tree_only.n_clusters_, "cluster") print("Zahn (f=2.5):", zahn.n_clusters_, "clusters, cut edges:", len(zahn.cut_edges_)) # [/snippet:tree] fig, axes = plt.subplots(1, 2, figsize=(11, 4.4), sharex=True, sharey=True) plot_mst(X, tree_only, ax=axes[0], labels=np.zeros(len(X), int), legend=False) axes[0].set_title("Minimal spanning tree of two blobs joined by a chain") plot_mst(X, zahn, ax=axes[1]) axes[1].set_title(f"Zahn's local criterion: {zahn.n_clusters_} clusters, the chain stays attached") plt.tight_layout() .. image-sg:: /auto_examples/05_graph/images/sphx_glr_plot_01_mst_clustering_001.png :alt: Minimal spanning tree of two blobs joined by a chain, Zahn's local criterion: 2 clusters, the chain stays attached :srcset: /auto_examples/05_graph/images/sphx_glr_plot_01_mst_clustering_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none tree only : 1 cluster Zahn (f=2.5): 2 clusters, cut edges: 1 .. GENERATED FROM PYTHON SOURCE LINES 62-70 Volume-based splitting breaks the chain --------------------------------------- The hypervolume criterion evaluates every edge of the largest cluster as a cut and keeps the one that lowers the total volume most. The union of the two blobs with the chain is a long, thin cloud with a large determinant, whereas the blobs alone are compact. The volume cuts therefore separate the chain from a blob and cut the chain itself. A Gaussian blob is *not* split, because halving it would increase the summed volume (:math:`2\times0.6>1`). .. GENERATED FROM PYTHON SOURCE LINES 70-83 .. code-block:: Python # [snippet:fit] mst = MSTClustering(random_state=0).fit(X) print("clusters found:", mst.n_clusters_) print("cut phases :", mst.cut_phases_, " (0 = length criteria, 1 = volume criterion)") print("hypervolumes :", mst.hypervolumes_.round(3)) # [/snippet:fit] fig, ax = plt.subplots(figsize=(6.5, 4.6)) plot_mst(X, mst, ax=ax) ax.set_title(f"Volume criterion: {mst.n_clusters_} tree clusters, the chain is cut off") plt.tight_layout() .. image-sg:: /auto_examples/05_graph/images/sphx_glr_plot_01_mst_clustering_002.png :alt: Volume criterion: 4 tree clusters, the chain is cut off :srcset: /auto_examples/05_graph/images/sphx_glr_plot_01_mst_clustering_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none clusters found: 4 cut phases : [0 1 1] (0 = length criteria, 1 = volume criterion) hypervolumes : [0.287 0.246 0.016 0.05 ] .. GENERATED FROM PYTHON SOURCE LINES 84-94 Gath–Geva refinement from the tree partition -------------------------------------------- The crisp tree partition initializes GG with as many clusters as there are tree clusters. GG keeps the structure and turns it into a fuzzy partition with covariance matrices (``cluster_centers_``, ``covariances_``, ``membership_``); the same GG started from a random partition typically merges a blob with the chain or splits a blob. The cut adequacy :math:`CP(e)` (``cut_quality_``) checks every cut against the refined memberships: values near 2 mean that the end points of the cut edge clearly belong to different final clusters. .. GENERATED FROM PYTHON SOURCE LINES 94-111 .. code-block:: Python # [snippet:refine] gg_random = GG(n_clusters=mst.n_clusters_, random_state=0).fit(X) ari_tree = adjusted_rand_score(y[blob], mst.tree_labels_[blob]) ari_gg = adjusted_rand_score(y[blob], mst.labels_[blob]) ari_random = adjusted_rand_score(y[blob], gg_random.labels_[blob]) print(f"ARI of the blobs: tree partition {ari_tree:.2f}, MST-GG {ari_gg:.2f}, GG from random start {ari_random:.2f}") print("cut adequacy CP(e):", mst.cut_quality_.round(2)) # [/snippet:refine] fig, axes = plt.subplots(1, 2, figsize=(11, 4.4), sharex=True, sharey=True) for ax, model, name in zip(axes, (mst, gg_random), ("MST-GG (tree initialization)", "GG from a random start"), strict=True): plot_membership_contours(model, X, ax=ax, labels=False) plot_covariance_ellipses(model.cluster_centers_, model.covariances_, ax=ax) ax.set_title(f"{name}: ARI(blobs) = {adjusted_rand_score(y[blob], model.labels_[blob]):.2f}") plt.tight_layout() .. image-sg:: /auto_examples/05_graph/images/sphx_glr_plot_01_mst_clustering_003.png :alt: MST-GG (tree initialization): ARI(blobs) = 1.00, GG from a random start: ARI(blobs) = 0.95 :srcset: /auto_examples/05_graph/images/sphx_glr_plot_01_mst_clustering_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none ARI of the blobs: tree partition 1.00, MST-GG 1.00, GG from random start 0.95 cut adequacy CP(e): [0. 1.52 2. ] .. GENERATED FROM PYTHON SOURCE LINES 112-120 Parameter study: greediness of the splitting -------------------------------------------- ``min_gain`` is the minimal *relative* decrease of the volume criterion that a split must achieve. Small values accept cosmetic cuts (a few tail points of a blob), whereas large values stop at the coarse clusters; ``min_cluster_size`` forbids tiny fragments altogether. The refinement can also be switched to FCM or turned off. The chain points pull FCM away from the tree partition, but not GG. .. GENERATED FROM PYTHON SOURCE LINES 120-147 .. code-block:: Python # [snippet:scan] gains = (0.05, 0.1, 0.15, 0.3, 0.6) sizes = (2, 5, 10, 20) table = np.array([[MSTClustering(min_gain=g, min_cluster_size=s, refine=None).fit(X).n_clusters_ for s in sizes] for g in gains]) # [/snippet:scan] fig, axes = plt.subplots(1, 2, figsize=(11, 3.8)) for j, s in enumerate(sizes): axes[0].plot(gains, table[:, j], "o-", color=PALETTE[j], label=f"min_cluster_size = {s}") axes[0].set_xlabel("min_gain (relative volume decrease required)") axes[0].set_ylabel("number of tree clusters") axes[0].set_title("Number of clusters versus the splitting parameters") axes[0].legend(frameon=False, fontsize=8) axes[0].grid(True, color="0.9") rows = [] for refine in ("gg", "fcm", None): model = MSTClustering(refine=refine, random_state=0).fit(X) rows.append((str(refine), adjusted_rand_score(model.tree_labels_, model.labels_), adjusted_rand_score(y[blob], model.labels_[blob]))) print(f"refine={refine!s:5s}: agreement with the tree partition {rows[-1][1]:.2f}, ARI(blobs) {rows[-1][2]:.2f}") fcm_refined = MSTClustering(refine="fcm", random_state=0).fit(X) plot_clusters(X, fcm_refined.labels_, fcm_refined.cluster_centers_, ax=axes[1], legend=False) axes[1].set_title("FCM refinement: the chain pulls the prototypes off the blobs") plt.tight_layout() .. image-sg:: /auto_examples/05_graph/images/sphx_glr_plot_01_mst_clustering_004.png :alt: Number of clusters versus the splitting parameters, FCM refinement: the chain pulls the prototypes off the blobs :srcset: /auto_examples/05_graph/images/sphx_glr_plot_01_mst_clustering_004.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none refine=gg : agreement with the tree partition 0.98, ARI(blobs) 1.00 refine=fcm : agreement with the tree partition 0.63, ARI(blobs) 0.66 refine=None : agreement with the tree partition 1.00, ARI(blobs) 1.00 .. GENERATED FROM PYTHON SOURCE LINES 148-157 **Take-away.** The MST turns clustering into the choice of edges to remove. Zahn's length criteria find separated groups but cannot see a chain. The fuzzy hypervolume criterion of the hybrid algorithm splits wherever the parts are much more compact than their union, which breaks chains and bends while leaving Gaussian groups intact; ``min_gain`` and ``min_cluster_size`` set how fine the result is. The tree partition is the initialization that Gath–Geva needs. Started there, GG delivers the fuzzy, parametric description that the tree alone lacks, and the cut adequacy shows which cuts the refinement confirmed. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.009 seconds) .. _sphx_glr_download_auto_examples_05_graph_plot_01_mst_clustering.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/ComplexSystemsIntelligence/fuzzyclust/main?urlpath=lab/tree/notebooks/auto_examples/05_graph/plot_01_mst_clustering.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_01_mst_clustering.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_mst_clustering.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_mst_clustering.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_