-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathunsupervised.py
403 lines (277 loc) · 12.2 KB
/
unsupervised.py
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
# coding: utf-8
# Yitzhak Cohen
# This program attempts to get as many conclusions as possible , using only unsupervised methods.
# It has 2 parts:
# 1. data analysis
# 2. Clustering and Dimension reduction methods.
# In[2]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
from sklearn import metrics
import matplotlib.pyplot as plt
get_ipython().magic(u'matplotlib inline')
import gc
from itertools import combinations
from sklearn.decomposition import PCA
from scipy.spatial import distance
from sklearn.cluster import SpectralClustering
from sklearn import metrics
# In[7]:
# Read the Data
diabetic_data = pd.read_csv('dataset_diabetes/diabetic_data.csv')
# In[3]:
# The icd9 codes description , just to understand the diagnoses.
diag_codes = pd.read_csv('icd9.csv',names=['code','diag_description'])
diag_codes_dict = dict(zip(diag_codes['code'].values,diag_codes['diag_description'].values))
def get_description(diag):
if diag in diag_codes_dict.keys():
ret = diag_codes_dict[diag]
else:
ret = diag
return ret
# In[6]:
# The description of the features : admission_source_id , discharge_disposition_id, admission_type_id
ids_mapping_dict = {}
key = ''
with open('dataset_diabetes/IDs_mapping.csv','r') as f:
for line in f.readlines():
line = line.strip()
if line == ',':
continue
vals = line.split(',')
if vals[1] == 'description':
key = vals[0]
ids_mapping_dict[key] = {}
continue
else:
ids_mapping_dict[key][vals[0]]= vals[1]
# In[2]:
# Seperate the columns into numerical and nominal columns , they should be handled differently.
nominal_columns = [u'race', u'gender', u'age',
u'admission_type_id', u'discharge_disposition_id',
u'admission_source_id', u'payer_code',
u'medical_specialty', u'diag_1', u'diag_2', u'diag_3',
u'max_glu_serum', u'A1Cresult', u'change', u'diabetesMed', u'readmitted']
numeric_columns = [u'time_in_hospital',u'num_lab_procedures', u'num_procedures',
u'num_medications', u'number_outpatient', u'number_emergency',
u'number_inpatient','number_diagnoses']
medication_columns = [u'metformin',u'repaglinide', u'nateglinide', u'chlorpropamide', u'glimepiride',
u'acetohexamide', u'glipizide', u'glyburide', u'tolbutamide',
u'pioglitazone', u'rosiglitazone', u'acarbose', u'miglitol',
u'troglitazone', u'tolazamide', u'examide', u'citoglipton', u'insulin',
u'glyburide-metformin', u'glipizide-metformin',
u'glimepiride-pioglitazone', u'metformin-rosiglitazone',
u'metformin-pioglitazone']
cols_to_remove = [u'encounter_id', u'patient_nbr',u'weight']
# In[8]:
# Get the features admission_source_id , discharge_disposition_id, admission_type_id description instead of integer code.
diabetic_data['admission_source_id'] = diabetic_data['admission_source_id'].apply(lambda x: ids_mapping_dict['admission_source_id'][str(x)])
diabetic_data['discharge_disposition_id'] = diabetic_data['discharge_disposition_id'].apply(lambda x: ids_mapping_dict['discharge_disposition_id'][str(x)])
diabetic_data['admission_type_id'] = diabetic_data['admission_type_id'].apply(lambda x: ids_mapping_dict['admission_type_id'][str(x)])
# In[9]:
# Ge the diagnoses description , instead of icd9 code.
diabetic_data['diag_1'] = diabetic_data['diag_1'].apply(get_description)
diabetic_data['diag_2'] = diabetic_data['diag_2'].apply(get_description)
diabetic_data['diag_3'] = diabetic_data['diag_3'].apply(get_description)
# # Features Connection
# This is the data analysis part, find which nominal features are connected , and which numerical features are correlated
# In[9]:
# Since some patients have more than one encounter , only keep analyze each patient's first encounter.
diabetic_data['encounter_id'] = diabetic_data['encounter_id'].astype('int')
patient_first_encounter = diabetic_data[[u'encounter_id', u'patient_nbr']].groupby('patient_nbr').min()['encounter_id'].reset_index()
diabetic_data_first_encounter = diabetic_data.merge(patient_first_encounter,on=['patient_nbr','encounter_id'])
# In[ ]:
diabetic_data_first_encounter[numeric_columns].corr().to_csv('corr.csv')
# In[112]:
arr = []
threshold = 1.5
samples_thres = 100
for comb in combinations(nominal_columns,2):
ct = pd.crosstab(diabetic_data_first_encounter[comb[0]],diabetic_data_first_encounter[comb[1]])
norm_ct = ct.fillna(0).apply(lambda r: r/r.sum(), axis=1).apply(lambda r: r/r.sum(), axis=1)
norm_ct = norm_ct[ct>samples_thres].fillna(0)
distribution = diabetic_data_first_encounter[comb[1]].value_counts(normalize=True)
for col in norm_ct.columns:
norm_ct[col] = norm_ct[col] / distribution[col]
res = norm_ct[(norm_ct> threshold) | (norm_ct < (1.0/threshold))].dropna(how='all',axis=1).dropna(how='all',axis=0)
if res.shape[0] > 0:
col1 = res.index.name
col2 = res.columns.name
for row in res.iterrows():
for idx,val in enumerate(row[1],0):
if val > 0:
arr.append([col1,row[0],col2,row[1].keys()[idx],val,ct.loc[row[0],row[1].keys()[idx]]])
res_df = pd.DataFrame(arr,columns = ['feature 1','value 1','feature 2','value 2','ratio','num samples'])
res_df.to_csv('res_first_encounter.csv',index=False)
# In[125]:
# Some interesting polts
arr = []
for age in diabetic_data_first_encounter['age'].unique():
try:
val = diabetic_data_first_encounter[diabetic_data_first_encounter['age']==age] ['diag_1'].value_counts(normalize=True)['250.13']
except:
val = 0
arr.append([age,val])
plt_df = pd.DataFrame(arr,columns=['age','probability'])
plt_df.set_index('age').plot(kind='bar')
# # Clustering
# This section attempts to create cluster using several techniques
# ### 1. K-Means
# In[ ]:
# Features characterizing the patient demographic and medical condition prior to his hospialization
patient_features = [u'race', u'gender', u'age',u'admission_type_id',u'admission_source_id', u'payer_code',u'number_outpatient', u'number_emergency',u'number_inpatient']
patient_df = diabetic_data_first_encounter[patient_features]
# Change age into numerical feature by taking each group average.
def get_age(age_group):
return np.mean([int(x) for x in age_group.replace('[','').replace(')','').split('-')])
patient_df['age'] = patient_df['age'].apply(get_age)
# Normalize numerical colum , in order to keep then pretty much on the same scale as the one hot features.
cols_to_normalize = ['age','number_outpatient','number_emergency','number_inpatient']
for col in cols_to_normalize:
patient_df[col] = (patient_df[col] - patient_df[col].min()) / (patient_df[col].max() - patient_df[col].min())
cols_to_one_hot = [u'race', u'gender',u'admission_type_id',u'admission_source_id',u'payer_code']
dummy_df = pd.get_dummies(patient_df[cols_to_one_hot])
patient_df = pd.concat([patient_df,dummy_df],axis=1)
for col in cols_to_one_hot:
del patient_df[col]
gc.collect()
# In[ ]:
#Find best k
res = []
for k in range(2,10):
kmeans = KMeans(n_clusters=k,random_state=0,n_jobs=3).fit(patient_df)
patient_df['cluster'] = kmeans.labels_
del kmeans
sample = patient_df.sample(10000)
score = metrics.silhouette_score(sample.iloc[:,:-1],sample.iloc[:,-1])
print k,score
res.append(score)
gc.collect()
for k in range(10,200,10):
kmeans = KMeans(n_clusters=k,random_state=0,n_jobs=3).fit(patient_df)
patient_df['cluster'] = kmeans.labels_
del kmeans
sample = patient_df.sample(10000)
score = metrics.silhouette_score(sample.iloc[:,:-1],sample.iloc[:,-1])
print k,score
res.append(score)
gc.collect()
# In[28]:
l = []
for k in range(2,10):
l.append(k)
for k in range(10,200,10):
l.append(k)
k_score = pd.DataFrame(res,index=l)
plt.plot(k_score)
# In[29]:
plt.plot(k_score)
# In[32]:
gc.collect()
# In[ ]:
patient_df_2 = diabetic_data_first_encounter[patient_features]
def get_age(age_group):
return np.mean([int(x) for x in age_group.replace('[','').replace(')','').split('-')])
patient_df_2['age'] = patient_df_2['age'].apply(get_age)
# In[ ]:
# Try using PCA for dimension reduction for the numerical columns.
cols_to_pca = ['number_outpatient','number_emergency','number_inpatient']
reduced_data = PCA(n_components=1).fit_transform(patient_df_2[cols_to_pca])
patient_df_2['hospitalizations'] = reduced_data
for col in cols_to_pca:
del patient_df_2[col]
cols_to_normalize = ['age','hospitalizations']
for col in cols_to_normalize:
patient_df_2[col] = (patient_df_2[col] - patient_df_2[col].min()) / (patient_df_2[col].max() - patient_df_2[col].min())
cols_to_one_hot = [u'race', u'gender',u'admission_type_id',u'admission_source_id',u'payer_code']
dummy_df = pd.get_dummies(patient_df_2[cols_to_one_hot])
patient_df_2 = pd.concat([patient_df_2,dummy_df],axis=1)
for col in cols_to_one_hot:
del patient_df_2[col]
gc.collect()
# In[ ]:
#Find best k
res = []
for k in range(2,10):
kmeans = KMeans(n_clusters=k,random_state=0,n_jobs=3).fit(patient_df_2)
patient_df['cluster'] = kmeans.labels_
del kmeans
sample = patient_df.sample(10000)
score = metrics.silhouette_score(sample.iloc[:,:-1],sample.iloc[:,-1])
print k,score
res.append(score)
gc.collect()
for k in range(10,200,10):
kmeans = KMeans(n_clusters=k,random_state=0,n_jobs=3).fit(patient_df_2)
patient_df['cluster'] = kmeans.labels_
del kmeans
sample = patient_df.sample(10000)
score = metrics.silhouette_score(sample.iloc[:,:-1],sample.iloc[:,-1])
print k,score
res.append(score)
gc.collect()
# In[41]:
l = []
for k in range(2,10):
l.append(k)
for k in range(10,200,10):
l.append(k)
k_score = pd.DataFrame(res,index=l)
# In[42]:
plt.plot(k_score)
# In[53]:
best_k = 30
kmeans = KMeans(n_clusters=best_k,random_state=0,n_jobs=3).fit(patient_df)
diabetic_data_first_encounter['cluster'] = kmeans.labels_
# ### 2.Similarity / Distance matrix
# Attenpt to cluster data using similarity between samples.
# In[68]:
patient_df = diabetic_data_first_encounter[patient_features]
# In[59]:
race_dict = dict(zip(patient_df['race'].unique(),range(patient_df['race'].nunique())))
gender_dict = dict(zip(patient_df['gender'].unique(),range(patient_df['gender'].nunique())))
payer_code_dict = dict(zip(patient_df['payer_code'].unique(),range(patient_df['payer_code'].nunique())))
# In[ ]:
patient_df['race'] = patient_df['race'].apply(lambda x :race_dict[x])
patient_df['gender'] = patient_df['gender'].apply(lambda x :gender_dict[x])
patient_df['payer_code'] = patient_df['payer_code'].apply(lambda x :payer_code_dict[x])
# In[36]:
# normalize numeric columns , so the distance will be on the same scale.
cols_to_normalize = ['age','number_outpatient','number_emergency','number_inpatient']
for col in cols_to_normalize:
patient_df[col] = (patient_df[col] - patient_df[col].min()) / (patient_df[col].max() - patient_df[col].min())
# In[46]:
nominal_indices = [0,1,3,4,5]
numeric_indices = [2,6,7,8]
# In[173]:
# For nominal features , the similarit will be in an inverse ratio to the general probability
nominal_coefficient = []
for i in range(6):
nominal_coefficient.append({})
for idx in nominal_indices:
nominal_coefficient[idx] = dict(1 / patient_df.iloc[:,idx].value_counts(normalize=True))
# In[195]:
# Function that returns similarity between 2 samples.
def get_similarity(x_1,x_2):
sim = np.zeros(9)
sim[nominal_indices] = [int(x) for x in x_1[nominal_indices] == x_2[nominal_indices]]
for idx in nominal_indices:
sim[idx] = sim[idx] * nominal_coefficient[idx][int(x_1[idx])]
sim[numeric_indices] = 1 - np.abs(x_1[numeric_indices] - x_2[numeric_indices])
return np.mean(sim)
# In[202]:
# Due to resources limitation only cluster 10,000 samples.
sample_df = patient_df.sample(10000)
vals = sample_df.values
sim = distance.cdist(vals,vals,get_similarity)
# In[ ]:
adj_mat = np.array(sim)
sc = SpectralClustering(30, affinity='precomputed', n_init=100)
sc.fit(adj_mat)
# In[111]:
sample_df['cluster'] = sc.labels_
sample_df['Unnamed: 0'] = sample_df.index
spectral_clutering = diabetic_data_first_encounter.merge(sample_df[['Unnamed: 0','cluster']],on='Unnamed: 0')
# In[ ]: