# Find Cluster Diameter and Associated Cluster Points with KMeans Clustering (scikit learn)

I have done clustering using Kmeans using sklearn. While it has a method to print the centroids, I am finding it rather bizarre that scikit-learn doesn't have a method to find out the cluster diameter (or that I have not seen it so far). Is there a neat way to obtain this for each cluster together with points associated with a cluster?

I currently have this rather kludgy code to do it

import numpy as np
from sklearn.cluster import KMeans
from sklearn import datasets

X = iris.data
y = iris.target

estimator = KMeans(n_clusters=3)
estimator.fit(X)
print({i: np.where(estimator.labels_ == i)[0] for i in range(estimator.n_clusters)}) #get the indices of points for each cluster

• What do you mean here by "length" of a cluster? diameter? number of points? Jun 6, 2018 at 20:35

For the time being, I've prepared a workaround solution:

#iris example
x = iris.data
y = iris.target

estimator = KMeans(n_clusters=3)
y_kmeans = estimator.fit_predict(x)


To get the clusters' radii you can use the following code snippet:

#empty dictionaries

clusters_centroids=dict()

'''looping over clusters and calculate Euclidian distance of
each point within that cluster from its centroid and
pick the maximum which is the radius of that cluster'''

for cluster in list(set(y)):

clusters_centroids[cluster]=list(zip(estimator.cluster_centers_[:, 0],estimator.cluster_centers_[:,1]))[cluster]
clusters_radii[cluster] = max([np.linalg.norm(np.subtract(i,clusters_centroids[cluster])) for i in zip(x[y_kmeans == cluster, 0],x[y_kmeans == cluster, 1])])


It will give you this:

• Implicitly assumes all clusters have the same radius
• Separates the data into Voronoi-cells (which can be seen from here as well).
• Cluster points (circles) can overlap (it is how it is defined).

If you want to relax the shape of the clusters (not strictly spherical or circles like K-means), you should perform Gaussian mixture models.

Appendix (To Reproduce the above Visualization):

#Visualising the clusters and cluster circles

fig, ax = plt.subplots(1,figsize=(7,5))

plt.scatter(x[y_kmeans == 0, 0], x[y_kmeans == 0, 1], s = 100, c = 'red', label = 'Iris-setosa')
art = mpatches.Circle(clusters_centroids[0],clusters_radii[0], edgecolor='r',fill=False)

plt.scatter(x[y_kmeans == 1, 0], x[y_kmeans == 1, 1], s = 100, c = 'blue', label = 'Iris-versicolour')
art = mpatches.Circle(clusters_centroids[1],clusters_radii[1], edgecolor='b',fill=False)

plt.scatter(x[y_kmeans == 2, 0], x[y_kmeans == 2, 1], s = 100, c = 'green', label = 'Iris-virginica')
art = mpatches.Circle(clusters_centroids[2],clusters_radii[2], edgecolor='g',fill=False)