.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/04_neighborhoods/plot_04_snn_density_peaks.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_04_neighborhoods_plot_04_snn_density_peaks.py: Shared-neighbor density and density peaks ========================================= Two density-based relatives of the shared-neighbor idea: * SNN clustering (Ertöz, Steinbach and Kumar 2003) measures density by the number of strong shared-neighbor links; core samples linked by strong links form clusters and weakly linked samples are noise. * Density peaks (Rodriguez and Laio 2014) compute for every sample its local density ρ and the distance δ to the nearest denser sample. Cluster centers stand out with both large ρ and large δ in the *decision graph*; every other sample follows its nearest denser neighbor, a tree pointing to the peaks. .. GENERATED FROM PYTHON SOURCE LINES 15-45 .. image-sg:: /auto_examples/04_neighborhoods/images/sphx_glr_plot_04_snn_density_peaks_001.png :alt: SNN density clustering (gray: noise), decision graph, density peaks, 3 centers :srcset: /auto_examples/04_neighborhoods/images/sphx_glr_plot_04_snn_density_peaks_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none SNN: 3 clusters, noise 5 rate 0.9868421052631579 density peaks rate: 0.9947368421052631 | .. code-block:: Python import matplotlib.pyplot as plt from sklearn.datasets import make_blobs from graphclust import DensityPeaks, SharedNearestNeighborClustering from graphclust.plotting import plot_decision_graph, plot_partition from graphclust.validity import clustering_rate # three groups of very different density X, y = make_blobs([150, 150, 80], centers=[[0, 0], [4, 0], [2, 4]], cluster_std=[0.4, 1.0, 0.25], random_state=0) # [snippet:density] snn = SharedNearestNeighborClustering(k=15, eps=5, min_pts=8).fit(X) dp = DensityPeaks(n_clusters=3, density="gaussian", percent=2.0).fit(X) # [/snippet:density] print("SNN:", snn.n_clusters_, "clusters, noise", (snn.labels_ < 0).sum(), " rate", clustering_rate(y, snn.labels_)) print("density peaks rate:", clustering_rate(y, dp.labels_)) fig, axes = plt.subplots(1, 3, figsize=(13, 4)) plot_partition(X, snn.labels_, ax=axes[0], title="SNN density clustering (gray: noise)") plot_decision_graph(dp.rho_, dp.delta_, dp.centers_, ax=axes[1]) plot_partition(X, dp.labels_, ax=axes[2], title="density peaks, 3 centers") for i in range(len(X)): j = dp.nearest_denser_[i] if j >= 0: axes[2].plot(*X[[i, j]].T, color="#C9D3DD", lw=0.6, zorder=0) for ax in (axes[0], axes[2]): ax.set_xticks([]) ax.set_yticks([]) plt.tight_layout() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.176 seconds) .. _sphx_glr_download_auto_examples_04_neighborhoods_plot_04_snn_density_peaks.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/04_neighborhoods/plot_04_snn_density_peaks.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_04_snn_density_peaks.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_04_snn_density_peaks.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_04_snn_density_peaks.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_