-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPropensity_score_matching.py
248 lines (192 loc) · 8.28 KB
/
Propensity_score_matching.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
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.preprocessing import MinMaxScaler
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.metrics.pairwise import euclidean_distances
from sklearn.ensemble import RandomForestClassifier
from lightgbm import LGBMClassifier
from sklearn import svm
from sklearn.neural_network import MLPClassifier
from sklearn.ensemble import VotingClassifier
from sklearn.metrics import confusion_matrix
import itertools
from sklearn.manifold import TSNE
import seaborn as sns
# Loading dataset
df = pd.read_excel('./Dataset.xlsx', sheet_name='Sheet1', engine='openpyxl')
df_AM = df[df['Center'] == 7]
df_ex = df[df['Center'] != 7]
##### 1. Propensity score matching #####
# df_op = Resection group in external dataset, df_TACE = TACE group in external dataset
df_op = df_ex[df_ex['Tx'] == 1].copy()
df_TACE = df_ex[df_ex['Tx'] == 2].copy()
X_treated = df_TACE.loc[:, 'age':'Meta'].values
X_control = df_op.loc[:, 'age':'Meta'].values
# Standard normalization
scaler = StandardScaler()
X_treated_scaled = scaler.fit_transform(X_treated)
X_control_scaled = scaler.transform(X_control)
# Propensity score model fitting
model = LogisticRegression(solver='lbfgs')
model.fit(np.vstack((X_treated_scaled, X_control_scaled)), np.hstack((np.ones(len(X_treated_scaled)), np.zeros(len(X_control_scaled)))))
# Calculating propensity score
propensity_scores_treated = model.predict_proba(X_treated_scaled)[:, 1]
propensity_scores_control = model.predict_proba(X_control_scaled)[:, 1]
control_idx = []
treated_idx = []
# Select the most similar control group data for each data point in the treatment group
for i, t in enumerate(propensity_scores_treated):
distances = euclidean_distances(t.reshape(-1, 1), propensity_scores_control.reshape(-1, 1))
indices = np.argsort(distances)
# Check if the index has already been selected and, if not, add it to the matched pair
for j in indices[0]:
if j not in control_idx:
if distances[0][j] < 0.1:
control_idx.append(j)
treated_idx.append(i)
break
_df_op = df_op.reset_index().copy()
_df_TACE = df_TACE.reset_index().copy()
df_TACE_sorted = _df_TACE.loc[treated_idx].copy()
df_op_sorted = _df_op.loc[control_idx].copy()
# Model training for treatment recommendation with whole internal dataset
X = np.array(df_AM.loc[:, 'age':'Meta'])
y = np.array(df_AM.loc[:, 'Tx'])
scaler = MinMaxScaler().fit(X)
X = scaler.transform(X)
lr = LogisticRegression(random_state=999, multi_class='multinomial')
rf = RandomForestClassifier(random_state = 999)
lgbm = LGBMClassifier(random_state=999)
sv = svm.SVC(random_state=999, probability=True)
mlp = MLPClassifier(random_state=999, max_iter=300)
eclf = VotingClassifier(estimators=[('lr', lr), ('rf', rf), ('lgbm', lgbm), ('svm', sv), ('mlp', mlp)], voting='soft')
eclf.fit(X, y)
# Model testing
for i in df_ex.index:
test_data = np.array(df_ex.loc[i, 'age':'Meta'])
test_data = test_data.reshape(1, -1)
test_data = scaler.transform(test_data)
results = eclf.predict_proba(test_data).flatten()
final = np.argsort(results)
df_ex.loc[i, 'pred_1'] = final[-1]
##### 2. Confusion matrix before and after PSM #####
df_op = df_op_sorted.copy() # matched resection group
df_TACE = df_TACE_sorted.copy() # matched TACE group
_df_op = df_ex[df_ex['Tx'] == 1].copy() # original resection group
_df_TACE = df_ex[df_ex['Tx'] == 2].copy() # original TACE group
# Retrieve the prediction values
for i in df_ex.index:
for j in df_op.index:
if df_op.loc[j, 'index'] == i:
df_op.loc[j, 'pred_1'] = df_ex.loc[i, 'pred_1']
for i in df_ex.index:
for j in df_TACE.index:
if df_TACE.loc[j, 'index'] == i:
df_TACE.loc[j, 'pred_1'] = df_ex.loc[i, 'pred_1']
def plot_confusion_matrix(cm, classes,
normalize=False,
reverse=False,
title='Confusion matrix',
cmap=plt.cm.Blues):
if reverse == True:
cm = cm[::-1,::-1]
cm_origin = cm.copy()
if normalize:
cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
print("Normalized confusion matrix")
else:
print('Confusion matrix, without normalization')
fig, ax = plt.subplots()
plt.imshow(cm, cmap=cmap)
plt.title(title)
plt.colorbar()
tick_marks = np.arange(len(classes))
plt.xticks(tick_marks, classes, rotation=45)
plt.yticks(tick_marks, classes)
fmt = '.2f' if normalize else 'd'
thresh = 0.5
for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
plt.text(j, i, format(cm[i, j], fmt)+'\n({})'.format(cm_origin[i,j]),
horizontalalignment="center",
verticalalignment="center",
color="white" if cm[i, j] > thresh else "black", fontsize=15)
plt.ylabel('True label')
plt.xlabel('Predicted label')
plt.tight_layout()
plt.show()
return()
classe_name = ['RFA', 'Resection', 'TACE', 'TACE+RT', 'Sorafenib', 'None']
# Confusion matrix for Matched resection group
y_real = np.array(df_op.loc[:, 'Tx'])
y_pred = np.array(df_op.loc[:, 'pred_1'])
cm = confusion_matrix(y_real, y_pred)
plt.figure(figsize=(8,8))
classes = [classe_name[int(i)] for i in sorted(df_op.loc[:, 'pred_1'].unique())]
plot_confusion_matrix(cm, classes=classes, normalize=True, reverse=False)
# Confusion matrix for original resection group
y_real = np.array(_df_op.loc[:, 'Tx'])
y_pred = np.array(_df_op.loc[:, 'pred_1'])
cm = confusion_matrix(y_real, y_pred)
plt.figure(figsize=(8,8))
classes = [classe_name[int(i)] for i in sorted(_df_op.loc[:, 'pred_1'].unique())]
plot_confusion_matrix(cm, classes=classes, normalize=True, reverse=False)
# Confusion matrix for Matched TACE group
y_real = np.array(df_TACE.loc[:, 'Tx'])
y_pred = np.array(df_TACE.loc[:, 'pred_1'])
cm = confusion_matrix(y_real, y_pred)
plt.figure(figsize=(8,8))
classes = [classe_name[int(i)] for i in sorted(df_TACE.loc[:, 'pred_1'].unique())]
plot_confusion_matrix(cm, classes=classes, normalize=True, reverse=False)
# Confusion matrix for original TACE group
y_real = np.array(_df_TACE.loc[:, 'Tx'])
y_pred = np.array(_df_TACE.loc[:, 'pred_1'])
cm = confusion_matrix(y_real, y_pred)
plt.figure(figsize=(8,8))
classes = [classe_name[int(i)] for i in sorted(_df_TACE.loc[:, 'pred_1'].unique())]
plot_confusion_matrix(cm, classes=classes, normalize=True, reverse=False)
##### 3. Feature analysis using t-SNE #####
# 20 features before feature reduction
AMC_TACE = np.array(df_AM[df_AM['Tx'] == 2].loc[:, 'age':'Meta']) # TACE group in training dataset
AMC_op = np.array(df_AM[df_AM['Tx'] == 1].loc[:, 'age':'Meta']) # Resection group in training dataset
correct = np.array(df_TACE[df_TACE['pred_1'] == 2].loc[:, 'age':'Meta']) # Pred = TACE in matched TACE group
incorrect = np.array(df_TACE[df_TACE['pred_1'] == 1].loc[:, 'age':'Meta']) # Pred = Resection in matched TACE group
_AMC_TACE = scaler.transform(AMC_TACE)
_AMC_op = scaler.transform(AMC_op)
_correct = scaler.transform(correct)
_incorrect = scaler.transform(incorrect)
# Feature reduction using t-SNE
_X = []
_y = []
for n in _AMC_TACE:
_X.append(n)
_y.append(0)
for n in _AMC_op:
_X.append(n)
_y.append(1)
for n in _correct:
_X.append(n)
_y.append(2)
for n in _incorrect:
_X.append(n)
_y.append(3)
T = TSNE(n_components = 2, perplexity=150).fit_transform(_X)
df = pd.DataFrame(T, columns=['1st dimension', '2nd dimension'])
for i, y in enumerate(_y):
if y == 0:
df.loc[i, 'Label'] = 'TACE group in the training dataset'
elif y == 1:
df.loc[i, 'Label'] = 'Resection group in the training dataset'
elif y == 2:
df.loc[i, 'Label'] = 'Correct prediction (=TACE) in the TACE group'
elif y == 3:
df.loc[i, 'Label'] = 'Incorrect prediction (=Resection) in the TACE group'
fig = plt.figure(figsize=(8,8))
fig.set_facecolor('white')
sns.set_palette("bright")
sns.scatterplot(data=df, x='1st dimension', y='2nd dimension', hue='Label',
alpha=0.7, s=70, style='Label')
plt.title('2-Dimensional t-SNE', fontsize=18)
plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0., fontsize=14)
plt.show()