1
$\begingroup$

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

enter image description here

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

x = 0.10
i=0
C_i = np.where(labels == i)[0].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

enter image description here

New addition:

suppose my loading file name is train. now using train.loc[26] command I get the info of that specific id.

enter image description here

But I need to collect all info into a new data frame like as train dataframe

$\endgroup$
3
  • $\begingroup$ Is there a problem left? $\endgroup$ – Emre Mar 9 '18 at 17:15
  • $\begingroup$ No. I already solved that problem and I also added the solution of my problem. $\endgroup$ – IS2057 Mar 18 '18 at 15:35
  • $\begingroup$ Please add the solution as an answer and accept it. $\endgroup$ – Emre Mar 19 '18 at 0:03
1
$\begingroup$

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".

$\endgroup$
0
1
$\begingroup$

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)[0].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)[0].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)

enter image description here

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()

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.