r"""
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.
"""

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