Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Scikit learn Scikit learn AffinityPropagation

From Leeroopedia


Knowledge Sources
Domains Clustering, Unsupervised Learning
Last Updated 2026-02-08 15:00 GMT

Overview

Concrete tool for performing affinity propagation clustering provided by scikit-learn.

Description

AffinityPropagation is a clustering algorithm that identifies exemplars among data points and forms clusters by passing messages between data points. Unlike K-Means, it does not require specifying the number of clusters in advance; instead, it determines the number of clusters based on preference values and pairwise similarity. The algorithm iteratively refines responsibility and availability matrices until convergence or until the maximum number of iterations is reached.

Usage

Use AffinityPropagation when you need a clustering algorithm that automatically determines the number of clusters from the data, especially when you have a precomputed similarity matrix or want to identify representative exemplars. It is well-suited for small to medium datasets where the number of clusters is unknown.

Code Reference

Source Location

Signature

class AffinityPropagation(ClusterMixin, BaseEstimator):
    def __init__(
        self,
        *,
        damping=0.5,
        max_iter=200,
        convergence_iter=15,
        copy=True,
        preference=None,
        affinity="euclidean",
        verbose=False,
        random_state=None,
    ):

Import

from sklearn.cluster import AffinityPropagation

I/O Contract

Inputs

Name Type Required Description
damping float No Damping factor in the range [0.5, 1.0) controlling message update smoothing. Default is 0.5.
max_iter int No Maximum number of iterations. Default is 200.
convergence_iter int No Number of iterations with no change in cluster count to declare convergence. Default is 15.
copy bool No Whether to make a copy of input data. Default is True.
preference array-like or float No Preferences for each point; higher values increase likelihood of being chosen as exemplar. Default is None (median of similarities).
affinity str No Affinity metric to use: "euclidean" or "precomputed". Default is "euclidean".
verbose bool No Whether to print progress messages. Default is False.
random_state int or RandomState No Random state for reproducibility. Default is None.

Outputs

Name Type Description
cluster_centers_indices_ ndarray of shape (n_clusters,) Indices of cluster centers (exemplars).
cluster_centers_ ndarray of shape (n_clusters, n_features) Cluster center coordinates.
labels_ ndarray of shape (n_samples,) Cluster label for each sample.
affinity_matrix_ ndarray of shape (n_samples, n_samples) Affinity matrix used for clustering.
n_iter_ int Number of iterations taken to converge.

Usage Examples

Basic Usage

from sklearn.cluster import AffinityPropagation
import numpy as np

X = np.array([[1, 2], [1, 4], [1, 0],
              [10, 2], [10, 4], [10, 0]])

clustering = AffinityPropagation(random_state=5).fit(X)
print(clustering.labels_)
print(clustering.cluster_centers_)

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment