Perform Affinity Propagation Clustering of data.
Read more in the User Guide.
Matrix of similarities between points.
Preferences for each point - points with larger values of preferences are more likely to be chosen as exemplars. The number of exemplars, i.e. of clusters, is influenced by the input preferences value. If the preferences are not passed as arguments, they will be set to the median of the input similarities (resulting in a moderate number of clusters). For a smaller amount of clusters, this can be set to the minimum value of the similarities.
Number of iterations with no change in the number of estimated clusters that stops the convergence.
Maximum number of iterations.
Damping factor between 0.5 and 1.
If copy is False, the affinity matrix is modified inplace by the algorithm, for memory efficiency.
The verbosity level.
Whether or not to return the number of iterations.
Pseudo-random number generator to control the starting state. Use an int for reproducible results across function calls. See the Glossary.
Added in version 0.23: this parameter was previously hardcoded as 0.
Index of clusters centers.
Cluster labels for each point.
Number of iterations run. Returned only if return_n_iter is set to True.
For an example usage, see Demo of affinity propagation clustering algorithm. You may also check out, Visualizing the stock market structure
When the algorithm does not converge, it will still return a arrays of cluster_center_indices and labels if there are any exemplars/clusters, however they may be degenerate and should be used with caution.
When all training samples have equal similarities and equal preferences, the assignment of cluster centers and labels depends on the preference. If the preference is smaller than the similarities, a single cluster center and label 0 for every sample will be returned. Otherwise, every training sample becomes its own cluster center and is assigned a unique label.
Brendan J. Frey and Delbert Dueck, “Clustering by Passing Messages Between Data Points”, Science Feb. 2007
>>> import numpy as np >>> from sklearn.cluster import affinity_propagation >>> from sklearn.metrics.pairwise import euclidean_distances >>> X = np.array([[1, 2], [1, 4], [1, 0], ... [4, 2], [4, 4], [4, 0]]) >>> S = -euclidean_distances(X, squared=True) >>> cluster_centers_indices, labels = affinity_propagation(S, random_state=0) >>> cluster_centers_indices array([0, 3]) >>> labels array([0, 0, 0, 1, 1, 1])
© 2007–2025 The scikit-learn developers
Licensed under the 3-clause BSD License.
https://scikit-learn.org/1.6/modules/generated/sklearn.cluster.affinity_propagation.html