Optimized Machine Learning Algorithms for the Classification and Diagnosis of Sleep Disorders
1WeAreGenius Research Institute, Lagos State, Nigeria
*Email address: atimothy.dev@gmail.comABSTRACT
Sleep disorders, including insomnia and obstructive sleep apnea, affect millions of individuals worldwide but are frequently undetected due to the high cost, limited availability, and complexity of conventional diagnostic tools such as polysomnography. This study presents an interpretable machine learning framework for multi-class sleep disorder classification that utilizes routine clinical and lifestyle data, offering an accessible, data-driven approach to screening. The dataset was preprocessed and balanced using SMOTE-ENN, and three ensemble models, Random Forest, XGBoost, and LightGBM, were systematically trained and optimized. Among these, LightGBM demonstrated superior performance, achieving a test accuracy, precision, and recall of 0.9835 above with a minimal train–test accuracy gap of 0.0105, indicating strong generalization and limited overfitting. Feature importance analysis identified systolic blood pressure and BMI category as the most influential predictors, consistent with established clinical knowledge. To enable practical application, the optimized model was deployed through a lightweight Flask-based web application, providing real-time, point-of-care predictions without requiring specialized equipment. The findings demonstrate that well-tuned tree-based ensemble models can deliver accurate, interpretable, and clinically actionable tools for the early detection of sleep disorders, supporting timely intervention and expanding access to sleep health management across diverse healthcare settings.
Article notes
Competing Interest Statement
The authors have declared no competing interest.
Funding Statement
This study did not receive any funding
1INTRODUCTION
Sleep is not merely a state of rest, but an active and restorative process essential for cognitive renewal, emotional regulation, and physical health (Nayana, 2025). Disorders such as insomnia and sleep apnea remain among the most critical yet frequently underdiagnosed conditions, with profound implications that include impaired daily performance, increased cardiovascular and metabolic risk, and decreased quality of life (Niu et al., 2025). Recent surveys indicate that more than 40% of adults report dissatisfaction with their sleep, with stress, occupational demands, and lifestyle habits emerging as dominant contributing factors (Triches et al., 2025).
Historically, clinical diagnosis depended on polysomnography (PSG), a laboratory-based examination that combines electroencephalographic, respiratory, cardiac, and movement signals (Kathrine et al., 2025). Despite its diagnostic accuracy, PSG is expensive, time-consuming, and unsuitable for population-level screening. Furthermore, its interpretation is subject to inter-rater variability, which limits repeatability. The growing availability of multimodal health data, including wearable sensors and electronic health records, underscores the need for objective, scalable, and automated diagnostic frameworks that utilize accessible biomarkers such as heart rate variability, blood pressure, physical activity, perceived stress, body composition, and demographic covariates (Elfouly and Alouani, 2025).
Machine learning (ML) offers a transformative opportunity in this field. By identifying nonlinear interactions and hidden patterns in various data streams, ML models can classify individuals into clinically relevant subgroups, allowing precision therapies (Tiwari et al., 2025). However, much of the current literature remains limited: many studies focus narrowly on single-modality signals (e.g., EEG or actigraphy) (Gramkow et al., 2025; Krauss et al., 2025; Bjarke Mikkelsen et al., 2025), overlooking the synergistic effects of psychosocial, physiological, and behavioral factors, such as how chronic stress influences the relationship between BMI and sleep problems. In addition, there is often little comparative benchmarking among algorithms, and limited efforts are made to turn successful models into practical clinical tools.
To close these gaps, this study proposes a robust end-to-end machine learning system to classify three states of sleep: no disorder, insomnia, and sleep apnea. Physiological, demographic, lifestyle, and mental health indicators are integrated through systematic preprocessing, feature engineering, and model optimization. Three advanced ensemble methods, Random Forest, XGBoost, and LightGBM, are extensively tested under similar experimental conditions to ensure a fair comparison. Our approach improves the precision of diagnosis, reduces the reliance on subjective clinical interpretation, and promotes early and accessible intervention.
The key contributions of this work are threefold:
- A comprehensive review of existing research on machine learning for the classification of sleep disorders, focusing on key challenges and limitations.
- A comparative evaluation of traditional machine learning models and deep learning architectures for an accurate classification of sleep disorders.
- A prototype web application that integrates the developed models to offer accessible and automated diagnostic support for clinicians and patients.
The remainder of this paper is structured as follows. Section 2 reviews previous studies; Section 3 describes the data set and methodology; Section 4 presents and discusses the experimental findings; and Section 5 provides concluding insights and recommendations.
3METHODOLOGY
This section outlines the end-to-end machine learning process used to classify sleep disorders. The framework incorporates data preprocessing, feature engineering, class balance management, model selection, rigorous performance evaluation, and statistical validation. To ensure reproducibility, promote computational efficiency, and uphold methodological rigor, all experiments were implemented in Python with robust scientific libraries, including NumPy, pandas, scikit-learn, XGBoost, LightGBM, SciPy, and statsmodels. Figure 1 presents an overview of the whole procedure.
3.1Dataset Description
The study uses the publicly accessible Sleep Health and Lifestyle dataset from Kaggle (Tharmalingam, 2024), which includes 374 anonymized records across 13 categories. The dataset consists of both categorical and numerical variables and covers individuals with a wide range of demographic and lifestyle characteristics. The target variable, Sleep Disorder, is a multiclass label that includes ‘None’, ‘Insomnia’, and ‘Sleep Apnea’.
The Person ID variable, which just serves as a unique identifier, was removed from the modeling process because it has no predictive value. Table 2 provides a complete summary of the dataset. Panel A shows categorical distributions, while Panel B offers descriptive statistics (mean, standard deviation, and range) for numerical features.
3.2Data Preprocessing
Initial data validation confirmed the absence of missing values and duplicate records, thereby maintaining dataset integrity. The initial Blood Pressure field, saved as a string (e.g., “120/80”), was parsed into two numerical features: SystolicBP and DiastolicBP. All categorical predictors were transformed into numerical representations using label encoding. The target variable Sleep Disorder was encoded as integers: Insomnia → 0, None → 1, Sleep Apnea → 2.
To reduce scale inequalities across features and guarantee fair influence during model improvement, all input variables were normalized using z-score normalization: µ and σ represent the feature-specific mean and standard deviation determined on the training set.
3.3Class Balancing and Data Partitioning
The original dataset exhibited considerable class imbalance (Table 3). To address this, the hybrid SMOTEENN technique was applied, combining the Synthetic Minority Oversampling Technique (SMOTE) with Edited Nearest Neighbors (ENN) to generate synthetic minority samples while removing noisy or borderline instances from the majority class. This approach enhanced class separability and reduced the risk of overfitting.
Following resampling, the dataset was expanded to N = 545 and partitioned into training, validation, and test subsets using stratified random sampling to preserve the proportional representation of each sleep disorder class across all splits. The final allocation followed a 60:20:20 ratio, resulting in 327 training, 109 validation, and 109 test samples. Stratification ensured rigorous evaluation and improved generalizability, particularly for minority classes.
3.4Model Development
The study assessed three ensemble-based classifiers known for their effective performance on structured tabular data: Light Gradient Boosting Machine (LightGBM), a histogram-based boosting algorithm optimized for speed and memory efficiency; Extreme Gradient Boosting (XGBoost), a regularized gradient boosting framework; and Random Forest, an averaging ensemble of decorrelated decision trees. Using cross-validation on the validation set and employing empirical best practices, hyperparameters were optimized through repeated testing. Table 4 displays the final model settings, which were created to balance regularization, computational efficiency, and prediction accuracy.
3.5Evaluation Metrics
This study evaluates machine learning algorithms for categorizing sleep problems. To address class imbalance, four measures were employed: accuracy, precision, recall, and weighted F1-score, along with train-test and validation-test gaps to assess generalization and overfitting.
- Accuracy: Measures the overall correctness of predictions.
- Precision: The proportion of correctly identified positive cases among all predicted positives.
- Recall (Sensitivity): The proportion of actual positive cases correctly identified.
- Weighted F1-score: The harmonic mean of precision and recall, averaged across classes using support-based weighting. as:
To evaluate generalization and detect potential overfitting, the train–test accuracy gap was computed Similarly, the validation–test accuracy gap was used to assess stability across unseen data: In addition to these numerical measures, confusion matrices and classification reports were generated to capture per-class misclassification patterns. Collectively, these metrics provided a robust and comprehensive evaluation of classification performance and model generalization.
3.6Model Deployment
The best-performing model, selected based on test accuracy, weighted F1-score, and the smallest generalization gap, was deployed as a lightweight web application utilizing the Flask framework. Flask was selected due to its ease of use, modularity, and seamless compatibility with machine learning libraries based on Python.
The trained model was serialized using joblib to retain scikit-learn compatibility and facilitate quick loading during inference. To guarantee consistency with training settings, input preprocessing (label encoding, standardization) was duplicated exactly in the deployment environment.
Clinicians or researchers can use a browser to enter patient-specific data (such as demographics, vital signs, and lifestyle measures) into the resultant interface. The backend returns a real-time prediction of the sleep disorder category after preprocessing the inputs and running the model. Adoption in clinical or public health settings is facilitated by this design, which eliminates the requirement for technical competence.
This deployment demonstrates the translational potential of machine learning in sleep medicine by integrating experimental modeling and real-world implementation, facilitating data-driven clinical decision-making, enabling early screening, and reducing diagnostic delays.
4RESULTS AND DISCUSSION
The empirical findings of the experimental study are presented, with an emphasis on the model’s performance, interpretability, and therapeutic applicability. Comparison research was conducted to determine the most effective practical strategy for diagnosing sleep problems, comparing three ensemble classifiers: Random Forest (RF), XGBoost, and LightGBM, based on several assessment criteria. The commentary situates the findings within the larger body of research, balancing their strengths and limitations in terms of practical therapeutic applicability. The efficiency of models in helping to identify and treat sleep-related problems early is further investigated by combining pathophysiological knowledge with empirical data.
4.1Feature Importance and Clinical Interpretability
Normalized importance scores were computed across the three ensemble models to evaluate the predictive contribution of individual features. Normalization ensures comparability between algorithms with distinct internal scoring methods. Results are presented in Table 5.
Among all predictors, systolic blood pressure (mean score = 0.2096) and BMI category (0.1672) emerged as the most critical features. LightGBM emphasized systolic blood pressure (0.3008), while XGBoost prioritized BMI (0.2511). Diastolic blood pressure, occupation, and physical activity showed high contributions, whereas sleep duration, daily steps, and heart rate had moderate effects. Demographic and subjective factors, including age, gender, sleep quality, and stress level, were consistently of minimal predictive value. Inter-model variability was greatest for BMI (std = 0.0783) and systolic blood pressure (0.0651), reflecting algorithm-specific weighting tendencies.
Consistency of feature ranking across models is reported in Table 6. Random Forest and XGBoost demonstrated moderate concordance (Kendall’s τ = 0.788, Spearman’s ρ = 0.841), with slightly weaker agreement between Random Forest and LightGBM (0.700, 0.772). XGBoost and LightGBM achieved the highest correlation (0.817, 0.863). This indicates that while all three models broadly agreed on the relative importance of key predictors, algorithm-specific weighting patterns introduced some variability.
Clinically, the prominence of systolic blood pressure and BMI aligns with established associations between hypertension, obesity, and obstructive sleep apnea (OSA). Elevated blood pressure reflects recurrent nocturnal hypoxia and sympathetic activation in OSA, while excess adiposity promotes airway collapse during sleep. These findings highlight the capacity of non-invasive, easily measurable features to serve as actionable biomarkers for early screening in primary care.
4.2Comparative Evaluation of Ensemble Classifiers
The predictive performance and generalization capability of the three ensemble models were rigorously evaluated across training, validation, and held-out test sets. Table 7 summarizes the macro-averaged accuracy and F1 scores for each data split.
LightGBM achieved the highest test accuracy (98.35%) and F1 score (98.35%), demonstrating superior generalization. While XGBoost achieved perfect training performance (100.00% accuracy and F1), its substantially lower test scores (94.51% accuracy) and significant performance drop indicate substantial overfitting. Random Forest showed more modest but stable performance across splits.
To further assess generalization and class-wise reliability, Table 8 reports test precision, recall, and the train–test accuracy gap.
LightGBM demonstrated the best balance between performance and stability, achieving the highest test precision (98.39%) and recall (98.37%) with the smallest train–test accuracy gap (1.10%). In comparison, XGBoost had the largest gap (5.49%), indicating its vulnerability to overfitting despite excellent in-sample results. Random Forest demonstrated moderate generalization (gap: 2.37%) with solid but lower test metrics. These findings suggest that LightGBM is the most dependable and clinically practical model, as it combines high diagnostic accuracy with consistency on unseen data.
4.3Confusion Matrix Analysis
Aggregate measures provide overall accuracy, whereas confusion matrices reveal class-specific behavior. Table 9 displays the test-set confusion matrices for all three models. Correct classifications are highlighted in green, misclassifications in red, and zero entries in light gray. Rows represent true labels, and columns represent predicted labels.
Interpretation
Random Forest correctly classified most cases but misidentified three insomnia patients and three sleep apnea patients as “Unknown,” along with two “Unknown” cases labeled as insomnia. XGBoost showed a similar pattern but with slightly higher error, misclassifying four “Unknown” cases as insomnia. In contrast, LightGBM produced the most accurate predictions, with only two insomnia cases and one sleep apnea case mislabeled as “Unknown,” achieving perfect separation for the “Unknown” class otherwise.
Across all models, no instances of sleep apnea were falsely categorized as insomnia, ensuring high clinical safety by avoiding false negatives for a life-threatening disorder. The consistent misclassification of insomnia as “Unknown” indicates overlapping symptom profiles that the current feature set cannot fully resolve. Incorporating physiological measures such as oxygen saturation or respiratory effort could further enhance discriminatory power.
Overall, LightGBM demonstrated the strongest diagnostic reliability, minimizing false positives while maintaining excellent recall for high-risk conditions, making it the most clinically dependable candidate for deployment.
4.4Comparison with Existing Literature
To better understand the findings, the Random Forest, XGBoost, and LightGBM models are compared to previous work on sleep disorder categorization (Table 10). Rahman et al. (2025) reported 97.33% accuracy using gradient boosting ensembles, while Alshammari (2024) obtained 92.92% with artificial neural networks (ANN) and standard machine learning techniques.
While both this study and that of Rahman et al. (2025) employed class balancing through resampling approaches, such as SMOTE, the current work advances the field in two crucial areas. First, hyperparameters were tweaked individually for each model rather than universally, ensuring that optimization matched the algorithms’ inductive features. Second, clinical dependability was highlighted, and the LightGBM model achieved a near-perfect recall for sleep apnea, thus reducing the likelihood of false negatives in a high-risk medical setting.
In this research, the LightGBM model outperformed the model reported by Rahman et al. (2025), reaching 98.39% precision and 98.37% recall, with a train-test gap of just 1.10%, suggesting outstanding generalization and stability. Random Forest and XGBoost also performed well, confirming the efficacy of tree-based ensembles for structured medical datasets. In comparison, the ANN-based technique of Alshammari (2024) yielded significantly lower accuracy, highlighting the ongoing benefits of ensemble approaches when interpretability, robustness, and clinical safety are prioritized.
These findings demonstrate that even with similar preprocessing procedures, careful model selection and rigorous tuning are necessary to achieve cutting-edge clinically significant results.
4.5Deployment of the Optimal Model
Following a thorough study, LightGBM was chosen as the best model for deployment due to its excellent test performance, small training-test gap, and strong clinical safety profile. The final trained model was serialized using Python’s joblib module, allowing efficient inference without retraining.
A lightweight REST API was developed using the Flask framework to handle prediction queries. The front-end, designed to provide physicians with an intuitive and responsive user experience, was implemented with standard web technologies, including HTML, CSS, and JavaScript. Incoming patient data are preprocessed prior to prediction to align with the model’s training schema, and results are returned immediately to support rapid clinical decision making.
Figure 2 illustrates the deployed interface, showing how clinicians can input key patient parameters and receive immediate, interpretable predictions.
5CONCLUSION
This paper presents an efficient machine learning framework for multi-class sleep disorder classification using only the original attributes of the Sleep Health and Lifestyle Dataset, without reliance on engineered features. LightGBM emerged as the best-performing model, achieving a test precision of 98.39%, recall of 98.37%, and a minimal train–test gap of 1.10% through SMOTEENN-based resampling and systematic hyperparameter tuning. In particular, the model maintained high recall for sleep apnea, thus minimizing false negatives, a critical requirement for safe clinical screening.
Compared with other ensemble approaches in this study, LightGBM consistently outperformed both Random Forest and XGBoost, delivering higher test precision, stronger generalization, and better resilience to overfitting. Compared with previous work, including Rahman et al. (2025) who reported slightly lower accuracy with gradient boosting, the LightGBM model in this work demonstrates superior performance, improved generalization, and greater robustness against overfitting.
The framework has been operationalized as a Flask-based REST API, enabling real-time predictions from structured patient inputs and offering scalable integration into clinics, telemedicine platforms, and low-resource environments. Future directions include large-scale external validation, incorporation of physiological signals from wearable devices, and the adoption of explainable AI techniques to enhance clinician confidence and model transparency.
Overall, the findings confirm that carefully optimized gradient boost ensembles, particularly LightGBM, can deliver accurate, safe, and deployable solutions for the classification of sleep disorders, supporting early diagnosis and improved access to sleep health care.
Data Availability
full available