📜  亲和力传播python代码示例

📅  最后修改于: 2022-03-11 14:46:32.605000             🧑  作者: Mango

代码示例1
from sklearn.cluster import AffinityPropagation
import numpy as np

X = np.array([[1, 2], [1, 4], [1, 0], [4, 2], [4, 4], [4, 0]])
clustering = AffinityPropagation(affinity = 'euclidean', random_state=5).fit(X)

labels = clustering.labels_ # label to each element
centers = clustering.cluster_centers_ # center of each cluster

# if you need a distance different from euclidean
# calculate your custom, pairwise distance among vectors 
# and store them into a matrix M. 
# Note: cluster_centers are no longer available

clustering = AffinityPropagation(affinity='precomputed', random_state=5).fit(M)