Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Scikit-Learn · Class

AffinityPropagation

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.

How it works

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.

Syntax

class sklearn.cluster.AffinityPropagation(*, damping=0.5, max_iter=200, convergence_iter=15, copy=True, preference=None, affinity='euclidean', verbose=False, random_state=None)

Parameters

NameTypeDescription
dampingfloat, default=0.5Damping factor between 0.5 and 1.
max_iterint, default=200Maximum number of iterations.
convergence_iterint, default=15Number of iterations with no change in the estimated cluster count that stops convergence.
copybool, default=TrueWhether to make a copy of the input data.
preferencearray-like of shape (n_samples,) or float, default=NonePreferences 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'.
verbosebool, default=FalseWhether to print verbose progress information.
random_stateint, RandomState instance or None, default=NoneControls randomness where applicable.

Returns

A fitted estimator with labels_ containing the cluster label for each sample and cluster_centers_indices_ containing indices of the selected cluster centers.

Key points

  • This estimator performs unsupervised affinity-propagation clustering and chooses representative training samples.
  • Use affinity='euclidean' for the estimator's negative squared Euclidean similarities, or affinity='precomputed' for an input similarity matrix.
  • Preferences may be one scalar or one value per sample, and larger values make exemplar selection more likely.
  • Preference values are the main control for how many exemplars, and therefore clusters, are found.
  • convergence_iter specifies how long the estimated cluster count must remain unchanged before convergence can stop fitting.
  • With Euclidean affinity, fitting constructs the similarity matrix from negative distances before applying the clustering procedure.

Common mistakes

  • Memory and runtime grow quadratically with the number of samples, so this estimator can become impractical for large datasets.
  • Changing preference values changes the likely number and identity of cluster representatives.
  • The affinity option is limited to Euclidean similarity or a supplied precomputed matrix.

Example: Cluster points with affinity propagation

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.

When to use it

Exemplar-based segmentation

You need clusters represented by actual observations rather than abstract centroids.

Custom similarity clustering

You have pairwise similarities instead of a conventional feature matrix.

Preference-driven cluster discovery

The desired cluster count is not known in advance.

Interview questions

QWhat does AffinityPropagation produce?

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.

QHow does preference affect clustering?

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.

QWhat are damping and convergence_iter used for?

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_propagation
  • sklearn.cluster
  • cluster_centers_indices_
  • labels_

Source: AffinityPropagation — scikit-learn 1.9.1 documentation. Checked 2026-10-15.