Skip to content Skip to sidebar Skip to footer

K-means In Python: Determine Which Data Are Associated With Each Centroid

I've been using scipy.cluster.vq.kmeans for doing some k-means clustering, but was wondering if there's a way to determine which centroid each of your data points is (putativly) as

Solution 1:

There is a function kmeans2 in scipy.cluster.vq that returns the labels, too.

In [8]: X = scipy.randn(100, 2)

In [9]: centroids, labels = kmeans2(X, 3)

In [10]: labels
Out[10]: 
array([2, 1, 2, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 2, 2, 1, 2, 1, 2, 1, 2, 0,
       1, 0, 2, 0, 1, 2, 0, 1, 0, 1, 1, 2, 2, 2, 2, 1, 2, 1, 1, 1, 2, 0, 0,
       2, 2, 0, 1, 0, 0, 0, 2, 2, 2, 0, 0, 1, 2, 1, 0, 0, 0, 2, 1, 1, 1, 1,
       1, 0, 0, 1, 0, 1, 2, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 2, 0, 2, 2, 0,
       1, 1, 0, 1, 0, 0, 0, 2])

Otherwise, if you must use kmeans, you can also use vq to get labels:

In [17]: from scipy.cluster.vq import kmeans, vq

In [18]: codebook, distortion = kmeans(X, 3)

In [21]: code, dist = vq(X, codebook)

In [22]: code
Out[22]: 
array([1, 0, 1, 0, 2, 2, 2, 0, 1, 1, 0, 2, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1,
       2, 2, 1, 2, 0, 1, 1, 0, 2, 2, 0, 1, 0, 1, 0, 2, 1, 2, 0, 2, 1, 1, 1,
       0, 1, 2, 0, 1, 2, 2, 1, 1, 1, 2, 2, 0, 0, 2, 2, 2, 2, 1, 0, 2, 2, 2,
       0, 1, 1, 2, 1, 0, 0, 0, 0, 1, 2, 1, 2, 0, 2, 0, 2, 2, 1, 1, 1, 1, 1,
       2, 0, 2, 0, 2, 1, 1, 1])

Documentation: scipy.cluster.vq

Post a Comment for "K-means In Python: Determine Which Data Are Associated With Each Centroid"