Skip to main content
NIHPA Author Manuscripts logoLink to NIHPA Author Manuscripts
. Author manuscript; available in PMC: 2026 Jul 10.
Published in final edited form as: Heart Lung. 2026 May 24;79:102852. doi: 10.1016/j.hrtlng.2026.102852

Optimizing Heart Failure Care: A Machine Learning-Based Prediction of Hospital Length of Stay for Heart Failure Patients

Arthur De Souza 1, Ray Opoku 2
PMCID: PMC13349166  NIHMSID: NIHMS2189421  PMID: 42176646

Abstract

Background

Heart failure represents a significant global health burden, with prolonged length of stay (LoS) tied to increased mortality and costs. Accurate prediction of hospital LoS is crucial for improving resource allocation, lowering mortality and readmission rates, and enhancing patient care.

Objectives

This study leverages machine learning (ML) models to predict LoS categories (Short: 1–3 days; Medium: 4–7 days; Long: ≥8 days) for heart failure patients, with a goal of developing and benchmarking a predictive model.

Methods

This was a retrospective analysis of 2,008 heart failure patients with 168 clinical variables available at the time of or shortly after admission. The analysis utilized a multiclass classification approach, with thirteen different ML models trained and validated using stratified 10-fold cross-validation.

Results

Among the models evaluated, the LightGBM model demonstrated the highest performance, achieving a micro-average ROC AUC of 0.78 and a macro-average ROC AUC of 0.68, with mean accuracy of 61%. Feature importance analysis identified brain natriuretic peptide (BNP), creatine kinase, and high-density lipoprotein cholesterol as the top three variables influencing the prediction.

Conclusions

The model identifies key biomarkers to alert clinicians to patients requiring intensive care, potentially reducing hospital costs and improving resource allocation. The results further underscore the potential of machine learning to support clinical decision-making by establishing a strong performance baseline for heart failure care. Future work should focus on leveraging larger and diverse datasets and additional models to enhance predictive accuracy.

Keywords: Heart Failure, Length of Stay, Machine Learning, Prediction

Introduction

Heart failure affects 26 million people worldwide and 6.2 million in the United States, resulting in over a million hospitalizations annually in the United States.1,2 Prolonged hospital stays are associated with higher mortality risk3, highlighting the need to identify patients at higher risks of having a long length of stay so that the medical staff can make the proper accommodations early on in the patient’s care. Providing the medical staff the ability to properly predict the length of stay of a patient with heart failure could lead to lowered mortality, lowered readmission rates, decreased hospital costs, as well as improved patient care and resource allocation.

Physicians and the medical team have traditionally relied on their training and expertise to predict the length of stay for patients in order to properly treat the severity of their disease. As artificial intelligence and machine learning continue to make advancements, they prove themselves as a useful tool to make predictions based on preset data. These models account for complex interplay of numerous variables with similar hidden patterns that may not be apparent using traditional methods, such as human observers or simpler statistical techniques. Furthermore, they can handle high-dimensional data with numerous features more effectively to potentially uncover subtle patterns that influence length of stay. Lastly, just as medical personnel adapt and improve over time with more experience, machine learning works in similar ways. As it is exposed to more data, it can adapt and improve the model in a way that allows continuous refinement of the algorithm.

There have been other research studies in the past that have also explored how to use machine learning to predict length of stay. For instance, Jain et al. (2024) used random forest, linear regression, and CART models to predict length of stay for general hospital admissions, including 285 diagnosis codes from the Clinical Classification System4. However, this study had its limitations as it achieved an accuracy of 78% and did not focus on LoS for a specific disease. This study aims to address these gaps by focusing on LoS for heart failure patients, using a large dataset, and exploring a wide range of machine learning models to achieve a higher accuracy.

Hypothesis and Rationale

We hypothesize that a LightGBM model, trained on a dataset of 2,008 heart failure patients with 168 clinical variables, could achieve a superior discriminatory power compared to traditional statistical methods. The dataset, sourced from PhysioNet5, includes retrospective electronic health records from a hospital in Sichuan, China, collected between 2016 and 2019. The data was obtained at mandatory follow-up visit at 28 days, 3 months and 6 months.

In practice, being able to predict the length of stay for heart failure patients would optimize resource allocation so that hospitals could better plan staffing levels, bed assignments and equipment usage based on the predicted LoS. Furthermore, this would allow early and possibly more aggressive interventions to be provided to the patients with longer LoS to reduce their LoS and improve patient care. As for when the model predicts a shorter LoS, it could enhance discharge planning by starting the process sooner and reducing risk of readmission. An accurate prediction of the patients LoS could aid in communicating expectations to the family in order to improve their personal planning. Eventually these models can be integrated into clinical decision support systems to ensure physicians are aware of when patients may require additional attention, aiding in their clinical decision making. Lastly, the hospital could be able to more accurately predict their costs and revenues for better financial management, then potentially reducing costs for patients.

Specific Aims

  1. Gather and preprocess a 2,008 patient dataset with 168 different demographic, clinical, and hospital admission variables.

  2. Train several machine learning models on how to predict length of stays in the hospital, categorizing them as “Short Stay” (1–3 days), “Medium Stay” (4–7 days), and “Long Stay” (≥8 days).

  3. Identify the model that has the highest accuracy in predicting length of stay.

  4. Interpret the models with the best accuracy and validate their performance.

Methods

Dataset and Preprocessing

This study used a dataset that included 2,008 patients with 168 different demographic, clinical, and hospital admission variables (Zhang et al., 2024). These include seven demographic variables (gender, occupation, weight, height, BMI, and nationality/ethnicity) twelve hospital admission variables (team admitting the patient, team discharging them, admitted emergently or not, discharge location, etc.), ten variables on clinical presentation and vitals, eighteen variables describing medical history, and the remaining variables described laboratory values, biomarkers, and imaging. Before using the dataset to train the model, several data preprocessing steps are required. First, the dataset needed to be checked for null values because the model cannot handle the null values by itself. The dataset did have null values for the comorbidities and alcohol use variables since some patients had no comorbidities or alcohol use. This is where imputation was used to substitute missing values in those categorical columns with ‘None’ instead of getting rid of those patients, which could result in significant information loss. However, if there were features with more than 20% missing data, they were removed. Features that are not useful in a practical setting were also removed from the dataset. These features include values that are only available at the time of discharge, follow up, or results from echocardiography. Therefore, the model only predicts the LoS based on features that are expected to be available at the time or shortly after admission, such as preliminary lab data. In addition to this, categorical values need to be converted into numbers since computers do not understand text. Ordered values were converted to numerical encodings to preserve their hierarchy, such as the New York Heart Association (NYHA) heart failure classifications. Non-ordered values were processed using one-hot encoding, such as gender and occupation. The processed dataset was integrated into PyCaret for model selection

The data set used in this study was derived from a retrospective analysis conducted at Zigong Fourth People’s Hospital. The original study included patients admitted with heart failure between December 2016 and June 2019. The study complied with the principles of the Declaration of Helsinki and was approved by the ethics committee of Zigong Fourth People’s Hospital.

Model Selection and Training

Thirteen models were evaluated: LightGBM, Extra Trees Classifier, Gradient Boosting Classifier, Logistic regression, XGBoost, Random Forest Classifier, Ada Boost Classifier, Linear Discriminant Analysis, Ridge Classifier, Naive Bayes, Support Vector Machines, K-Nearest Neighbors, and Decision Tree Classifier model. PyCaret was used as a standardized way to train the machine learning models. Stratified 10-fold cross-validation was used to ensure each fold has an equal proportion of observations in the samples. The data is split up for the model to train itself on 9 folds and validate itself on the last fold, repeating this 10 times and reporting the mean performance across the folds. Performance was assessed using accuracy, Receiver Operating Characteristic Area Under the Curve (ROC AUC), precision, recall, and F1-score. In order to reduce complexity and risk of overfitting, highly collinear features were removed with a threshold of 90%. Therefore, redundant variables were removed to make it less likely the model would overfit coincidental patterns. Synthetic Minority Oversampling Technique (SMOTE) was used to address class imbalance during data training in order to improve representation of the minority classes. Once cross validation confirmed LightGBM as the top performing model, hyperparameter optimization for this model was attempted using a randomized search algorithm. The default PyCaret configuration yielded better performance across the 10-fold cross-validation compared to the tuned configuration; therefore, the default PyCaret parameters were retained, and the model was trained on the full dataset.

Software and Tools

This study was performed using Python with scikit-learn (version 1.4.2), pandas (version 1.4), Numpy (version 1.23.5), SciPy (version 1.11.4), joblib (version 1.2.0), matplotlib (version 3.5.3) and PyCaret.

Patient and Public Involvement

Patients and the public were not involved in the design, conduct, reporting or dissemination plans of our research.

Results

Among the 13 models evaluated, LightGBM (Light Gradient Boosting Machine) demonstrated the highest performance. As shown in Figure 1, the model achieved a micro-average ROC AUC of 0.78, a macro-average ROC AUC of 0.68. The higher micro-average shows that the model performed well across all classes, while having moderate performance when each class is considered equally, as shown by the lower macro-average.

Figure 1:

Figure 1:

ROC Curves for LGBM Classifier

Other high-performing models shown in Table 1 include Extra Trees Classifier, Gradient Boosting Classifier, and XGBoost. During the selection phase, all results in Table 1 were based on mean performance across 10-fold cross-validation. After LightGBM was selected, it was finalized by training on the full dataset resulting in the micro-average ROC AUC of 0.78. Collectively, these results establish a robust performance benchmark for multiclass prediction of LoS in heart failure by capturing complex clinical patterns in the dataset.

Table 1:

Model Performance Comparison

Model Accuracy AUC Recall Precision F1
LightGBM 0.6052 0.6877 0.6052 0.6066 0.5996
Extra Trees (ET) 0.6034 0.6917 0.6034 0.6115 0.5916
Gradient Boosting (GBC) 0.6021 0.6021 0.6088 0.5974
Extreme Gradient Boosting (XGBoost) 0.594 0.6838 0.594 0.6063 0.5851
Random Forest (RF) 0.5791 0.6664 0.5791 0.5748 0.5646
AdaBoost 0.5355 0.5355 0.5483 0.5372
Linear Discriminant Analysis (LDA) 0.5342 0.5342 0.5899 0.5521
Ridge Classifier 0.5187 0.5187 0.5872 0.54
Decision Tree (DT) 0.4937 0.5453 0.4937 0.5019 0.4953
Naive Bayes (NB) 0.4826 0.6422 0.4826 0.5612 0.4843
Logistic Regression (LR) 0.4414 0.4414 0.5287 0.4686
K-Nearest Neighbors (KNN) 0.4047 0.5298 0.4047 0.4807 0.4291
Support Vector Machine (SVM - Linear) 0.3487 0.3487 0.4428 0.3199

Note: AUC values were not computed for specific models as evaluation was based on discrete class predictions rather than continuous probability estimates or decision scores that are necessary for ROC AUC calculation (e.g. logistic regression, gradient boosting etc.)

Model Performance

Figure 2, 3 and 4 describes the model’s ability to correctly and incorrectly predict the three different classes. Figure 2 shows that model performed best in the short LoS category, with a 0.58 precision, 0.61 recall, and 0.595 F1-score. Figure 2 shows the different classes (Short, Medium, and Long LoS) represented as 0, 1, and 2, respectively. The report is broken down by precision (proportion of correctly predicted instances out of all of the model’s predictions), recall (proportion of how many instances of classes the model correctly found, out of how many there were in that class), and F1-score (the mean between recall and precision). Long LoS had the highest precision of 0.67, indicating that when the model predicted a long LoS, it was usually correct. However, it also had the lowest recall, 0.148, indicating it missed many of the long LoS cases leading to the lowest F1-score. Lastly, the support is how many patients were in each class for this specific report. Therefore, this shows that the dataset had a lower number of long LoS patients (27), which may have caused bias in the results due to class imbalance. In Figure 3, the model predicted short LoS (class 0) correctly with 128 patients, but incorrectly predicted short LoS in the medium LoS category for 80 patients and one patient in the long LoS category. The medium LoS category was correctly predicted with 84 patients, while it incorrectly predicted the short LoS category for 81 patients and 1 patient for long LoS. For the long LoS, 4 patients were correctly predicted, while 12 patients with short LoS and 11 patients with medium LoS were incorrectly predicted. Overall, the model’s performance is best with identifying short LoS patients, but it can have difficulty differentiating between short and medium LoS. It also shows that the model struggled with correctly predicting the long LoS category.

Figure 2:

Figure 2:

LGBM Classification Report

Figure 3:

Figure 3:

LGBM Confusion Matrix

Figure 4:

Figure 4:

LGBM Precision-Recall Curve

The micro-averaged precision-recall curve, Figure 4, shows that the model had true positive predictions on 58% of all positive classifications. As the model declines in precision, the recall increases. This exhibits the trade off of decreasing false negatives, by increasing recall, while avoiding false positives. Figure 5 is a feature importance plot that displays the variables that most influenced the model. Feature importance helps compare what most influenced the model to make its prediction with what a clinician would find useful in heart failure treatment, thus showing if the model’s predictions makes clinical sense. The plot listed brain natriuretic peptide (BNP), creatine kinase, and high density lipoprotein cholesterol as the top 3 variables that impacted the model; therefore, it was able to correlate relevant biomarkers with heart failure outcomes and LoS.

Figure 5:

Figure 5:

Feature Importance Plot

Discussion

This study highlights the potential of machine learning to predict LoS in heart failure patients, with LightGBM outperforming other models. The achieved ROC AUC scores (micro: 0.78; macro: 0.68) suggest moderate discriminatory ability in complex multiclass classification. The observed performance levels may be attributed to the clinical variability of patient outcomes in heart failure and the constraints of the 2,008 patient dataset. Compared to Jain et al. (2024), who reported 78% accuracy for general admissions, our focus on heart failure patients provides greater specificity but similar predictive challenges.

Integration of machine learning models into clinical decision support systems could enhance resource allocation and patient care. For example, real-time LoS predictions could guide staffing, bed assignments, and early interventions. Using these predictions could aid in triage of the longer LoS patients to provide proactive care coordination and efficient use of resources. However, ethical considerations, such as patient data privacy and informed consent, must be addressed6. Continuous model retraining with diverse datasets is essential to mitigate bias and improve generalizability.

Limitations

Dataset Size:

The 2,008-patient dataset may not fully represent the diversity of heart failure outcomes

Model Scope:

While 13 models were tested, other algorithms (e.g., neural networks) could yield better performance.

Generalizability:

The dataset, sourced from a single hospital in China, may not reflect global patient populations.

Future Directions

To enhance the predictive accuracy and clinical utility of machine learning models for LoS prediction in heart failure patients, future research should prioritize the use of larger and more diverse datasets. Expanding the sample size and incorporating data from multiple healthcare institutions across different regions would improve model generalizability and capture a broader range of patient outcomes. This approach could mitigate potential biases inherent in the current dataset, which is limited to a single hospital in China.

Additionally, exploring advanced machine learning techniques, such as deep learning models (e.g., neural networks), could yield superior performance compared to the 13 models tested in this study. These models may better capture complex, non-linear relationships within high-dimensional clinical data. Investigating ensemble methods that combine multiple algorithms could further enhance predictive accuracy.

Real-world validation of the models in clinical settings is critical to assess their practical utility. Pilot studies integrating the top-performing model (e.g., LightGBM) into hospital electronic health record systems could provide insights into its impact on resource allocation, patient care, and clinical decision-making. Such studies should also evaluate user acceptance and workflow integration among healthcare providers.

Future work should focus on developing frameworks for patient data privacy, ensuring informed consent, and maintaining transparency regarding the use of artificial intelligence in clinical decision-making. Collaborative efforts with policymakers and healthcare stakeholders will be necessary to establish guidelines that balance innovation with patient autonomy and data security.

Conclusions

This study demonstrates the feasibility of using machine learning, particularly LightGBM, to predict LoS in heart failure patients. While the study establishes a strong baseline performance with a micro-average ROC AUC of 0.78, it underscores the complexity of predicting LoS in heart failure patients. The results demonstrated the ability for machine learning to identify top clinical biomarkers to provide a foundation for future decision support systems. Integration into clinical workflows could optimize resource allocation, improve patient outcomes, and reduce costs. Future work should focus on leveraging larger, diverse datasets and additional models to maximize the impact of machine learning in healthcare.

Highlights.

  • ML models categorize LoS for heart failure patients

  • BNP and creatine kinase are key biomarkers for predicting LoS

  • LightGBM model achieved superior performance with a micro-average ROC AUC of 0.78

  • Early LoS prediction supports clinical resource planning and management

Acknowledgements

The authors would like to acknowledge Dr. Patrick Tighe and Paul Nickerson for aiding in the original project concept and guidance with the early study objectives.

Funding

This work was supported by the National Institutes of Health (NIH) T35 grant, number T35HL007489-41. The funder had no role in the study design, data collection and analysis, decision to publish, or preparation of the manuscript.

Footnotes

Publisher's Disclaimer: This is a PDF file of an unedited manuscript that has been accepted for publication. As a service to our customers we are providing this early version of the manuscript. The manuscript will undergo copyediting, typesetting, and review of the resulting proof before it is published in its final form. Please note that during the production process errors may be discovered which could affect the content, and all legal disclaimers that apply to the journal pertain.

Data Availability

Data from PhysioNet can be requested through https://physionet.org/content/heart-failure-zigong/1.3/

References

  • 1.Ambrosy AP, Fonarow GC, Butler J, et al. The global health and economic burden of hospitalizations for heart failure: lessons learned from hospitalized heart failure registries. J Am Coll Cardiol. 2014;63(12):1123–1133. doi: 10.1016/j.jacc.2013.11.053 [DOI] [PubMed] [Google Scholar]
  • 2.Levy-Lambert D, Ramly EP, Kantar RS, Alfonso AR, Levine JP. Congestive Heart Failure Predicts Major Complications and Increased Length of Stay in Lower Extremity Pedicled Flap Reconstruction. Plast Reconstr Surg. 2020;146(6):790e–795e. doi: 10.1097/PRS.0000000000007363. [DOI] [PubMed] [Google Scholar]
  • 3.Laupland KB, Kirkpatrick AW, Kortbeek JB, & Zuege DJ Long-term mortality outcome associated with prolonged admission to the ICU. Chest, 2006;129(4), 954–959. [DOI] [PubMed] [Google Scholar]
  • 4.Jain R, Singh M, Rao AR, Garg R. Predicting hospital length of stay using machine learning on a large open health dataset. BMC Health Serv Res. 2024;24(1):860. doi: 10.1186/s12913-024-11238-y. [DOI] [PMC free article] [PubMed] [Google Scholar]
  • 5.Zhang Z, Cao L, Zhao Y, Xu Z, Chen R, Lv L, et al. Hospitalized patients with heart failure: integrating electronic healthcare records and external outcome data [Internet]. PhysioNet; [cited 2024 Nov 14]. Available from: https://physionet.org/content/heart-failure-zigong/1.3/ [DOI] [PMC free article] [PubMed] [Google Scholar]
  • 6.Jiang F, Jiang Y, Zhi H, et al. Artificial intelligence in healthcare: past, present and future. Stroke Vasc Neurol. 2017;2(4):230–243. Published 2017 Jun 21. doi: 10.1136/svn-2017-000101 [DOI] [PMC free article] [PubMed] [Google Scholar]

Associated Data

This section collects any data citations, data availability statements, or supplementary materials included in this article.

Data Availability Statement

Data from PhysioNet can be requested through https://physionet.org/content/heart-failure-zigong/1.3/

RESOURCES