Exemplar-based segmentation
You need clusters represented by actual observations rather than abstract centroids.
Scikit-Learn · Class
AffinityPropagation clusters data by exchanging messages to identify representative samples, or exemplars. It supports Euclidean similarities or a precomputed affinity matrix, with preferences influencing the number of clusters.
AffinityPropagation clusters samples by iteratively exchanging messages about which samples should represent others. It can calculate negative squared Euclidean similarities itself or consume a precomputed similarity matrix; preferences influence which samples become exemplars, while damping helps stabilize updates until convergence.
class sklearn.cluster.AffinityPropagation(*, damping=0.5, max_iter=200, convergence_iter=15, copy=True, preference=None, affinity='euclidean', verbose=False, random_state=None)| Name | Type | Description |
|---|---|---|
damping | float, default=0.5 | Damping factor between 0.5 and 1. |
max_iter | int, default=200 | Maximum number of iterations. |
convergence_iter | int, default=15 | Number of iterations with no change in the estimated cluster count that stops convergence. |
copy | bool, default=True | Whether to make a copy of the input data. |
preference | array-like of shape (n_samples,) or float, default=None | Preferences for points; a scalar or one value per sample. Larger values make points more likely to become exemplars. |
affinity | {'euclidean', 'precomputed'}, default='euclidean' | Affinity mode: 'euclidean' or 'precomputed'. |
verbose | bool, default=False | Whether to print verbose progress information. |
random_state | int, RandomState instance or None, default=None | Controls randomness where applicable. |
A fitted estimator with labels_ containing the cluster label for each sample and cluster_centers_indices_ containing indices of the selected cluster centers.
from sklearn.cluster import AffinityPropagation
from sklearn.datasets import make_blobs
X, _ = make_blobs(n_samples=60, centers=3, random_state=0)
model = AffinityPropagation(random_state=0).fit(X)
print(model.labels_)
print(model.cluster_centers_indices_)Creates synthetic points, fits affinity propagation, and prints each point's cluster label plus the selected exemplar indices.
You need clusters represented by actual observations rather than abstract centroids.
You have pairwise similarities instead of a conventional feature matrix.
The desired cluster count is not known in advance.
Affinity propagation selects representative samples, called exemplars, by exchanging messages between pairs of points. The fitting process returns labels and the indices of the selected cluster centers.
Follow-up: How does the estimator obtain similarities by default? With affinity='euclidean', it uses negative squared Euclidean distances between samples.
The preference controls how likely each point is to become an exemplar, and therefore influences the number of clusters. If omitted, preferences are derived from the median input similarities.
Follow-up: What happens when preferences are equal? The resulting number of clusters can be sensitive to the common preference relative to the similarities.
Damping is a factor between 0.5 and 1 that moderates message updates. Convergence is declared after the configured number of iterations with no change in the estimated cluster count, subject to max_iter.
Follow-up: Why might damping need adjustment? More damping can reduce oscillations in iterative message updates, at the cost of slower changes.
sklearn.cluster.affinity_propagationsklearn.clustercluster_centers_indices_labels_