-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathshapp_LLMmed.py
executable file
·234 lines (179 loc) · 9.78 KB
/
shapp_LLMmed.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
"""
shap_LLMmed.py
@author: Soroosh Tayebi Arasteh <[email protected]>
https://github.com/tayebiarasteh/
"""
import pandas as pd
import shap
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
import os
from sklearn import metrics
from sklearn.preprocessing import LabelEncoder
from sklearn.ensemble import GradientBoostingClassifier, RandomForestClassifier
from sklearn.preprocessing import MinMaxScaler, StandardScaler
import warnings
warnings.filterwarnings('ignore')
class cohort1():
def __int__(self):
pass
def shap_plot(self):
shap_values_df = pd.read_csv('final_shap_1.csv')
avg_abs_shap_values = shap_values_df.abs().mean().sort_values(ascending=False)
top_features = avg_abs_shap_values.index.tolist()
min_avg_shap = avg_abs_shap_values.min()
max_avg_shap = avg_abs_shap_values.max()
norm_dataset = plt.Normalize(vmin=min_avg_shap, vmax=max_avg_shap)
sm_dataset = plt.cm.ScalarMappable(cmap="YlOrRd", norm=norm_dataset)
palette_dict_custom = {feature: sm_dataset.to_rgba(value) for feature, value in avg_abs_shap_values.items()}
means_dataset = shap_values_df[top_features].mean()
fig, ax = plt.subplots(figsize=(18, 18))
sns.set_style("white")
sns.boxplot(data=shap_values_df[top_features], orient='h', palette=palette_dict_custom,
fliersize=5, flierprops={"marker": "o", "markerfacecolor": "black", "markeredgecolor": "black"},
ax=ax)
ax.scatter(means_dataset, range(len(top_features)), marker='x', color='black', s=500, zorder=5,
label='Mean')
ax.axvline(x=0, color='black', linestyle='-', linewidth=3)
x_min_specified = -0.08
x_max_specified = 0.20
specified_xticks = [-3, -2, -1, 0, 1, 2, 3, 4]
ax.set_xlim(x_min_specified, x_max_specified)
ax.set_xticks(specified_xticks)
ax.set_xticklabels([f"{int(value)}" for value in specified_xticks],
fontsize=30)
ax.set_yticklabels(top_features, fontsize=30)
ax.set_xlabel("SHAP value (impact on model output)", fontsize=35, labelpad=20)
ax.set_ylabel("")
ax.set_title("Metastatic Disease [Endocrinologic Oncology]", fontsize=40, ha='left', x=0, y=1.02)
cax_new = fig.add_axes([0.93, 0.125, 0.02, 0.75])
cbar = plt.colorbar(sm_dataset, cax=cax_new, orientation='vertical')
cbar.set_ticks(np.linspace(avg_abs_shap_values.min(), avg_abs_shap_values.max(), 5))
cbar.set_ticklabels(
[f"{value:.2f}" for value in np.linspace(avg_abs_shap_values.min(), avg_abs_shap_values.max(), 5)])
cbar.ax.tick_params(labelsize=30)
cbar.set_label('Mean absolute SHAP value', rotation=270, fontsize=35, labelpad=40)
plt.show()
class cohort2():
def __int__(self):
pass
def shap_plot(self):
shap_values_df = pd.read_csv('final_shap_2.csv')
avg_abs_shap_values = shap_values_df.abs().mean().sort_values(ascending=False)
top_features = avg_abs_shap_values.index.tolist()
min_avg_shap = avg_abs_shap_values.min()
max_avg_shap = avg_abs_shap_values.max()
norm_dataset = plt.Normalize(vmin=min_avg_shap, vmax=max_avg_shap)
sm_dataset = plt.cm.ScalarMappable(cmap="YlOrRd", norm=norm_dataset)
palette_dict_custom = {feature: sm_dataset.to_rgba(value) for feature, value in avg_abs_shap_values.items()}
means_dataset = shap_values_df[top_features].mean()
fig, ax = plt.subplots(figsize=(18, 18))
sns.set_style("white")
sns.boxplot(data=shap_values_df[top_features], orient='h', palette=palette_dict_custom,
fliersize=5, flierprops={"marker": "o", "markerfacecolor": "black", "markeredgecolor": "black"},
ax=ax)
ax.scatter(means_dataset, range(len(top_features)), marker='x', color='black', s=500, zorder=5,
label='Mean')
ax.axvline(x=0, color='black', linestyle='-', linewidth=3)
x_min_specified = -0.08
x_max_specified = 0.20
specified_xticks = [-2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
ax.set_xlim(x_min_specified, x_max_specified)
ax.set_xticks(specified_xticks)
ax.set_xticklabels([f"{int(value)}" for value in specified_xticks],
fontsize=30)
ax.set_yticklabels(top_features, fontsize=30)
ax.set_xlabel("SHAP value (impact on model output)", fontsize=35, labelpad=20)
ax.set_ylabel("")
ax.set_title("Oesophageal Cancer [Gastrointestinal Oncology]", fontsize=40, ha='left', x=0, y=1.02)
cax_new = fig.add_axes([0.93, 0.125, 0.02, 0.75])
cbar = plt.colorbar(sm_dataset, cax=cax_new, orientation='vertical')
cbar.set_ticks(np.linspace(avg_abs_shap_values.min(), avg_abs_shap_values.max(), 5))
cbar.set_ticklabels(
[f"{value:.2f}" for value in np.linspace(avg_abs_shap_values.min(), avg_abs_shap_values.max(), 5)])
cbar.ax.tick_params(labelsize=30)
cbar.set_label('Mean absolute SHAP value', rotation=270, fontsize=35, labelpad=40)
plt.show()
class cohort3():
def __int__(self):
pass
def shap_plot(self):
shap_values_df = pd.read_csv('final_shap_3.csv')
avg_abs_shap_values = shap_values_df.abs().mean().sort_values(ascending=False)
top_features = avg_abs_shap_values.index.tolist()
min_avg_shap = avg_abs_shap_values.min()
max_avg_shap = avg_abs_shap_values.max()
norm_dataset = plt.Normalize(vmin=min_avg_shap, vmax=max_avg_shap)
sm_dataset = plt.cm.ScalarMappable(cmap="YlOrRd", norm=norm_dataset)
palette_dict_custom = {feature: sm_dataset.to_rgba(value) for feature, value in avg_abs_shap_values.items()}
means_dataset = shap_values_df[top_features].mean()
fig, ax = plt.subplots(figsize=(18, 18))
sns.set_style("white")
sns.boxplot(data=shap_values_df[top_features], orient='h', palette=palette_dict_custom,
fliersize=5, flierprops={"marker": "o", "markerfacecolor": "black", "markeredgecolor": "black"},
ax=ax)
ax.scatter(means_dataset, range(len(top_features)), marker='x', color='black', s=500, zorder=5,
label='Mean')
ax.axvline(x=0, color='black', linestyle='-', linewidth=3)
x_min = -0.5
x_max = 0.4
specified_xticks = np.linspace(x_min, x_max, num=10)
ax.set_xlim(x_min, x_max)
ax.set_xticks(specified_xticks)
ax.set_xticklabels([f"{value:.1f}" for value in specified_xticks], fontsize=30)
ax.set_yticklabels(top_features, fontsize=30)
ax.set_xlabel("SHAP value (impact on model output)", fontsize=35, labelpad=20)
ax.set_ylabel("")
ax.set_title("Hereditary Hearing Loss [Otolaryngology]", fontsize=40, ha='left', x=0, y=1.02)
cax = fig.add_axes([0.93, 0.125, 0.02, 0.75])
cbar = plt.colorbar(sm_dataset, cax=cax, orientation='vertical')
cbar.set_ticks(
np.linspace(avg_abs_shap_values.min(), avg_abs_shap_values.max(), 5))
cbar.set_ticklabels([f"{value:.2f}" for value in
np.linspace(avg_abs_shap_values.min(),
avg_abs_shap_values.max(), 5)])
cbar.ax.tick_params(labelsize=30)
cbar.set_label('Mean absolute SHAP value', rotation=270, fontsize=35, labelpad=40)
plt.show()
class cohort4():
def __int__(self):
pass
def shap_plot(self):
shap_values_df = pd.read_csv('final_shap_4.csv')
avg_abs_shap_values = shap_values_df.abs().mean().sort_values(ascending=False)
top_features = avg_abs_shap_values.index.tolist()
min_avg_shap = avg_abs_shap_values.min()
max_avg_shap = avg_abs_shap_values.max()
norm_dataset = plt.Normalize(vmin=min_avg_shap, vmax=max_avg_shap)
sm_dataset = plt.cm.ScalarMappable(cmap="YlOrRd", norm=norm_dataset)
palette_dict_custom = {feature: sm_dataset.to_rgba(value) for feature, value in avg_abs_shap_values.items()}
means_dataset = shap_values_df[top_features].mean()
fig, ax = plt.subplots(figsize=(18, 18))
sns.set_style("white")
sns.boxplot(data=shap_values_df[top_features], orient='h', palette=palette_dict_custom,
fliersize=5, flierprops={"marker": "o", "markerfacecolor": "black", "markeredgecolor": "black"},
ax=ax)
ax.scatter(means_dataset, range(len(top_features)), marker='x', color='black', s=500, zorder=5,
label='Mean')
ax.axvline(x=0, color='black', linestyle='-', linewidth=3)
x_min = -0.1
x_max = 0.2
specified_xticks = [-0.1, 0, 0.1, 0.2]
ax.set_xlim(x_min, x_max)
ax.set_xticks(specified_xticks)
ax.set_xticklabels([f"{value:.1f}" for value in specified_xticks], fontsize=30)
ax.set_yticklabels(top_features, fontsize=30)
ax.set_xlabel("SHAP value (impact on model output)", fontsize=35, labelpad=20)
ax.set_ylabel("")
ax.set_title("Cardiac Amyloidosis [Cardiology]", fontsize=40, ha='left', x=0, y=1.02)
cax = fig.add_axes([0.93, 0.125, 0.02, 0.75])
cbar = plt.colorbar(sm_dataset, cax=cax, orientation='vertical')
cbar.set_ticks(
np.linspace(avg_abs_shap_values.min(), avg_abs_shap_values.max(), 5))
cbar.set_ticklabels([f"{value:.2f}" for value in
np.linspace(avg_abs_shap_values.min(),
avg_abs_shap_values.max(), 5)])
cbar.ax.tick_params(labelsize=30)
cbar.set_label('Mean absolute SHAP value', rotation=270, fontsize=35, labelpad=40)
plt.show()