Note
Go to the end to download the full example code or to run this example in your browser via Binder.
Jarvis–Patrick clustering and its two integer parameters#
Jarvis–Patrick clustering links two samples when each is among the other’s
k nearest neighbors and they share at least l of them; clusters are
the connected components. The method finds clusters of different density,
but its crisp threshold makes the result jump with both parameters: the same
data give six, five, three or four groups, and only some settings separate the
outlier from the three clusters.

k=6 l=2: 6 groups, clustering rate 0.96
k=8 l=4: 6 groups, clustering rate 0.96
k=12 l=6: 3 groups, clustering rate 0.74
k=12 l=8: 4 groups, clustering rate 1.00
import matplotlib.pyplot as plt
from graphclust import JarvisPatrick
from graphclust.datasets import make_variety
from graphclust.plotting import plot_partition
from graphclust.validity import clustering_rate
X, y = make_variety()
fig, axes = plt.subplots(1, 4, figsize=(14, 3.7))
for ax, (k, l) in zip(axes, [(6, 2), (8, 4), (12, 6), (12, 8)]):
# [snippet:jp]
jp = JarvisPatrick(k=k, min_shared=l).fit(X)
# [/snippet:jp]
rate = clustering_rate(y, jp.labels_)
plot_partition(X, jp.labels_, ax=ax, markers=False, title=f"k = {k}, l = {l}: {jp.n_clusters_} groups, {100 * rate:.0f}%")
ax.set_xticks([])
ax.set_yticks([])
print(f"k={k} l={l}: {jp.n_clusters_} groups, clustering rate {rate:.2f}")
plt.tight_layout()
Total running time of the script: (0 minutes 0.066 seconds)