Tantaoui El Mehdi Tantaoui El Mehdi - 9 months ago 56
Python Question

get the ith cluster after performing K-means algorithm in python

I have multiple points in a matrix that I tried to classify using K-means algorithm using 'scikit-learn' library in Python. Here is what I've done so far :

k_means = KMeans(init='k-means++', k=5, n_init=10)
k_means.fit(X) ## X is the matrix containing the data
... # some code to plot

The result is the following :

enter image description here

Now I want to get the points that are in the "red" cluster for example. How can I do that with

Answer Source

I think I found it. It simple: the object km have severale attributes. Among those attributes there is labels that gives for every point to which cluster it belongs:

km_labels = km.labels_
#to get all the items in the first cluster just get the indexes int km_labels with the

#value = 1

index = [x[0] for x, value in numpy.ndenumerate(km_labels) if value==1]