This project builds an end-to-end deep learning pipeline for predicting Hospital Length of Stay (HLOS) for COVID-19 inpatients using clinical data from a Canadian hospital (508 patients). The pipeline covers data preprocessing, feature engineering, model training with hyperparameter tuning, and comprehensive evaluation across four distinct model architectures.
| # | Model | Input Data | Architecture |
|---|---|---|---|
| 1 | MLP | Static admission features | Feedforward network with BatchNorm + Dropout |
| 2 | LSTM | Sequential daily vitals/labs | Multi-layer LSTM with sequence-to-sequence output |
| 3 | Self-Attention | Sequential daily vitals/labs | Transformer-style multi-head self-attention |
| 4 | MLP + Word2Vec | Static features + NLP embeddings | MLP enriched with Word2Vec comorbidity embeddings |
| Model | MSE | RMSE | MAE | R² |
|---|---|---|---|---|
| MLP (static) | 152.15 | 12.33 | 7.82 | −0.20 |
| LSTM (sequential) | 103.17 | 10.16 | 6.80 | 0.13 |
| Self-Attention (sequential) | 57.37 | 7.57 | 4.33 | 0.52 |
| MLP + Word2Vec | 147.45 | 12.14 | 7.99 | −0.16 |
- Self-Attention achieves the best performance (R² = 0.52), outperforming the LSTM by a significant margin. The attention mechanism can directly model inter-day dependencies without the information bottleneck of recurrent hidden states.
- Sequential models (LSTM, Attention) substantially outperform static models (MLP), confirming that the temporal trajectory of patient vitals and lab values carries critical prognostic signal beyond a single admission snapshot.
- Word2Vec comorbidity embeddings provide marginal improvement over the plain MLP (MSE 147 vs 152). The structured binary comorbidity columns already capture most of the signal, and the small corpus (404 patients) limits Word2Vec's representational power.
Hospital-Length-of-Stay-Prediction/
├── HLOS_Unified_Pipeline.py # Full pipeline (standalone script)
├── notebooks/
│ └── HLOS_Unified_Pipeline.ipynb # Jupyter/Colab notebook version
├── assets/
│ └── architecture_overview.png # Model architecture diagram (optional)
├── requirements.txt # Python dependencies
├── .gitignore
├── LICENSE
└── README.md
Source: Canadian Hospital COVID-19 Inpatient Data
Format: Excel workbook with 4 sheets:
| Sheet | Description | Shape |
|---|---|---|
Data-at-admission |
Static patient features at admission (demographics, vitals, labs, comorbidities) | 508 × 55 |
Days-breakdown |
Daily vitals and lab values for each patient (14-day records, 8 recorded days) | 4,064 × 70 |
Hospital-length-of-stay |
Target variable — actual length of stay + outcomes | 508 × 11 |
Medication-Static-List |
Reference list of all medications | 31,254 × 2 |
Static Features (58 total):
- Demographics: age, sex, height, weight
- Vitals: systolic/diastolic BP, heart rate, respiratory rate, SpO2, temperature, GCS (motor/verbal/eye)
- Lab Values: CBC (WBC, RBC, hemoglobin, hematocrit, MCV, MCH, MCHC, RDW, platelets), coagulation (APTT, PT, INR), liver (ALT, AST, bilirubin), renal (creatinine, sodium, potassium), inflammatory (lactate, CRP, ferritin, D-dimer, ESR), ABG (PaO2, PaO2/FiO2, pH)
- Clinical: intubation status, 18 binary comorbidity indicators
- NLP (Model 4 only): 50-dimensional Word2Vec embeddings from free-text comorbidity descriptions
Sequential Features (33 per timestep): Daily vitals + labs + intubation status across 8 recorded days.
Note: The dataset file (
Canada_Hosp1_COVID_InpatientData.xlsx) is not included in this repository. Place it in the project root directory before running the pipeline.
- Python 3.8+
- PyTorch 2.0+
- CUDA-capable GPU (optional but recommended)
git clone https://github.com/rishishanthan/Hospital-Length-of-Stay-Prediction.git
cd Hospital-Length-of-Stay-Prediction
pip install -r requirements.txtOption 1 — Python Script:
python HLOS_Unified_Pipeline.pyOption 2 — Jupyter Notebook:
jupyter notebook notebooks/HLOS_Unified_Pipeline.ipynbOption 3 — Google Colab:
Click the "Open in Colab" badge at the top, upload the Excel dataset when prompted, and run all cells.
┌─────────────────────────────────────────────────────────────────────┐
│ RAW DATA (4 Excel Sheets) │
└──────────────┬──────────────────────────────────┬───────────────────┘
│ │
┌───────▼───────┐ ┌────────▼────────┐
│ Data-at- │ │ Days-breakdown │
│ admission │ │ (sequential) │
└───────┬───────┘ └────────┬────────┘
│ │
┌──────────▼──────────┐ ┌──────────▼──────────┐
│ Static Preprocessing │ │ Seq. Preprocessing │
│ • Merge with HLOS │ │ • Merge with HLOS │
│ • Parse comorbidities│ │ • Compute remaining │
│ • Encode categoricals│ │ LOS per day │
│ • Median imputation │ │ • Median imputation │
│ • StandardScaler │ │ • 3D tensor build │
│ • Word2Vec (Model 4) │ │ • StandardScaler │
└──────┬──────┬───────┘ └──────┬──────┬──────┘
│ │ │ │
┌─────▼──┐ ┌─▼──────────┐ ┌───────▼──┐ ┌─▼───────────┐
│ MLP │ │ MLP+Word2Vec│ │ LSTM │ │ Attention │
│Model 1 │ │ Model 4 │ │ Model 2 │ │ Model 3 │
└────┬───┘ └─────┬──────┘ └────┬─────┘ └──────┬──────┘
│ │ │ │
└─────┬─────┘ └──────┬───────┘
│ │
┌──────▼─────────────────────────────────▼──────┐
│ Evaluation & Comparison │
│ MSE · RMSE · MAE · R² · 5-Trial Analysis │
└────────────────────────────────────────────────┘
MLP Configurations
| Config | Hidden Layers | Dropout | Learning Rate | Test MSE |
|---|---|---|---|---|
| 1 ✅ | [128, 64] | 0.3 | 1e-3 | 152.15 |
| 2 | [256, 128, 64] | 0.3 | 1e-3 | 170.97 |
| 3 | [128, 64, 32] | 0.2 | 5e-4 | 190.86 |
| 4 | [256, 128, 64, 32] | 0.3 | 1e-3 | 165.23 |
LSTM Configurations
| Config | Hidden Dim | Layers | Dropout | Learning Rate | Test MSE |
|---|---|---|---|---|---|
| 1 | 64 | 1 | 0.0 | 1e-3 | 108.58 |
| 2 | 64 | 2 | 0.2 | 1e-3 | 104.00 |
| 3 | 128 | 2 | 0.2 | 5e-4 | 108.55 |
| 4 ✅ | 128 | 3 | 0.3 | 5e-4 | 103.17 |
Self-Attention Configurations
| Config | d_model | Heads | Layers | Dropout | Learning Rate | Test MSE |
|---|---|---|---|---|---|---|
| 1 | 64 | 4 | 2 | 0.2 | 1e-3 | 61.95 |
| 2 ✅ | 64 | 4 | 3 | 0.2 | 5e-4 | 57.37 |
| 3 | 128 | 4 | 2 | 0.2 | 5e-4 | 70.21 |
| 4 | 128 | 8 | 3 | 0.3 | 5e-4 | 64.08 |
MLP + Word2Vec Configurations
| Config | Hidden Layers | Dropout | Learning Rate | Test MSE |
|---|---|---|---|---|
| 1 | [128, 64] | 0.3 | 1e-3 | 149.05 |
| 2 ✅ | [256, 128, 64] | 0.3 | 1e-3 | 147.45 |
| 3 | [256, 128, 64, 32] | 0.2 | 5e-4 | 181.87 |
| Model | Training MSE (mean ± std) | Test MSE (mean ± std) |
|---|---|---|
| MLP | 35.70 ± 19.05 | 159.47 ± 24.24 |
| LSTM | 122.58 ± 23.10 | 115.96 ± 8.26 |
The LSTM shows lower variance across trials (std = 8.26 vs 24.24), indicating more stable generalization. The MLP's large train-test gap confirms overfitting on the small static dataset.
- Imputation: KNN or iterative imputation instead of simple median
- Target transform: Log-transform the right-skewed HLOS distribution
- Architecture: Bidirectional LSTM, GRU variants, Temporal Fusion Transformers
- NLP: Pre-train Word2Vec on a larger medical corpus (e.g., PubMed/MIMIC-III notes)
- Ensembling: Combine predictions from multiple models
- Feature engineering: Add missingness indicator features for highly sparse columns (PaO2, ferritin, ESR have >90% missing)
- Deep Learning: PyTorch
- NLP: Gensim (Word2Vec)
- Data Processing: Pandas, NumPy, Scikit-learn
- Environment: Google Colab / Jupyter Notebook
This project is licensed under the MIT License — see the LICENSE file for details.
Rishi Shanthan Bhagavatham
MS in Engineering Science (Data Science) — University at Buffalo (SUNY)