Bobby M
Bobby M

Reputation: 159

in Python how to find the sum of point-to-centroid distance in each cluster

in Matlab the kmeans function can give sumd, which is the within-cluster sums of point-to-centroid distances in the k-by-1 vector.

[idx,C,sumd] = kmeans(___) 

i need to do this in python.

I have found that km.transform returns an array of distances form cluster

array([[0.13894406, 2.90411146],
       [3.25560603, 0.21255051],
       [2.43748321, 0.60557231],
       [1.16330349, 4.20635901],
       [0.53391368, 2.50914184],
       [3.43498204, 0.39192652]])

if i do km.predict i get the identity of the clusters

array([0, 1, 1, 0, 0, 1], dtype=int32)

I'm struggling to figure out how i can calculate the mean distance for each cluster.

any suggestions would be appreciated

Upvotes: 0

Views: 777

Answers (2)

Paul Panzer
Paul Panzer

Reputation: 53099

You can use np.bincount:

dists = np.array([[0.13894406, 2.90411146],
                  [3.25560603, 0.21255051],
                  [2.43748321, 0.60557231],
                  [1.16330349, 4.20635901],
                  [0.53391368, 2.50914184],
                  [3.43498204, 0.39192652]])
ids = np.array([0, 1, 1, 0, 0, 1], dtype=np.int32)
np.bincount(ids, dists[np.arange(len(dists)), ids]) / np.bincount(ids)
# array([0.61205374, 0.40334978])

Upvotes: 0

James
James

Reputation: 36756

You can get the distance each row is to the nearest cluster using:

dist = np.array([[0.13894406, 2.90411146],
    [3.25560603, 0.21255051],
    [2.43748321, 0.60557231],
    [1.16330349, 4.20635901],
    [0.53391368, 2.50914184],
    [3.43498204, 0.39192652]])

labels = np.array([0, 1, 1, 0, 0, 1])

d_closest = dist[np.arange(len(dist)), labels]

Then to calculate the per-cluster mean distance (you can also do this in numpy as an array with the index as the label, I find the dictionary more intuitive):

avg_dist_map = {k: d_closest[labels==k].mean() for k in set(labels)}
avg_dist_map
# returns:
{0: 0.6120537433333334, 1: 0.40334978000000005}

Upvotes: 1

Related Questions