|
a |
|
b/.ipynb_checkpoints/Null-classifier-checkpoint.ipynb |
|
|
1 |
{ |
|
|
2 |
"cells": [ |
|
|
3 |
{ |
|
|
4 |
"cell_type": "markdown", |
|
|
5 |
"metadata": {}, |
|
|
6 |
"source": [ |
|
|
7 |
"<h1 align=\"center\">Machine learning-based prediction of early recurrence in glioblastoma patients: a glance towards precision medicine <br><br>[Null-classifier]</h1>" |
|
|
8 |
] |
|
|
9 |
}, |
|
|
10 |
{ |
|
|
11 |
"cell_type": "markdown", |
|
|
12 |
"metadata": {}, |
|
|
13 |
"source": [ |
|
|
14 |
"<h2>[1] Library</h2>" |
|
|
15 |
] |
|
|
16 |
}, |
|
|
17 |
{ |
|
|
18 |
"cell_type": "code", |
|
|
19 |
"execution_count": 1, |
|
|
20 |
"metadata": { |
|
|
21 |
"collapsed": true |
|
|
22 |
}, |
|
|
23 |
"outputs": [ |
|
|
24 |
{ |
|
|
25 |
"name": "stderr", |
|
|
26 |
"output_type": "stream", |
|
|
27 |
"text": [ |
|
|
28 |
"/Users/valerio_mc/opt/anaconda3/lib/python3.7/site-packages/sklearn/utils/deprecation.py:144: FutureWarning: The sklearn.neighbors.base module is deprecated in version 0.22 and will be removed in version 0.24. The corresponding classes / functions should instead be imported from sklearn.neighbors. Anything that cannot be imported from sklearn.neighbors is now part of the private API.\n", |
|
|
29 |
" warnings.warn(message, FutureWarning)\n", |
|
|
30 |
"/Users/valerio_mc/opt/anaconda3/lib/python3.7/site-packages/sklearn/utils/deprecation.py:144: FutureWarning: The sklearn.ensemble.bagging module is deprecated in version 0.22 and will be removed in version 0.24. The corresponding classes / functions should instead be imported from sklearn.ensemble. Anything that cannot be imported from sklearn.ensemble is now part of the private API.\n", |
|
|
31 |
" warnings.warn(message, FutureWarning)\n", |
|
|
32 |
"/Users/valerio_mc/opt/anaconda3/lib/python3.7/site-packages/sklearn/utils/deprecation.py:144: FutureWarning: The sklearn.ensemble.base module is deprecated in version 0.22 and will be removed in version 0.24. The corresponding classes / functions should instead be imported from sklearn.ensemble. Anything that cannot be imported from sklearn.ensemble is now part of the private API.\n", |
|
|
33 |
" warnings.warn(message, FutureWarning)\n", |
|
|
34 |
"/Users/valerio_mc/opt/anaconda3/lib/python3.7/site-packages/sklearn/utils/deprecation.py:144: FutureWarning: The sklearn.ensemble.forest module is deprecated in version 0.22 and will be removed in version 0.24. The corresponding classes / functions should instead be imported from sklearn.ensemble. Anything that cannot be imported from sklearn.ensemble is now part of the private API.\n", |
|
|
35 |
" warnings.warn(message, FutureWarning)\n", |
|
|
36 |
"/Users/valerio_mc/opt/anaconda3/lib/python3.7/site-packages/sklearn/utils/deprecation.py:144: FutureWarning: The sklearn.utils.testing module is deprecated in version 0.22 and will be removed in version 0.24. The corresponding classes / functions should instead be imported from sklearn.utils. Anything that cannot be imported from sklearn.utils is now part of the private API.\n", |
|
|
37 |
" warnings.warn(message, FutureWarning)\n", |
|
|
38 |
"/Users/valerio_mc/opt/anaconda3/lib/python3.7/site-packages/sklearn/utils/deprecation.py:144: FutureWarning: The sklearn.metrics.classification module is deprecated in version 0.22 and will be removed in version 0.24. The corresponding classes / functions should instead be imported from sklearn.metrics. Anything that cannot be imported from sklearn.metrics is now part of the private API.\n", |
|
|
39 |
" warnings.warn(message, FutureWarning)\n" |
|
|
40 |
] |
|
|
41 |
} |
|
|
42 |
], |
|
|
43 |
"source": [ |
|
|
44 |
"# OS library\n", |
|
|
45 |
"import os\n", |
|
|
46 |
"import sys\n", |
|
|
47 |
"import argparse\n", |
|
|
48 |
"import itertools\n", |
|
|
49 |
"import random\n", |
|
|
50 |
"\n", |
|
|
51 |
"# Analysis\n", |
|
|
52 |
"import numpy as np\n", |
|
|
53 |
"import pandas as pd\n", |
|
|
54 |
"import seaborn as sns\n", |
|
|
55 |
"import matplotlib.pyplot as plt\n", |
|
|
56 |
"\n", |
|
|
57 |
"# Sklearn\n", |
|
|
58 |
"from boruta import BorutaPy\n", |
|
|
59 |
"from sklearn.preprocessing import LabelEncoder\n", |
|
|
60 |
"from sklearn.model_selection import train_test_split\n", |
|
|
61 |
"from sklearn.ensemble import RandomForestClassifier\n", |
|
|
62 |
"from sklearn.metrics import confusion_matrix, f1_score, recall_score, classification_report, accuracy_score, auc, roc_curve\n", |
|
|
63 |
"from sklearn.model_selection import RandomizedSearchCV\n", |
|
|
64 |
"from sklearn.dummy import DummyClassifier\n", |
|
|
65 |
"\n", |
|
|
66 |
"import scikitplot as skplt\n", |
|
|
67 |
"from imblearn.over_sampling import RandomOverSampler, SMOTENC, SMOTE" |
|
|
68 |
] |
|
|
69 |
}, |
|
|
70 |
{ |
|
|
71 |
"cell_type": "markdown", |
|
|
72 |
"metadata": {}, |
|
|
73 |
"source": [ |
|
|
74 |
"<h2>[2] Exploratory data analysis and Data Preprocessing</h2>" |
|
|
75 |
] |
|
|
76 |
}, |
|
|
77 |
{ |
|
|
78 |
"cell_type": "markdown", |
|
|
79 |
"metadata": {}, |
|
|
80 |
"source": [ |
|
|
81 |
"<h4>[-] Load the database</h4>" |
|
|
82 |
] |
|
|
83 |
}, |
|
|
84 |
{ |
|
|
85 |
"cell_type": "code", |
|
|
86 |
"execution_count": null, |
|
|
87 |
"metadata": {}, |
|
|
88 |
"outputs": [], |
|
|
89 |
"source": [ |
|
|
90 |
"file = os.path.join(sys.path[0], \"db.xlsx\")\n", |
|
|
91 |
"db = pd.read_excel(file)\n", |
|
|
92 |
"\n", |
|
|
93 |
"print(\"N° of patients: {}\".format(len(db)))\n", |
|
|
94 |
"print(\"N° of columns: {}\".format(db.shape[1]))\n", |
|
|
95 |
"db.head()" |
|
|
96 |
] |
|
|
97 |
}, |
|
|
98 |
{ |
|
|
99 |
"cell_type": "markdown", |
|
|
100 |
"metadata": {}, |
|
|
101 |
"source": [ |
|
|
102 |
"<h4>[-] Drop unwanted columns + create <i>'results'</i> column</h4>" |
|
|
103 |
] |
|
|
104 |
}, |
|
|
105 |
{ |
|
|
106 |
"cell_type": "code", |
|
|
107 |
"execution_count": null, |
|
|
108 |
"metadata": {}, |
|
|
109 |
"outputs": [], |
|
|
110 |
"source": [ |
|
|
111 |
"df = db.drop(['Name_Surname','...'], axis = 'columns')\n", |
|
|
112 |
"\n", |
|
|
113 |
"print(\"Effective features to consider: {} \".format(len(df.columns)-1))\n", |
|
|
114 |
"print(\"Creating 'result' column...\")\n", |
|
|
115 |
"\n", |
|
|
116 |
"# 0 = No relapse\n", |
|
|
117 |
"df.loc[df['PFS'] > 6, 'result'] = 0\n", |
|
|
118 |
"\n", |
|
|
119 |
"# 1 = Early relapse (within 6 months)\n", |
|
|
120 |
"df.loc[df['PFS'] <= 6, 'result'] = 1\n", |
|
|
121 |
"\n", |
|
|
122 |
"df.head()" |
|
|
123 |
] |
|
|
124 |
}, |
|
|
125 |
{ |
|
|
126 |
"cell_type": "markdown", |
|
|
127 |
"metadata": {}, |
|
|
128 |
"source": [ |
|
|
129 |
"<h4>[-] Check for class imbalance in the <i>'results'</i> column </h4>" |
|
|
130 |
] |
|
|
131 |
}, |
|
|
132 |
{ |
|
|
133 |
"cell_type": "code", |
|
|
134 |
"execution_count": null, |
|
|
135 |
"metadata": {}, |
|
|
136 |
"outputs": [], |
|
|
137 |
"source": [ |
|
|
138 |
"print(\"PFS Overview\")\n", |
|
|
139 |
"print(df.result.value_counts())\n", |
|
|
140 |
"\n", |
|
|
141 |
"df.result.value_counts().plot(kind='pie', autopct='%1.0f%%', colors=['skyblue', 'orange'], explode=(0.05, 0.05))" |
|
|
142 |
] |
|
|
143 |
}, |
|
|
144 |
{ |
|
|
145 |
"cell_type": "markdown", |
|
|
146 |
"metadata": {}, |
|
|
147 |
"source": [ |
|
|
148 |
"<h4>[-] Label encoding of the categorical variables </h4>" |
|
|
149 |
] |
|
|
150 |
}, |
|
|
151 |
{ |
|
|
152 |
"cell_type": "code", |
|
|
153 |
"execution_count": null, |
|
|
154 |
"metadata": {}, |
|
|
155 |
"outputs": [], |
|
|
156 |
"source": [ |
|
|
157 |
"df['sex'] =df['sex'].astype('category')\n", |
|
|
158 |
"df['ceus'] =df['ceus'].astype('category')\n", |
|
|
159 |
"df['ala'] =df['ala'].astype('category')\n", |
|
|
160 |
"\n", |
|
|
161 |
"#df['Ki67'] =df['Ki67'].astype(int)\n", |
|
|
162 |
"df['MGMT'] =df['MGMT'].astype('category')\n", |
|
|
163 |
"df['IDH1'] =df['IDH1'].astype('category')\n", |
|
|
164 |
"\n", |
|
|
165 |
"df['side'] =df['side'].astype('category')\n", |
|
|
166 |
"df['ependima'] =df['ependima'].astype('category')\n", |
|
|
167 |
"df['cc'] =df['cc'].astype('category')\n", |
|
|
168 |
"df['necrotico_cistico'] =df['necrotico_cistico'].astype('category')\n", |
|
|
169 |
"df['shift'] =df['shift'].astype('category')\n", |
|
|
170 |
"\n", |
|
|
171 |
"## VARIABLE TO ONE-HOT-ENCODE\n", |
|
|
172 |
"df['localization'] =df['localization'].astype(int)\n", |
|
|
173 |
"df['clinica_esordio'] =df['clinica_esordio'].astype(int)\n", |
|
|
174 |
"df['immediate_p_o'] =df['immediate_p_o'].astype(int)\n", |
|
|
175 |
"df['onco_Protocol'] =df['onco_Protocol'].astype(int)\n", |
|
|
176 |
"\n", |
|
|
177 |
"df['result'] =df['result'].astype(int)\n", |
|
|
178 |
"\n", |
|
|
179 |
"dummy_v = ['localization', 'clinica_esordio', 'onco_Protocol', 'immediate_p_o']\n", |
|
|
180 |
"\n", |
|
|
181 |
"df = pd.get_dummies(df, columns = dummy_v, prefix = dummy_v)" |
|
|
182 |
] |
|
|
183 |
}, |
|
|
184 |
{ |
|
|
185 |
"cell_type": "markdown", |
|
|
186 |
"metadata": {}, |
|
|
187 |
"source": [ |
|
|
188 |
"<h2>[3] Prediction Models</h2>" |
|
|
189 |
] |
|
|
190 |
}, |
|
|
191 |
{ |
|
|
192 |
"cell_type": "markdown", |
|
|
193 |
"metadata": {}, |
|
|
194 |
"source": [ |
|
|
195 |
"<h4> [-] Training and testing set splitting</h4>" |
|
|
196 |
] |
|
|
197 |
}, |
|
|
198 |
{ |
|
|
199 |
"cell_type": "code", |
|
|
200 |
"execution_count": 5, |
|
|
201 |
"metadata": { |
|
|
202 |
"collapsed": true |
|
|
203 |
}, |
|
|
204 |
"outputs": [ |
|
|
205 |
{ |
|
|
206 |
"ename": "NameError", |
|
|
207 |
"evalue": "name 'df' is not defined", |
|
|
208 |
"output_type": "error", |
|
|
209 |
"traceback": [ |
|
|
210 |
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", |
|
|
211 |
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", |
|
|
212 |
"\u001b[0;32m<ipython-input-5-48cdcc32916c>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtarget\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mdf\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'result'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0minputs\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mdf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdrop\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'result'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'PFS'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0maxis\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'columns'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", |
|
|
213 |
"\u001b[0;31mNameError\u001b[0m: name 'df' is not defined" |
|
|
214 |
] |
|
|
215 |
} |
|
|
216 |
], |
|
|
217 |
"source": [ |
|
|
218 |
"target = df['result']\n", |
|
|
219 |
"inputs = df.drop(['result', 'PFS'], axis = 'columns')\n", |
|
|
220 |
"x_train, x_test, y_train, y_test = train_test_split(inputs['...'],target,test_size=0.20, random_state=10)" |
|
|
221 |
] |
|
|
222 |
}, |
|
|
223 |
{ |
|
|
224 |
"cell_type": "markdown", |
|
|
225 |
"metadata": {}, |
|
|
226 |
"source": [ |
|
|
227 |
"<h4> [-] Dummy Training </h4>" |
|
|
228 |
] |
|
|
229 |
}, |
|
|
230 |
{ |
|
|
231 |
"cell_type": "code", |
|
|
232 |
"execution_count": null, |
|
|
233 |
"metadata": {}, |
|
|
234 |
"outputs": [], |
|
|
235 |
"source": [ |
|
|
236 |
"dummy_train = DummyClassifier(strategy=\"uniform\", random_state = 42)\n", |
|
|
237 |
"dummy_train.fit(x_train, y_train)\n", |
|
|
238 |
"\n", |
|
|
239 |
"score_dummy_train = dummy_train.score(x_train, y_train)\n", |
|
|
240 |
"print(\"Dummy Train accuracy ***TRAIN***: \", round(score_dummy_train*100,2), \"% \\n\")\n", |
|
|
241 |
"\n", |
|
|
242 |
"y_dummy_train_predicted = dummy_train.predict(x_train)\n", |
|
|
243 |
"y_dummy_train_proba = dummy_train.predict_proba(x_train)\n", |
|
|
244 |
"\n", |
|
|
245 |
"cm_dummy_train = confusion_matrix(y_train, y_dummy_train_predicted)\n", |
|
|
246 |
"print(cm_dummy_train, \"\\n\")\n", |
|
|
247 |
"\n", |
|
|
248 |
"false_positive_rate, true_positive_rate, thresholds = roc_curve(y_train, y_dummy_train_predicted)\n", |
|
|
249 |
"roc_auc = auc(false_positive_rate, true_positive_rate)\n", |
|
|
250 |
"\n", |
|
|
251 |
"\n", |
|
|
252 |
"print('1. The F-1 Score of the model {} \\n '.format(round(f1_score(y_train, y_dummy_train_predicted, average = 'macro'), 2)))\n", |
|
|
253 |
"print('2. The Recall Score of the model {} \\n '.format(round(recall_score(y_train, y_dummy_train_predicted, average = 'macro'), 2)))\n", |
|
|
254 |
"print('3. Classification report \\n {} \\n'.format(classification_report(y_train, y_dummy_train_predicted)))\n", |
|
|
255 |
"print('3. AUC: \\n {} \\n'.format(roc_auc))\n", |
|
|
256 |
"\n", |
|
|
257 |
"tn, fp, fn, tp = cm_dummy_train.ravel()\n", |
|
|
258 |
"\n", |
|
|
259 |
"# Sensitivity, hit rate, Recall, or true positive rate\n", |
|
|
260 |
"tpr = tp/(tp+fn)\n", |
|
|
261 |
"print(\"Sensitivity (TPR): {}\".format(tpr))\n", |
|
|
262 |
"\n", |
|
|
263 |
"# Specificity or true negative rate\n", |
|
|
264 |
"tnr = tn/(tn+fp)\n", |
|
|
265 |
"print(\"Specificity (TNR): {}\".format(tnr))\n", |
|
|
266 |
"\n", |
|
|
267 |
"# Precision or positive predictive value\n", |
|
|
268 |
"ppv = tp/(tp+fp)\n", |
|
|
269 |
"print(\"Precision (PPV): {}\".format(ppv))\n", |
|
|
270 |
"\n", |
|
|
271 |
"# Negative predictive value\n", |
|
|
272 |
"npv = tn/(tn+fn)\n", |
|
|
273 |
"print(\"Negative Predictive Value (NPV): {}\".format(npv))\n", |
|
|
274 |
"\n", |
|
|
275 |
"# False positive rate\n", |
|
|
276 |
"fpr = fp / (fp + tn)\n", |
|
|
277 |
"print(\"False Positive Rate (FPV): {}\".format(fpr))\n", |
|
|
278 |
"\n", |
|
|
279 |
"tnr = tn/(tn+fp)\n" |
|
|
280 |
] |
|
|
281 |
}, |
|
|
282 |
{ |
|
|
283 |
"cell_type": "markdown", |
|
|
284 |
"metadata": {}, |
|
|
285 |
"source": [ |
|
|
286 |
"<h4> [-] Dummy Testing </h4>" |
|
|
287 |
] |
|
|
288 |
}, |
|
|
289 |
{ |
|
|
290 |
"cell_type": "code", |
|
|
291 |
"execution_count": null, |
|
|
292 |
"metadata": {}, |
|
|
293 |
"outputs": [], |
|
|
294 |
"source": [ |
|
|
295 |
"dummy_testing = DummyClassifier(strategy=\"uniform\", random_state = 42)\n", |
|
|
296 |
"dummy_testing.fit(x_test, y_test)\n", |
|
|
297 |
"\n", |
|
|
298 |
"score_dummy_testing = dummy_testing.score(x_test, y_test)\n", |
|
|
299 |
"print(\"Dummy Test accuracy ***TEST***: \", round(score_dummy_testing*100,2), \"% \\n\")\n", |
|
|
300 |
"\n", |
|
|
301 |
"y_dummy_testing_predicted = dummy_testing.predict(x_test)\n", |
|
|
302 |
"y_dummy_testing_proba = dummy_testing.predict_proba(x_test)\n", |
|
|
303 |
"\n", |
|
|
304 |
"cm_dummy_testing = confusion_matrix(y_test, y_dummy_testing_predicted)\n", |
|
|
305 |
"print(cm_dummy_testing, \"\\n\")\n", |
|
|
306 |
"\n", |
|
|
307 |
"false_positive_rate, true_positive_rate, thresholds = roc_curve(y_test, y_dummy_testing_predicted)\n", |
|
|
308 |
"roc_auc = auc(false_positive_rate, true_positive_rate)\n", |
|
|
309 |
"\n", |
|
|
310 |
"\n", |
|
|
311 |
"print('1. The F-1 Score of the model {} \\n '.format(round(f1_score(y_test, y_dummy_testing_predicted, average = 'macro'), 2)))\n", |
|
|
312 |
"print('2. The Recall Score of the model {} \\n '.format(round(recall_score(y_test, y_dummy_testing_predicted, average = 'macro'), 2)))\n", |
|
|
313 |
"print('3. Classification report \\n {} \\n'.format(classification_report(y_test, y_dummy_testing_predicted)))\n", |
|
|
314 |
"print('3. AUC: \\n {} \\n'.format(roc_auc))\n", |
|
|
315 |
"\n", |
|
|
316 |
"tn, fp, fn, tp = cm_dummy_train.ravel()\n", |
|
|
317 |
"\n", |
|
|
318 |
"# Sensitivity, hit rate, Recall, or true positive rate\n", |
|
|
319 |
"tpr = tp/(tp+fn)\n", |
|
|
320 |
"print(\"Sensitivity (TPR): {}\".format(tpr))\n", |
|
|
321 |
"\n", |
|
|
322 |
"# Specificity or true negative rate\n", |
|
|
323 |
"tnr = tn/(tn+fp)\n", |
|
|
324 |
"print(\"Specificity (TNR): {}\".format(tnr))\n", |
|
|
325 |
"\n", |
|
|
326 |
"# Precision or positive predictive value\n", |
|
|
327 |
"ppv = tp/(tp+fp)\n", |
|
|
328 |
"print(\"Precision (PPV): {}\".format(ppv))\n", |
|
|
329 |
"\n", |
|
|
330 |
"# Negative predictive value\n", |
|
|
331 |
"npv = tn/(tn+fn)\n", |
|
|
332 |
"print(\"Negative Predictive Value (NPV): {}\".format(npv))\n", |
|
|
333 |
"\n", |
|
|
334 |
"# False positive rate\n", |
|
|
335 |
"fpr = fp / (fp + tn)\n", |
|
|
336 |
"print(\"False Positive Rate (FPV): {}\".format(fpr))\n", |
|
|
337 |
"\n", |
|
|
338 |
"tnr = tn/(tn+fp)" |
|
|
339 |
] |
|
|
340 |
} |
|
|
341 |
], |
|
|
342 |
"metadata": { |
|
|
343 |
"kernelspec": { |
|
|
344 |
"display_name": "Python 3", |
|
|
345 |
"language": "python", |
|
|
346 |
"name": "python3" |
|
|
347 |
}, |
|
|
348 |
"language_info": { |
|
|
349 |
"codemirror_mode": { |
|
|
350 |
"name": "ipython", |
|
|
351 |
"version": 3 |
|
|
352 |
}, |
|
|
353 |
"file_extension": ".py", |
|
|
354 |
"mimetype": "text/x-python", |
|
|
355 |
"name": "python", |
|
|
356 |
"nbconvert_exporter": "python", |
|
|
357 |
"pygments_lexer": "ipython3", |
|
|
358 |
"version": "3.7.4" |
|
|
359 |
} |
|
|
360 |
}, |
|
|
361 |
"nbformat": 4, |
|
|
362 |
"nbformat_minor": 2 |
|
|
363 |
} |