Tantaoui El Mehdi - 1 year ago 77

Python Question

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 :

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

`sklearn`

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]
```