且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Python将k-means集群关联到实例

更新时间:2023-12-02 22:35:46

下面是一个示例,假设您将数据从文件中读取到列表中:

Here's an example, assuming you read the data into a list from file:

import sklearn.cluster
import numpy as np

data = [
    ['bob', 1, 3, 7],
    ['joe', 2, 4, 8],
    ['bill', 1, 6, 4],
]

labels = [x[0] for x in data]
a = np.array([x[1:] for x in data])
clust_centers = 2

model = sklearn.cluster.k_means(a, clust_centers)

模型现在包含一个具有(质心,标签,间质)的元组

model now contains a tuple with (centroids, labels, intertia)

所以像这样重新获得标签:

So get the labels back like this:

clusters = dict(zip(lables, model[1]))

并打印"one"的集群ID:

And to print the cluster id for 'one':

print clusters['bob']

或将其发送回csv,如下所示:

Or send it back out to a csv like this:

for d in data:
    print '%s,%d' % (','.join([str(x) for x in d]), clusters[d[0]])