Search code examples
pythonscikit-learnk-means

Value at KMeans.cluster_centers_ in sklearn KMeans


On doing K means fit on some vectors with 3 clusters, I was able to get the labels for the input data. KMeans.cluster_centers_ returns the coordinates of the centers and so shouldn't there be some vector corresponding to that? How can I find the value at the centroid of these clusters?


Solution

  • closest, _ = pairwise_distances_argmin_min(KMeans.cluster_centers_, X)

    The array closest will contain the index of the point in X that is closest to each centroid.

    Let's say the closest gave output as array([0,8,5]) for the three clusters. So X[0] is the closest point in X to centroid 0, and X[8] is the closest to centroid 1 and so on.

    Source: https://codedump.io/share/XiME3OAGY5Tm/1/get-nearest-point-to-centroid-scikit-learn