project — Classifying short windows of heart-rate data into fitness zones using a stacking ensemble: Random Forest and SVM as base models, MLP as meta-model.
This repository demonstrates an end-to-end pipeline that converts wearable heart-rate data into reliable per-window heart-rate zone predictions using a stacking ensemble. The approach combines two complementary base learners (Random Forest and SVM) whose probabilistic outputs are used to train a compact MLP meta-model — all tuned with randomized hyperparameter search and validated with stratified folds. The result is a lightweight, accurate classifier suitable for on-device or near-device deployment in sports and health applications.
- File:
dataset_training_withclass_edited.csv(noted path in notebook:/mnt/data/dataset_training_withclass_edited.csv) - Rows: 942
- Columns: 4
- Column names:
Average BPM,Time (hh:mm:ss),Date,fatigue - Detected target column:
fatigue. - Quick stats (Average BPM): min 0, 25% 126, median 147, 75% 161, max 198.
- Data loading & preprocessing
- Load the CSV, parse timestamps, clean missing/outlier BPM values, and create short fixed-length windows (features computed per-window).
- Feature engineering (per window)
- Time-domain stats such as
mean,min,max, (and optionallystd,IQR, delta/slope features).
- Time-domain stats such as
- Base model hyperparameter tuning
- Random Forest:
RandomizedSearchCVwithparam_distributions=rf_param_grid,n_iter=20,scoring='f1_macro',cv=3. Best RF hyperparameters found:{'n_estimators': 100, 'min_samples_split': 2, 'min_samples_leaf': 2, 'max_depth': None, 'bootstrap': False}. - SVM (probability=True):
RandomizedSearchCVwithparam_distributions=svm_param_grid,n_iter=10,scoring='f1_macro',cv=3. Best SVM hyperparameters found:{'kernel': 'linear', 'gamma': 'scale', 'C': 1}.
- Random Forest:
- Stacking strategy (manual stacking implemented in notebook)
- Use
StratifiedKFold(n_splits=5)to generate out-of-foldpredict_probafor base models on training data and to generate base-modelpredict_probaon test data. - Construct stacked feature matrices
S_trainandS_test(shape = n_samples × (n_classes × n_base_models)), then scale and feed into the meta-model.
- Use
- Meta-model
sklearn.neural_network.MLPClassifierused as meta learner (compact MLP). The MLP is trained on stacked probabilistic features.
- Evaluation
- Final evaluation performed on hold-out test set using accuracy, precision, recall, and macro F1; confusion matrix visualized.
- RandomizedSearchCV (Random Forest):
n_iter=20,cv=3, scoring=f1_macro. Best params:{'n_estimators': 100, 'min_samples_split': 2, 'min_samples_leaf': 2, 'max_depth': None, 'bootstrap': False}. - RandomizedSearchCV (SVM):
n_iter=10,cv=3, scoring=f1_macro. Best params:{'kernel': 'linear', 'gamma': 'scale', 'C': 1}. - Stacking folds:
n_folds = 5(StratifiedKFold for out-of-fold meta features).
evaluation on testing data (Meta Model MLP)
- Accuracy:
0.9841269841269841(~98.41%) - Precision (macro / averaged):
0.986733958183129(~98.67%) - Recall (macro / averaged):
0.9835796387520526(~98.36%) - F1-Score (macro):
0.9848284635394545(~98.48%)
Notes on results:
- Confusion matrix and per-class metrics are available in the notebook and provide insight into which zones are occasionally confused (typically adjacent-intensity zones).
- These high-level numbers come directly from the notebook's printed evaluation output — include them in your portfolio but also consider reporting per-class precision/recall when presenting to hiring teams.
- Place
dataset_training_withclass_edited.csvin the working directory (or update the notebook path). The notebook references/mnt/data/dataset_training_withclass_edited.csv. - Create and activate a virtual environment and install required packages:
python -m venv .venv
source .venv/bin/activate # macOS/Linux
.\.venv\Scripts\activate # Windows
pip install -r requirements.txt-
Run the notebook (
jupyter laborjupyter notebook) and execute cells in order. The notebook performs the following steps automatically:- hyperparameter searches (RF: 20 candidates, SVM: 10 candidates — takes some time but is parallelized
n_jobs=-1), - stacking generation (5-fold StratifiedKFold),
- meta-model training and final evaluation.
- hyperparameter searches (RF: 20 candidates, SVM: 10 candidates — takes some time but is parallelized
-
Export the notebook as IPYNB (
File → Download as → IPYNB) and attach the confusion matrix & sample prediction plots in your application materials.
Muhammad 'Aamir Nashrullah — Machine Learning & Embedded Systems Engineer
- Linkedin:
www.linkedin.com/in/muhammad-aamir-nashrullah-bb39a7229 - Email:
arul.amir20@gmail.com - GitHub:
https://github.com/Arulamir
EPL-2.0 License — see LICENSE for details.