.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/03_trees/plot_05_hdbscan.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_05_hdbscan.py: HDBSCAN*: density-corrected spanning trees ========================================== HDBSCAN* (Campello, Moulavi and Sander 2013) keeps the spanning tree of the MST methods but measures distances in a density-aware way: the *mutual reachability distance* of two samples is at least the distance of each to its ``min_samples``-th neighbor. Sparse regions are pushed apart, the MST of these distances is turned into a hierarchy, and the clusters that persist longest (largest stability) are selected. Samples in sparse regions become noise. The density-based validity index DBCV, computed on mutual reachability trees as well, scores the result without using labels. .. GENERATED FROM PYTHON SOURCE LINES 14-41 .. image-sg:: /auto_examples/03_trees/images/sphx_glr_plot_05_hdbscan_001.png :alt: MST of mutual reachability distances, Zahn's criteria (6 clusters), HDBSCAN* (3 clusters, gray: noise) :srcset: /auto_examples/03_trees/images/sphx_glr_plot_05_hdbscan_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none clusters: 3 noise samples: 1 DBCV: 0.423 Zahn DBCV: 0.037 rate 0.89 HDBSCAN rate (noise counted as error): 0.98 | .. code-block:: Python import matplotlib.pyplot as plt from graphclust import HDBSCANStar, MSTClustering from graphclust.datasets import make_variety from graphclust.plotting import plot_graph, plot_partition from graphclust.validity import clustering_rate, dbcv X, y = make_variety() # [snippet:hdbscan] hdb = HDBSCANStar(min_cluster_size=8, min_samples=4).fit(X) print("clusters:", hdb.n_clusters_, " noise samples:", (hdb.labels_ < 0).sum()) print("DBCV:", round(dbcv(X, hdb.labels_), 3)) # [/snippet:hdbscan] zahn = MSTClustering(lambda_=2.0, min_cluster_size=2).fit(X) print("Zahn DBCV:", round(dbcv(X, zahn.labels_), 3), " rate", clustering_rate(y, zahn.labels_)) print("HDBSCAN rate (noise counted as error):", clustering_rate(y, hdb.labels_)) fig, axes = plt.subplots(1, 3, figsize=(13, 4.2)) plot_graph(X, hdb.mst_, ax=axes[0], node_size=8, title="MST of mutual reachability distances") plot_partition(X, zahn.labels_, ax=axes[1], title=f"Zahn's criteria ({zahn.n_clusters_} clusters)") plot_partition(X, hdb.labels_, ax=axes[2], title=f"HDBSCAN* ({hdb.n_clusters_} clusters, gray: noise)") 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.059 seconds) .. _sphx_glr_download_auto_examples_03_trees_plot_05_hdbscan.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_05_hdbscan.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_05_hdbscan.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_05_hdbscan.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_05_hdbscan.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_