Reputation: 3380
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
?
Upvotes: 1
Views: 1804
Reputation: 3380
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]
Upvotes: 1
Reputation: 1085
KMeans is a formula rather than a database. By that I mean it doesn't keep a record of the value for each record, it records a function that will assign the cluster given the input again. To get cluster assignment,there's a method called 'predict'.
kmeans.predict(value)
This will give you the cluster assignment from kmeans. If you iterate through your points you should be able to get them.
Upvotes: 1