This is not a medodea, but here you can try:
>>> import numpy as np
>>> from sklearn.cluster import KMeans
>>> from sklearn.metrics import pairwise_distances_argmin_min
>>> X = np.random.randn(10, 4)
>>> km = KMeans(n_clusters=2).fit(X)
>>> closest, _ = pairwise_distances_argmin_min(km.cluster_centers_, X)
>>> closest
array([0, 8])
The array closestcontains the index of point in X, closest to each centroid. Thus, X[0]it is the nearest point to Xto the centroid of 0, and X[8]is closest to the centroid 1.
source
share