带有pycluster的加权聚类

时间:2015-04-08 05:17:30

标签: python k-means

我设法采用了代码片段来介绍如何使用PyCluster的k-means聚类算法。我希望能够对数据点进行加权,但不幸的是,我只能权衡这些功能。我是否遗漏了某些东西,或者我可以使用一些技巧让一些积分比其他积分更多?

import numpy as np
import Pycluster as pc

points = np.asarray([
    [1.0, 20, 30, 50],
    [1.2, 15, 34, 50],
    [1.6, 13, 20, 55],
    [0.1, 16, 40, 26],
    [0.3, 26, 30, 23],
    [1.4, 20, 28, 20],
])

# would like to specify 6 weights for each of the elements in `points`
weights = np.asarray([1.0, 1.0, 1.0, 1.0])

clusterid, error, nfound = pc.kcluster(
    points, nclusters=2, transpose=0, npass=10, method='a', dist='e', weight=weights
)
centroids, _ = pc.clustercentroids(points, clusterid=clusterid)
print centroids

2 个答案:

答案 0 :(得分:0)

加权单个数据点不是KMeans算法的一个特征。这是在算法定义中:它在pycluster,MLlib或TrustedAnalytics中不可用。

但是,您可以添加重复的数据点。例如,如果您希望第二个数据点计数两倍,请将列表更改为:

points = np.asarray([
    [1.0, 20, 30, 50],
    [1.2, 15, 34, 50],
    [1.2, 15, 34, 50],
    [1.6, 13, 20, 55],
    [0.1, 16, 40, 26],
    [0.3, 26, 30, 23],
    [1.4, 20, 28, 20],
])

答案 1 :(得分:0)

如今,您可以在sklearn的fit方法中使用sample_weights。这是example