I have done clustering using Kmeans using sklearn. While it has a method to print the centroids, I am finding it rather bizzare that scikit-learn doesn't have a method to print out the cluster-points of each cluster (or that I have not seen it so far). Is there a neat way to get the cluster-points of each cluster?
I currently have this rather cludgy code to do it, where V is the dataset:
def getClusterPoints(V, labels):
clusters = {}
for l in range(0, max(labels)+1):
data_points = []
indices = [i for i, x in enumerate(labels) if x == l]
for idx in indices:
data_points.append(V[idx])
clusters[l] = data_points
return clusters
Suggestions/links are much appreciated.
Thanks!
PD.
Best Answer
For example
You can get clusters of each point by
Out:
Then get the indices of points for each cluster
Out:
Edit
If you want to use array of points in
X
as values rather than the array of indices: