Transforming Healthcare: Project-based Deep Learning-Powered Survival Prediction

Abhinaya Saravanan Last Updated : 12 May, 2023
11 min read

Introduction

Predicting patient outcomes is critical to healthcare management, enabling hospitals to optimize resources and improve patient care. Machine learning algorithms or deep learning techniques have proven valuable in survival prediction rates, offering insights that can help guide treatment plans and prioritize resources.

In this captivating blog, I promise to make you all delve into the following learning objectives:

  1. To uncover the power of deep learning in revolutionizing healthcare through a patient survival prediction project.
  2. Gain a comprehensive understanding of the end-to-end machine learning solution, from data preprocessing to model deployment.
  3. Discover the elegance of the Keras model architecture and its seamless integration into the project workflow.
  4. Learn how to leverage Streamlit and cloud services to create an interactive, user-friendly web application for real-world deployment.

"survival prediction

Source: biteable.com

This article was published as a part of the Data Science Blogathon.

Table of Contents

Project Description

The Hospital Patient Survival Prediction-Deployment project is a machine learning-based web application that predicts the probability of the patient surviving during their stay in a hospital. This project can be helpful for healthcare organizations and hospitals to prioritize care and allocate resources for patients at higher risk of adverse outcomes. It also has the potential to improve patient care and overall hospital efficiency.

"survival prediction

GitHub Repository

The complete code and related files for this project can be found at the following GitHub repository: https://github.com/Abhissaro/Hospital_PatientSurvival_Prediction-Deployment

Problem Statement

This project aims to build and deploy a machine learning model that accurately predicts the survival probability of a patient during their hospital stay, based on their medical records and other relevant information.

"survival prediction

Our primary goal is to classify the ‘hospital_death’ variable, which is binary in nature, using the 84 available features as predictors. The classification process will be carried out step-by-step, focusing on different aspects of the problem. The model’s performance will be evaluated using accuracy and the area under the Receiver Operating Characteristic (ROC) curve as the primary scoring metrics.

Prerequisites

To understand and complete this project, one should have a basic understanding of Python programming, machine learning algorithms, and libraries such as pandas, scikit-learn, keras architecture, and Streamlit.

Dataset Description

The dataset used for this project is sourced from the Global Open Source Severity of Illness Score (GOSSIS) study. The study, published in the journal Critical Care Medicine, aimed to develop and evaluate a large, open-source, international, and multicenter dataset of intensive care unit (ICU) patients. The dataset has nearly 100K rows and includes various features such as age, gender, type of admission, length of stay, and diagnosis. Detailed information about each feature is provided in the dataset’s documentation. The dataset includes various clinical parameters and patient outcomes. You can find more information about the dataset and the study in the following link:

https://journals.lww.com/ccmjournal/Citation/2019/01001/33__THE_GLOBAL_OPEN_SOURCE_SEVERITY_OF_ILLNESS.36.aspx

Please note that you might need to request access to the dataset from the authors or the institution responsible for the study.

Below is an overview of the basic steps followed to fine-tune the model to get the desired output for the prediction of patient survival for a better understanding before we delve into the coding part.

Data Preprocessing and Feature Engineering

  • Handling missing values: Numerical features’ missing values were filled with mean, and categorical features’ missing values were filled with mode.
  • Encoding categorical features using LabelEncoder.
  • Checking for outliers and plotting the distributions of some features.

Model Building and Feature Selection

  • Keras model building and Splitting the dataset into training and testing sets.
  • Selecting the top 6 features using mutual_info_classif.

Data Standardization and Model Training

  • Standardizing the dataset using StandardScaler.
  • Training the deep learning model using Keras.

Model Evaluation and Visualization

  • Evaluating the model’s accuracy, precision, and recall on the testing dataset.
  • Plotting the accuracy and loss curves.

Saving and Loading the Model

  • Saving the trained model as ‘keras_model.h5’ and loading it for further use.

Explainable AI with SHAP

  • Using SHAP to explain the model’s predictions and visualize the feature importance.

Code, Explanations & Key Observations

Let’s dive right into the exhilarating world of code and start building our exceptional Hospital Patient Survival Prediction ipynb file and relava
App. Prepare to be amazed by the incredible features and seamless user experience. Get ready to bring your A-game, and let’s get coding!

Install and Import Libraries

!pip install tensorflow

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
import warnings
warnings.filterwarnings('ignore')
  • Load and explore the dataset:
df = pd.read_csv("D:/TMLC/Patient survival DL1/Dataset/Dataset.csv")
df.head()

df.info()
df.shape

# Checking missing values
df.isnull().any().sum()
df.isnull().sum().sort_values(ascending=True)

# Statistical measures about the data
df.describe()

# Checking the distribution of the target variable
df['hospital_death'].value_counts()
df.groupby('hospital_death').mean()
sns.set_style('whitegrid')
sns.countplot(x='hospital_death',data=df)

 

Output observations:

  1. The DataFrame has 91,713 rows (entries) and 186 columns (features).
  2. The data types of the columns include float64 (170 columns), int64 (8 columns), and object (8 columns).
  3. The number of missing values in each column is sorted in ascending order. The column with the least missing values is ‘encounter_id’ with 0 missing values, and the columns with the most missing values are ‘h1_bilirubin_max’ and ‘h1_bilirubin_min’, both having 84,619 missing values.
  4. The ‘hospital_death’ column is the target variable, and it has two classes: 0 (no death) and 1 (death). There are 83,798 instances of class 0 and 7,915 instances of class 1.
  5. The mean values for each feature are grouped by the ‘hospital_death’ column, indicating how different features vary between the two classes.

Data Preprocessing and Feature Engineering for HealthCare Dataset

object_columns = df.select_dtypes(include=['object']).columns
cols = df.select_dtypes([np.number]).columns

# Checking outliers
sns.distplot(df['hospital_death']) 
sns.distplot(df['hepatic_failure']) 
sns.distplot(df['solid_tumor_with_metastasis']) 

# Filling missing values
df[cols] = df[cols].fillna(df[cols].mean())

object_columns = ['ethnicity', 'gender', 'hospital_admit_source', 'icu_admit_source',
       'icu_stay_type', 'icu_type', 'apache_3j_bodysystem',
       'apache_2_bodysystem']

for i in object_columns:
    df[i].fillna(df[i].mode()[0], inplace=True)
    print(i)

df.isnull().sum()

# Encoding categorical features
from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()

df[object_columns] = df[object_columns].apply(le.fit_transform)
df.head()
df.info()

Output’s key observations:

  1. Missing values in numerical columns are filled with their respective mean values, and categorical columns are filled with their respective modes.
  2. Categorical columns are encoded using LabelEncoder from the scikit-learn library, converting them into numerical values.
  3. All 186 columns in the DataFrame have numerical values (int64 or float64). The dataset is ready for further analysis and model building.

Model Building and Feature Selection for HealthCare Dataset

X = df.drop(columns='hospital_death', axis=1)
Y = df['hospital_death']

from sklearn.model_selection import train_test_split
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2, random_state=2)

from sklearn.feature_selection import mutual_info_classif
mutual_info = mutual_info_classif(X_train, Y_train)
mutual_info = pd.Series(mutual_info)
mutual_info.index = X_train.columns
mutual_info.sort_values(ascending=False).plot.bar(figsize=(20, 8))

from sklearn.feature_selection import SelectKBest
sel_six_cols = SelectKBest(mutual_info_classif, k=6)
sel_six_cols.fit(X_train, Y_train)

X_train_new = sel_six_cols.transform(X_train)
X_test_new = sel_six_cols.transform(X_test)

Output’s Key observation:

  1. The dataset has been split into training (73,370 rows) and testing (18,343 rows) sets with 185 features.
  2. Mutual information has been calculated for each feature with respect to the target variable using the ‘mutual_info_classif’ function from scikit-learn.
  3. The mutual information scores range from 0.000000 to 0.074317.
  4. Based on their mutual information score, the top six important features are selected for simplifying the model and to improve its performance by adding on the most relevant features for predicting the target variable.

Data Standardization and Model Training for HealthCare Dataset

from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
X_train_std = scaler.fit_transform(X_train_new)
X_test_std = scaler.transform(X_test_new)

from keras import Sequential
from keras.layers import Dense
model = Sequential()
model.add(Dense(12, input_dim=6, activation='relu'))
model.add(Dense(8, activation='relu'))
model.add(Dense(1, activation='sigmoid'))

import tensorflow as tf
model.compile(
    loss = tf.keras.losses.binary_crossentropy,
    optimizer = tf.keras.optimizers.Adam(learning_rate = 0.02),
    metrics = [
        tf.keras.metrics.BinaryAccuracy(name='accuracy'),
        tf.keras.metrics.Precision(name='precision'),
tf.keras.metrics.Recall(name='recall')
]
)

Fit the Keras model on the dataset

history = model.fit(X_train_std, Y_train, epochs=20, validation_split=0.2, batch_size=10)#import csv

Output’ key Observation:

  1. At the end of the training process (Epoch 20), the model achieves the following performance metrics on the training set: Loss: 0.2219, Accuracy: 92.12%, Precision: 66.95%, Recall: 21.13%.
  2. In addition to the training set, the model’s performance is also evaluated on a validation set. At the end of the 20th epoch, the model achieves the following performance metrics on the validation set: Loss: 0.2172, Accuracy: 92.31%, Precision: 71.90%, Recall: 17.42%.

From these results, it can be observed that the l achieves relatively high accuracy on both the training and validation sets.

Model Evaluation

# Evaluate the keras model
_, accuracy = model.evaluate(X_test_std, Y_test)
print('Accuracy: %.2f' % (accuracy*100))
_, precision = model.evaluate(X_test_std, Y_test)
print('Precision: %f' % precision)
_, recall = model.evaluate(X_test_std, Y_test)
print('Recall: %f' % recall)

# Plotting accuracy and loss curves
plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])

plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['training data', 'validation data'], loc='lower right')

plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])

plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['training data', 'validation data'], loc='upper right')
HealthCare

HealthCare

 HealthCare

Output’s Key observation:

  1. The loss for the validation set decreases significantly compared to the training set, indicating that the model generalizes well to the unseen data.
  2. The validation set’s accuracy is higher than the training set, suggesting that the model performs better on the validation data.
  3. The spikes in each epoch might indicate fluctuations in the model’s performance during training. These spikes can be attributed to the learning rate, batch size, or other hyperparameters.
"survival prediction | HealthCare

Saving and Loading the HealthCare Model

# Saving the model
model.save('keras_model.h5')

# Loading the model
from keras.models import load_model
model = load_model('keras_model.h5')
survival prediction | HealthCare

Key observation:

  1. The Keras method is called to save the model as ‘keras_model.h5’, an HDF5 format in which the model’s weight and configurations are structured for further evaluations and predictions in deep learning modeling.

Model Explainability for HealthCare Model

import shap
shap.initjs()

X_sample = pd.DataFrame(X_train_new, columns=X_train.columns[sel_six_cols.get_support()]).sample(100)
explainer = shap.KernelExplainer(model.predict, X_train_new)
shap_values = explainer.shap_values(X_sample)

# SHAP summary plot
shap.summary_plot(shap_values, X_sample, plot_type="bar")

# Force plot
shap.force_plot(explainer.expected_value[0], shap_values[0], features=X_sample)

# SHAP summary plot for individual features
shap.summary_plot(shap_values[0], features=X_sample)
survival prediction | HealthCare

Key Observations:

  1. ‘shap_values’: The ‘explainer.shap_values(X_sample)’ function computes SHAP values for a given sample of input features (X_sample). SHAP values help understand each feature’s contribution toward the model’s prediction for a specific instance.
  2. Summary Plot: The ‘shap.summary_plot(shap_values, X_sample, plot_type=”bar”)’ generates a bar chart summarizing the importance of each feature across all instances in X_sample. The features are ranked by their average absolute SHAP values, providing an overall understanding of the impact of each feature on the model’s predictions.
  3. Force Plot: The ‘shap.force_plot(explainer.expected_value[0], shap_values[0], features = X_sample)’ function creates a forced plot for a single instance (shap_values[0]) within the sample. The force plot visualizes the contributions of each feature towards pushing the model’s prediction above or below the expected value (average prediction for the dataset). It helps to understand how individual features positively or negatively influence the prediction for a specific instance.

Preparing Files for HealthCare Model Deployment

To deploy the patient survival prediction model using Streamlit, we need to prepare several files, including the Python files for processing user data and returning predictions and the Streamlit code for the front end, which are explained below, one by one.

Preparing Python Files

  1. Create a Python file (e.g., prediction.py) to process user data and return predictions from the trained deep learning model. This file should contain functions to preprocess the input data and call the model for predictions.
  2. Create a Python file (e.g., app.py) for the Streamlit front-end code. This file should include the necessary code to create input widgets, get predictions using the functions from the prediction.py file, and display the results in the web application.
import pandas as pd
import numpy as np
import streamlit as st
from keras.models import load_model
from tensorflow import keras
import tensorflow as tf

from prediction import get_prediction

st.set_page_config(page_title='Hostpital Patient Survival Prediction', page_icon="🏥", layout="wide", initial_sidebar_state='expanded')

model = load_model('keras_model.h5')

# creating option list for dropdown menu

features = ['apache_3j_diagnosis','gcs_motor_apache', 'd1_lactate_max', 'd1_lactate_min','apache_4a_hospital_death_prob', 'apache_4a_icu_death_prob']
#'apache_3j_diagnosis', 'gcs_motor_apache', 'd1_lactate_max','d1_lactate_min', 'apache_4a_hospital_death_prob', 'apache_4a_icu_death_prob'], dtype='object'

st.markdown("<h1 style='text-align: center;'>Patient Survival Prediction App 🏥 </h1>", unsafe_allow_html=True)
def main():
    with st.form('prediction_form'):

        st.header("Predict the input for following features:") 
                
        apache_3j_diagnosis = st.slider('gcs_motor_apache', 0.0300, 2201.05, value=1.0000, format="%f")  
        gcs_motor_apache = st.slider('gcs_motor_apache', 1.0000, 6.0000, value=1.0000, format="%f")
        d1_lactate_max = st.selectbox( 'd1_lactate_max:', [3.970, 5.990, 0.900, 1.100, 1.200, 2.927, 9.960, 19.500])
        d1_lactate_min = st.selectbox('d1_lactate_min:', [2.380, 2.680, 6.860, 0.900, 1.100, 1.200, 1.000, 2.125])
        apache_4a_hospital_death_prob = st.selectbox( 'apache_4a_hospital_death_prob:', [0.990, 0.980, 0.950, 0.040, 0.030, 0.086, 0.020, 0.010])
        apache_4a_icu_death_prob = st.selectbox('apache_4a_icu_death_prob:', [0.950, 0.940, 0.920, 0.030, 0.043, 0.030, 0.043, 0.020, 0.010])
        submit = st.form_submit_button("Predict")

    if submit:

        data= np.array([apache_3j_diagnosis, gcs_motor_apache, d1_lactate_max, d1_lactate_min, apache_4a_hospital_death_prob, apache_4a_icu_death_prob]).reshape(1, -1)
        
        pred = get_prediction(data=data, model=model)

        if pred[0][0]< 0.5:
            survival = 'No'
        elif pred[0][0] > 0.5:
            survival = 'Yes'

        
        
        st.write(f"The predicted Patient Survival is:  {survival}")


if __name__ == '__main__':
       main()
"prediction" file:
import numpy as np
import tensorflow as tf 
from tensorflow import keras

from keras.models import load_model

model = load_model('keras_model.h5')

def get_prediction(data,model):
    """
    Predict the class of a given data point.
    """
    return model.predict(data)

Preparing Files for Streamlit Deployment:

Creating “requirements.txt”: This file lists all the libraries required for your project. To generate it, follow these steps:

streamlit==1.15.2
numpy==1.21.5
pandas==1.3.5
keras==2.11.0
tensorflow==2.11.0
scikit-learn==1.0.2
protobuf==3.19.6
"runtime.txt":
Python 3.11.0
"setup.sh" file:
#import csv
HealthCare using deep learning | survival prediction

Pushing the HealthCare Project to GitHub

After preparing the necessary files, create a new repository on GitHub and push the project folder containing all the files to the repository. You can follow this guide on adding files to a GitHub repository or watch this video tutorial.

Once your deep learning project is pushed to GitHub, we can proceed with the deployment process on Streamlit.

Deploying the deep learning Project with Streamlit:

To deploy your project on Streamlit, you must have an account on Streamlit Sharing. After creating an account and signing in, follow these steps:

  1. Click on ‘New App’ in the Streamlit Sharing dashboard.
  2. In the ‘GitHub URL’ field, enter your GitHub repository’s URL containing the project.
  3. Choose the branch you want to deploy from (usually ‘main’ or ‘master’).
  4. Specify the file path of your Streamlit app (e.g., app.py) in the ‘Path to Streamlit app’ field.
  5. Click on ‘Deploy’.

Streamlit will deploy your app, and you will receive a URL to access the deployed application.

That’s all, folks! We’ve successfully created a cutting-edge, visually appealing Hospital Patient Survival Prediction App.

Introducing the Patient Survival Prediction App, an innovative and user-friendly tool designed to revolutionize the healthcare industry. This powerful web-based application is hosted live and can be accessed at Hospital Patient Survival Prediction.

survival prediction | HealthCare

Conclusion

The Patient Survival Prediction deep learning model App for healthcare harnesses the power of machine learning and advanced analytics to predict a patient’s likelihood of survival in a hospital setting. The cutting-edge model, trained on numerous critical features, is adept at processing complex medical data to deliver accurate predictions.

We have chosen the Render cloud platform to deploy this fantastic app, ensuring seamless performance and exceptional user experience. By leveraging the robust capabilities of Render, we have made the app available to medical professionals and researchers globally.

Click here to discover how the Patient Survival Prediction App can revolutionize healthcare!

HealthCare Project | survival prediction

Key Takeaways

  1. Innovative Machine Learning Model: We have developed a deep learning model app that harnesses the power of machine learning, utilizing a meticulously trained and finely tuned model to predict patient survival with remarkable accuracy.
  2. Seamless User Interface: The Hospital Patient Survival Prediction App for healthcare transformation boasts a sleek and user-friendly interface, making it simple for healthcare professionals to input relevant patient data and obtain prompt predictions.
  3. Deployment on a Cloud Platform: By leveraging the Render cloud platform, the app is effortlessly accessible to users worldwide, enabling healthcare professionals across the globe to benefit from its incredible predictive capabilities.
  4. End-to-End Solution: This comprehensive project showcases the seamless integration of various components, including data processing, machine learning model training, user interface design, and deployment, making it a shining example of an end-to-end solution in the field of healthcare technology.

The media shown in this article is not owned by Analytics Vidhya and is used at the Author’s discretion. 

A Computer Science Engineer cum Robotics/IT Teacher with experience in Machine Learning, Modelling and it's Deployment. Skilled in Machine Learning, Deep Learning, NLP, CV. I'm eager to learn and explore more about Artificial Intelligence, ML, Data Science, Deep Learning and Robotics.

Responses From Readers

We use cookies essential for this site to function well. Please click to help us improve its usefulness with additional cookies. Learn about our use of cookies in our Privacy Policy & Cookies Policy.

Show details