-
Notifications
You must be signed in to change notification settings - Fork 0
/
knee_replacement_T-SNE.py
142 lines (116 loc) · 4.25 KB
/
knee_replacement_T-SNE.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
import numpy as np
import pandas as pd
from sklearn.preprocessing import scale
from scipy._lib.six import xrange
import numpy as np
from sklearn.decomposition import PCA
from sklearn.manifold import TSNE
import matplotlib.pyplot as plt
%matplotlib inline
## Input file
X = pd.read_csv('Aqua_Knee.csv')
#Random split
msk = np.random.rand(len(X)) < 0.2
train = X[msk]
Hold_out = X[~msk] ### Will use later
## Convert Bool to 0/1
X['is_aqua'] *= 1
X['trans'] *= 1
X['infection_knee'] *=1
## Dummy code out variables
dummies = pd.get_dummies(X['Patient_Race_Code']).rename(columns=lambda x: 'Race_' + str(x))
X = pd.concat([X, dummies], axis=1)
X = X.drop(['Patient_Race_Code'], axis=1)
dummies = pd.get_dummies(X['Patient_Sex']).rename(columns=lambda x: 'Sex_' + str(x))
X = pd.concat([X, dummies], axis=1)
X = X.drop(['Patient_Sex'], axis=1)
dummies = pd.get_dummies(X['weekdays']).rename(columns=lambda x: 'W_' + str(x))
X = pd.concat([X, dummies], axis=1)
X = X.drop(['weekdays'], axis=1)
dummies = pd.get_dummies(X['ASA_MAPPED_SCOR_CD']).rename(columns=lambda x: 'ASA_' + str(x))
X = pd.concat([X, dummies], axis=1)
## Main Functions
import pandas as pd
import numpy as np
from sklearn.cross_validation import train_test_split
from sklearn import manifold
from sklearn.cross_validation import StratifiedShuffleSplit
from sklearn.preprocessing import normalize
import matplotlib.pyplot as plt
import matplotlib.cm as cm
def run_tsne(train, target):
sss = StratifiedShuffleSplit(target, test_size=0.2)
for train_index, test_index in sss:
break
X_train, X_valid = train[train_index], train[test_index]
Y_train, Y_valid = target[train_index], target[test_index]
train_norm = normalize(X_valid, axis=0)
tsne = manifold.TSNE(n_components=3,
init='pca',
perplexity=15,
random_state=101,
method='barnes_hut',
n_iter=2000,
verbose=2)
train_tsne = tsne.fit_transform(train_norm)
return (train_tsne, Y_valid)
def tsne_vis(tsne_data, tsne_groups):
colors = cm.rainbow(np.linspace(0, 1, 5))
labels = ['ASA_1', 'ASA_2' ,'ASA_3', 'ASA_4']
plt.figure(figsize=(10, 10))
for l, c, co, in zip(labels, colors, range(4)):
plt.scatter(tsne_data[np.where(tsne_groups == co), 0],
tsne_data[np.where(tsne_groups == co), 1],
marker='o',
color=c,
linewidth='1',
alpha=0.8,
label=l)
plt.xlabel('Dimension 1')
plt.ylabel('Dimension 2')
plt.title('t-SNE on 10% of train samples')
plt.legend(loc='best')
plt.savefig('rainbow-01.png')
plt.show(block=False)
plt.figure(figsize=(10, 10))
for l, c, co, in zip(labels, colors, range(5)):
plt.scatter(tsne_data[np.where(tsne_groups == co), 0],
tsne_data[np.where(tsne_groups == co), 2],
marker='o',
color=c,
linewidth='1',
alpha=0.8,
label=l)
plt.xlabel('Dimension 1')
plt.ylabel('Dimension 3')
plt.title('t-SNE on 10% of train samples')
plt.legend(loc='best')
plt.savefig('rainbow-02.png')
plt.show(block=False)
plt.figure(figsize=(10, 10))
for l, c, co, in zip(labels, colors, range(5)):
plt.scatter(tsne_data[np.where(tsne_groups == co), 1],
tsne_data[np.where(tsne_groups == co), 2],
marker='o',
color=c,
linewidth='1',
alpha=0.8,
label=l)
plt.xlabel('Dimension 2')
plt.ylabel('Dimension 3')
plt.title('t-SNE on 10% of train samples')
plt.legend(loc='best')
plt.savefig('rainbow-03.png')
plt.show(block=False)
def map_column(table, f):
labels = sorted(table[f].unique())
mappings = dict()
for i in range(len(labels)):
mappings[labels[i]] = i
table = table.replace({f: mappings})
return table
## Plug in what you need here ##
X = train_df.drop(['Sex_F'], axis=1).values
Y = train_df[['ASA_MAPPED_SCOR_CD']].values
tsne_data, tsne_groups = run_tsne(X, Y)
tsne_vis(tsne_data, tsne_groups)