# How to extract all information by using id

I need to apply classifier algorithm after clustering. Now after clustering I find the id numbers that which id belongs which cluster. I clustered them into 2 cluster.

Now I need to collect those data by using those id. But I don't know how I can collect all information by using those id.

As I use jupyeter notebook and in main data I have no attribute named id and those id assigned jupyter notebook when I load data from main data file.

This is my main data Here is my code to find which data belongs which cluster.

x = 0.10
i=0
C_i = np.where(labels == i).tolist()
n_i = len(C_i) # number of points in cluster i

# (2) indices of the points from X to be sampled from cluster i
sample_i = np.random.choice(C_i, int(x * n_i))
print (i, sample_i)


and after clustering I find these ids suppose my loading file name is train. now using train.loc command I get the info of that specific id. But I need to collect all info into a new data frame like as train dataframe

• Is there a problem left?
– Emre
Mar 9 '18 at 17:15
• No. I already solved that problem and I also added the solution of my problem. Mar 18 '18 at 15:35
– Emre
Mar 19 '18 at 0:03

Define a new column, then use these ids to select the relevant rows and set that column to the appropriate id:

from pandas import DataFrame,Series
from numpy.random import rand
df = DataFrame(rand(10,10)).assign(cluster=0)
clusters = Series([[1,3,5],[0,2,4,6,7,8,9]])
for cluster,rows in clusters.iteritems():
df.loc[rows]["cluster"] = cluster


Now you can do your groupbys on column "cluster".

Solution:

To create a list by using those index number like. Suppose I need 10% data's index number. At first I collect 0 (where i=0) number cluster's index numbers then 1(where i=1) number cluster index numbers

x = 0.10 i=0 C_i = np.where(labels == i).tolist() n_i = len(C_i) #indices of the points from X to be sampled from cluster i sample_i = np.random.choice(C_i, int(x * n_i)) print (i, sample_i) list1=(sample_i)

x = 0.10 i=1 C_i = np.where(labels == i).tolist() n_i = len(C_i) # indices of the points from X to be sampled from cluster i sample_i = np.random.choice(C_i, int(x * n_i)) print (i, sample_i) list2=(sample_i) After found two list for 2 cluster I merge those two list into 1 list

new_list = np.concatenate((list1,list2)) new_train_data=train.loc[new_list] new_train_data.head()