affinity_propagation#
- sklearn.cluster.affinity_propagation(S,*,preference=None,convergence_iter=15,max_iter=200,damping=0.5,copy=True,verbose=False,return_n_iter=False,random_state=None)[source]#
Perform Affinity Propagation Clustering of data.
Read more in theUser Guide.
- Parameters:
- Sarray-like of shape (n_samples, n_samples)
Matrix of similarities between points.
- preferencearray-like of shape (n_samples,) or float, default=None
Preferences for each point - points with larger values ofpreferences are more likely to be chosen as exemplars. The number ofexemplars, i.e. of clusters, is influenced by the input preferencesvalue. If the preferences are not passed as arguments, they will beset to the median of the input similarities (resulting in a moderatenumber of clusters). For a smaller amount of clusters, this can be setto the minimum value of the similarities.
- convergence_iterint, default=15
Number of iterations with no change in the numberof estimated clusters that stops the convergence.
- max_iterint, default=200
Maximum number of iterations.
- dampingfloat, default=0.5
Damping factor between 0.5 and 1.
- copybool, default=True
If copy is False, the affinity matrix is modified inplace by thealgorithm, for memory efficiency.
- verbosebool, default=False
The verbosity level.
- return_n_iterbool, default=False
Whether or not to return the number of iterations.
- random_stateint, RandomState instance or None, default=None
Pseudo-random number generator to control the starting state.Use an int for reproducible results across function calls.See theGlossary.
Added in version 0.23:this parameter was previously hardcoded as 0.
- Returns:
- cluster_centers_indicesndarray of shape (n_clusters,)
Index of clusters centers.
- labelsndarray of shape (n_samples,)
Cluster labels for each point.
- n_iterint
Number of iterations run. Returned only if
return_n_iterisset to True.
Notes
For an example usage,seeDemo 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_indicesand 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 centerand label
0for every sample will be returned. Otherwise, everytraining sample becomes its own cluster center and is assigned a uniquelabel.References
Brendan J. Frey and Delbert Dueck, “Clustering by Passing MessagesBetween Data Points”, Science Feb. 2007
Examples
>>>importnumpyasnp>>>fromsklearn.clusterimportaffinity_propagation>>>fromsklearn.metrics.pairwiseimporteuclidean_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_indicesarray([0, 3])>>>labelsarray([0, 0, 0, 1, 1, 1])
