how to describe each group after a kmeans?

I made a Kmeans algorithm and plot the result. Everything is going well but I want to know which individuals are in which group. is there a way (and what it is) to get individuals from a particular group? Thank you for your answers.

Upvotes: 0

Views: 179

Answers (2)

Peaches
Peaches

Reputation: 48

Your question is unclear.

Here are the assumptions with respect to your questions,

  1. You want to get the label for the given data-point
from sklearn.cluster import KMeans
from sklearn import datasets
import numpy as np

centers = [[1, 1], [-1, -1], [1, -1]]
iris = datasets.load_iris()
X = iris.data
y = iris.target

km = KMeans(n_clusters=3)
km.fit(X)

Now def a function to extract the labels, using numpy

def ClusterIndices(clustNum, labels_array): #numpy 
    return np.where(labels_array == clustNum)[0]

Now for retrieving the labels using the same functions

ClusterIndices(1, km.labels_)
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
   17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
   34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49])

Get the datapoints

X[ClusterIndicesNumpy(1,km.labels_)]
array([[5.1, 3.5, 1.4, 0.2],
       [4.9, 3. , 1.4, 0.2],
       [4.7, 3.2, 1.3, 0.2],
       [4.6, 3.1, 1.5, 0.2],
       [5. , 3.6, 1.4, 0.2],
       [5.4, 3.9, 1.7, 0.4],
       [4.6, 3.4, 1.4, 0.3],
       [5. , 3.4, 1.5, 0.2],
       [4.4, 2.9, 1.4, 0.2],
       [4.9, 3.1, 1.5, 0.1],...[4.8, 3. , 1.4, 0.3],
   [5.1, 3.8, 1.6, 0.2],
   [4.6, 3.2, 1.4, 0.2],
   [5.3, 3.7, 1.5, 0.2],
   [5. , 3.3, 1.4, 0.2]])

Upvotes: 1

Danylo Baibak
Danylo Baibak

Reputation: 2316

Here is how I solved the kind of similar task:

import matplotlib.pyplot as plt
colors = ["g", "r", "m", "c", "y", "k"]

for i in range(len(feature_coords)):
    plt.scatter(feature_coords[i][0], feature_coords[i][1], c=colors[kmeans.labels_[i]], s=10)

plt.title('Clusterization of the topics')
plt.show()

Upvotes: 0

Related Questions