How could I find the indexes of data in each cluster after applying the PCA and a clustering method?

Viewed 1673

I would like to order each point of the plot to its corresponding index in the table.
Here are the first three lines of data I'm working with.

data to analyse

After applying the PCA (2D) to my data, I get a plot similar to this, and then I use the kmeans algorithm for clustering.

(possible)clustering using kmeans.
Suppose I take all data in the red cluster. How could I find the indexes of those data set in the table ?
Or how could I find the indexes of each point in the table ?
The goal is to be able to order each point in the plot to its corresponding line in the table. I use Python.
Articles or documents that explain it will also be welcome. Thank you in advance.

1 Answers

You can find the indexes of points in each cluster as below.

Suppose data with 12 samples each with three features. PCA is used to decrease the number of features. Then, k-means is used to cluster the data.

import numpy as np
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt
from sklearn import decomposition

data=np.array([[5,4.5,6],[1,1,2],[1,2,1.5],[8,8.5,8],[2,2.5,1.5],[4,4.5,5],[8.5,8,9],[4,6,5.5],[5,6,5],[8,9,8.5],[9,9,8],[9,8,9]])

pca = decomposition.PCA(n_components=2) # apply pca
pca.fit(data)
data2 = pca.transform(data) # new data saved in data2
print("data with two features:\n", data2)
plt.plot(data2[:,0],data2[:,1],'ro')
for i in range(data2.shape[0]):        
        plt.text(data2[i,0],data2[i,1], str(i), fontsize=12)
plt.show()

#run Kmeans on data2 with 3 clusters
km=KMeans(n_clusters=3) # number of clusters =3 
km=km.fit(data2)
cluster_labels=km.labels_ # get cluster label of all data
print("cluster labels of points:", cluster_labels)

# get indexes of points in each cluster 
#Note: you can use these indexes in both data and data2
index_cluster_0=np.where(cluster_labels==0)[0] # get indexes of points in cluster 0 
print("indexes of points in cluster 0:", index_cluster_0)
index_cluster_1=np.where(cluster_labels==1)[0] # get indexes of points in cluster 1
print("indexes of points in cluster 1:", index_cluster_1)
index_cluster_2=np.where(cluster_labels==2)[0] # get indexes of points in cluster 2
print("indexes of points in cluster 2:", index_cluster_2)

#plot the results
plt.plot(data2[index_cluster_0,0],data2[index_cluster_0,1],'ro') #samples in cluster 0 are red
plt.plot(data2[index_cluster_1,0],data2[index_cluster_1,1],'bo') #samples in cluster 1 are blue
plt.plot(data2[index_cluster_2,0],data2[index_cluster_2,1],'go') #samples in cluster 2 are green
plt.title('Cluster 0: red, Cluster 1: blue, Cluster 2: green')
plt.show()

Output:

 data with two features:
 [[ 0.78528736  1.06750913]
 [ 7.42907267  0.7576433 ]
 [ 7.15249919 -0.32552917]
 [-4.40158551 -0.37613828]
 [ 6.26503561 -0.55127462]
 [ 1.95747537  0.30362524]
 [-4.9903965   0.69673761]
 [ 0.83759652 -0.57439496]
 [ 0.51143086 -0.70659728]
 [-4.96288343 -0.46968141]
 [-5.28904909 -0.60188372]
 [-5.29448305  0.77998415]]

enter image description here

cluster labels of points: [2 1 1 0 1 2 0 2 2 0 0 0]
indexes of points in cluster 0: [ 3  6  9 10 11]
indexes of points in cluster 1: [1 2 4]
indexes of points in cluster 2: [0 5 7 8]

enter image description here

Note that you can use the obtained indexes in both original data and the data achieved by PCA.

Related