在scikit-learn中使用亲和传播使用亲和矩阵进行聚类

时间:2017-09-14 21:09:09

标签: python scikit-learn cluster-analysis

我尝试使用预先计算的亲和度矩阵进行聚类,但即使对于简单的情况它也不起作用。我尝试了不同的转储参数和对角线的不同值,但没有成功。

以下是示例。

affinities = 
    [[ 0. -1. -2. -6. -7.]
     [-1.  0. -1. -7. -8.]
     [-2. -1.  0. -8. -9.]
     [-6. -7. -8.  0. -1.]
     [-7. -8. -9. -1.  0.]]

我尝试使用sklearn Affinity Propagation模块的fit(..)方法聚类矩阵:

import sklearn.cluster
clusterer = sklearn.cluster.AffinityPropagation(affinity='precomputed', damping=0.9, verbose=True)
result = clusterer.fit(affinities)

from pprint import pprint
pprint(vars(result))

但是没有发现任何聚类(请注意结果显然应该是[0,0,0,1,1]):

Converged after 23 iterations.
{'affinity': 'precomputed',
 'affinity_matrix_': array([[ 0., -1., -2., -6., -7.],
       [-1.,  0., -1., -7., -8.],
       [-2., -1.,  0., -8., -9.],
       [-6., -7., -8.,  0., -1.],
       [-7., -8., -9., -1.,  0.]]),
 'cluster_centers_indices_': array([0]),
 'convergence_iter': 15,
 'copy': True,
 'damping': 0.9,
 'labels_': array([0, 0, 0, 0, 0]),
 'max_iter': 200,
 'n_iter_': 24,
 'preference': None,
 'verbose': True}

1 个答案:

答案 0 :(得分:1)

使用参数damping(0.5)的默认值可以解决问题:

Converged after 59 iterations.
{'affinity': 'precomputed',
 'affinity_matrix_': array([[ 0., -1., -2., -6., -7.],
       [-1.,  0., -1., -7., -8.],
       [-2., -1.,  0., -8., -9.],
       [-6., -7., -8.,  0., -1.],
       [-7., -8., -9., -1.,  0.]]),
 'cluster_centers_indices_': array([1, 3]),
 'convergence_iter': 15,
 'copy': True,
 'damping': 0.5,
 'labels_': array([0, 0, 0, 1, 1]),
 'max_iter': 200,
 'n_iter_': 60,
 'preference': None,
 'verbose': True}
相关问题