.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/03_trees/plot_01_mst_zahn.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_03_trees_plot_01_mst_zahn.py: Zahn's MST clustering and the chaining effect ============================================= Deleting the inconsistent edges of the minimal spanning tree separates clusters of arbitrary shape. Zahn's global criterion calls an edge inconsistent when it is λ times longer than the average tree edge; his local criterion compares it with the edges around it, which also works for clusters of different density. Neither can cut a *chain* of points between two groups, because every edge of the chain is short: this is the chaining effect known from single linkage. .. GENERATED FROM PYTHON SOURCE LINES 13-40 .. image-sg:: /auto_examples/03_trees/images/sphx_glr_plot_01_mst_zahn_001.png :alt: minimal spanning tree, global + local criterion, λ = 2 (4 clusters), four longest edges removed (single linkage) :srcset: /auto_examples/03_trees/images/sphx_glr_plot_01_mst_zahn_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none 4 clusters, 3 edges removed lambda=1.5: 7 clusters, clustering rate 0.44 lambda=2: 4 clusters, clustering rate 0.53 lambda=3: 2 clusters, clustering rate 0.53 lambda=4: 2 clusters, clustering rate 0.53 lambda=6: 2 clusters, clustering rate 0.53 | .. code-block:: Python import matplotlib.pyplot as plt from graphclust import MSTClustering from graphclust.datasets import make_chainlink from graphclust.plotting import plot_graph, plot_partition from graphclust.validity import clustering_rate X, y = make_chainlink() # [snippet:zahn] mst = MSTClustering(lambda_=2.0, criterion="both", min_cluster_size=3).fit(X) print(f"{mst.n_clusters_} clusters, {len(mst.removed_edges_)} edges removed") # [/snippet:zahn] fig, axes = plt.subplots(1, 3, figsize=(12, 4)) plot_graph(X, mst.mst_, ax=axes[0], node_size=12, title="minimal spanning tree") plot_partition(X, mst.labels_, ax=axes[1], title=f"global + local criterion, λ = 2 ({mst.n_clusters_} clusters)") for lam in (1.5, 2, 3, 4, 6): m = MSTClustering(lambda_=lam, min_cluster_size=3).fit(X) print(f"lambda={lam}: {m.n_clusters_} clusters, clustering rate {clustering_rate(y, m.labels_):.2f}") sl = MSTClustering(n_clusters=4, criterion=None).fit(X) plot_partition(X, sl.labels_, ax=axes[2], title="four longest edges removed (single linkage)") for ax in axes: ax.set_xticks([]) ax.set_yticks([]) plt.tight_layout() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.057 seconds) .. _sphx_glr_download_auto_examples_03_trees_plot_01_mst_zahn.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/graphclust/main?urlpath=lab/tree/notebooks/auto_examples/03_trees/plot_01_mst_zahn.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_01_mst_zahn.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_mst_zahn.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_mst_zahn.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_