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.

SNN density clustering (gray: noise), decision graph, density peaks, 3 centers
SNN: 3 clusters, noise 5  rate 0.9868421052631579
density peaks rate: 0.9947368421052631

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()

Total running time of the script: (0 minutes 0.176 seconds)

Gallery generated by Sphinx-Gallery