342 lines (341 with data), 9.2 kB
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h1 align=\"center\">Machine learning-based prediction of early recurrence in glioblastoma patients: a glance towards precision medicine <br><br>[Logistic Regression]</h1>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## [1] Library"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# OS library\n",
"import os\n",
"import sys\n",
"import argparse\n",
"\n",
"# Analysis\n",
"import numpy as np\n",
"import pandas as pd\n",
"import seaborn as sns\n",
"import matplotlib.pyplot as plt\n",
"\n",
"# Sklearn\n",
"from boruta import BorutaPy\n",
"from sklearn.preprocessing import LabelEncoder\n",
"from sklearn.model_selection import train_test_split\n",
"from sklearn.metrics import confusion_matrix, f1_score, recall_score, classification_report, accuracy_score, auc, roc_curve\n",
"from sklearn.model_selection import GridSearchCV\n",
"from sklearn.linear_model import LogisticRegression\n",
"\n",
"import scikitplot as skplt\n",
"from imblearn.over_sampling import RandomOverSampler, SMOTENC, SMOTE"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## [2] Data Preprocessing"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h4>[-] Load the database</h4>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"file = os.path.join(sys.path[0], \"db.xlsx\")\n",
"db = pd.read_excel(file)\n",
"\n",
"print(\"N° of patients: {}\".format(len(db)))\n",
"print(\"N° of columns: {}\".format(db.shape[1]))\n",
"db.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h4>[-] Drop unwanted columns + create <i>'results'</i> column</h4>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"df = db.drop(['Name_Surname', '...'], axis = 'columns')\n",
"\n",
"print(\"Effective features to consider: {} \".format(len(df.columns)-1))\n",
"print(\"Creating 'result' column...\")\n",
"\n",
"# 0 = No relapse\n",
"df.loc[df['PFS'] > 6, 'result'] = 0\n",
"\n",
"# 1 = Early relapse (within 6 months)\n",
"df.loc[df['PFS'] <= 6, 'result'] = 1\n",
"\n",
"df.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h4>[-] Label encoding of the categorical variables </h4>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"df['sex'] =df['sex'].astype('category')\n",
"df['ceus'] =df['ceus'].astype('category')\n",
"df['ala'] =df['ala'].astype('category')\n",
"\n",
"#df['Ki67'] =df['Ki67'].astype(int)\n",
"df['MGMT'] =df['MGMT'].astype('category')\n",
"df['IDH1'] =df['IDH1'].astype('category')\n",
"\n",
"df['side'] =df['side'].astype('category')\n",
"df['ependima'] =df['ependima'].astype('category')\n",
"df['cc'] =df['cc'].astype('category')\n",
"df['necrotico_cistico'] =df['necrotico_cistico'].astype('category')\n",
"df['shift'] =df['shift'].astype('category')\n",
"\n",
"## VARIABLE TO ONE-HOT-ENCODE\n",
"df['localization'] =df['localization'].astype(int)\n",
"df['clinica_esordio'] =df['clinica_esordio'].astype(int)\n",
"df['immediate_p_o'] =df['immediate_p_o'].astype(int)\n",
"df['onco_Protocol'] =df['onco_Protocol'].astype(int)\n",
"\n",
"df['result'] =df['result'].astype(int)\n",
"\n",
"dummy_v = ['localization', 'clinica_esordio', 'onco_Protocol', 'immediate_p_o']\n",
"\n",
"df = pd.get_dummies(df, columns = dummy_v, prefix = dummy_v)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## [3] Prediction Models"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h4> [-] Training and testing set splitting</h4>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"target = df['result']\n",
"inputs = df.drop(['result', 'PFS'], axis = 'columns')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Select columns (variable) at a univariate analysis ad a p-value lower than 0.05"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"cols = ['age', 'EOR', \n",
" 'onco_Protocol_0','onco_Protocol_1', 'onco_Protocol_2', \n",
" 'onco_Protocol_3', 'onco_Protocol_5', 'MGMT', \n",
" 'IDH1', 'edema volume', 'residual_tumor', \n",
" 'KPS_preop', 'KPS_postop']\n",
"\n",
"\n",
"x_train, x_test, y_train, y_test = train_test_split(inputs[cols],target,test_size=0.20, random_state=42)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h4> [-] SMOTE-NC</h4>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"os = SMOTENC(categorical_features=[2,3,4,5,6,7,8], k_neighbors=4, random_state= 42)\n",
"smote_x,smote_y= os.fit_sample(x_train, y_train)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h4> [-] Grid Search Hyperparameter tuning</h4>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"random_grid = [{'penalty' : ['l1', 'l2', 'elasticnet', 'none'],\n",
" 'C': [0.001, 0.01, 0.1, 1, 10, 100, 1000]}]\n",
"\n",
"# First create the base model to tune\n",
"lg = LogisticRegression(random_state=42)\n",
"\n",
"# Random search of parameters, using 5 fold cross validation, different combinations, and use all available cores\n",
"lg_random = GridSearchCV(estimator = lg, param_grid=random_grid,\n",
" cv = 5)\n",
"# Fit the random search model\n",
"lg_random.fit(x_train, y_train)\n",
"lg_random.best_params_"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h4> [-] Logistic Regression</h4>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"log_pfs = LogisticRegression(random_state=42, penalty='l2', C=10)\n",
"log_pfs.fit(smote_x, smote_y)\n",
"\n",
"score_log = log_pfs.score(x_test, y_test)\n",
"print(\"### TESTING ###\")\n",
"print(\"Logistic Regression's accuracy: \", round(score_log*100,2), \"% \\n\")\n",
"\n",
"y_pred = log_pfs.predict(x_test)\n",
"y_proba = log_pfs.predict_proba(x_test)\n",
"cm_log = confusion_matrix(y_test, y_pred)\n",
"print(cm_log, \"\\n\")\n",
"\n",
"false_positive_rate, true_positive_rate, thresholds = roc_curve(y_test, y_pred)\n",
"roc_auc = auc(false_positive_rate, true_positive_rate)\n",
"\n",
"\n",
"print('1. The F-1 Score of the model {} \\n '.format(round(f1_score(y_test, y_pred, average = 'macro'), 2)))\n",
"print('2. The Recall Score of the model {} \\n '.format(round(recall_score(y_test, y_pred, average = 'macro'), 2)))\n",
"print('3. Classification report \\n {}'.format(classification_report(y_test, y_pred)))\n",
"print('3. AUC: \\n {} \\n'.format(roc_auc))\n",
"\n",
"tn, fp, fn, tp = cm_log.ravel()\n",
"\n",
"# Sensitivity, hit rate, Recall, or true positive rate\n",
"tpr = tp/(tp+fn)\n",
"print(\"Sensitivity (TPR): {}\".format(tpr))\n",
"\n",
"# Specificity or true negative rate\n",
"tnr = tn/(tn+fp)\n",
"print(\"Specificity (TNR): {}\".format(tnr))\n",
"\n",
"# Precision or positive predictive value\n",
"ppv = tp/(tp+fp)\n",
"print(\"Precision (PPV): {}\".format(ppv))\n",
"\n",
"# Negative predictive value\n",
"npv = tn/(tn+fn)\n",
"print(\"Negative Predictive Value (NPV): {}\".format(npv))\n",
"\n",
"# False positive rate\n",
"fpr = fp / (fp + tn)\n",
"print(\"False Positive Rate (FPV): {}\".format(fpr))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}