This repository contains the complete implementation of the Week 5 Assignment: Tree-Based Models & Ensembles for the Fusemachines AI Fellowship. The project focuses on predicting customer churn for a telecommunications firm using machine learning classification models, as well as predicting customer tenure using regression architectures.
WK5/
├── W5_Tree-Based Models & Ensembles_Assignment.ipynb # Fully implemented & executed Jupyter notebook
├── W5_Project_Guide.md # Official assignment guidelines & requirements
├── telco_churn_pipeline_v1.joblib # Serialized production pipeline (preprocessors + SMOTE + tuned classifier)
└── README.md # This project documentation
- Gini Impurity & Shannon Entropy: Custom mathematical functions implemented from scratch to calculate split criteria.
- Information Gain: Recursive split quality calculation based on parent and child node distributions.
- Bootstrap Sampling: Reimplemented bagging mechanics (
bootstrap_sample) from scratch to build custom ensemble intuition.
- The Leakage Trap: Applying oversampling (SMOTE) on the entire dataset prior to cross-validation leaks information from validation folds into training folds, yielding over-optimistic evaluation metrics.
- The Solution: Leveraged
imblearn.pipeline.Pipeline(ImbPipeline) instead ofsklearn.pipeline.Pipeline. This encapsulates SMOTE inside the cross-validation loops, ensuring oversampling is only performed on training folds.
- Built and evaluated:
- Naïve Baseline Decision Tree
- Random Forest Classifier
- Regularized Gradient Boosted Trees (XGBoost)
- Optimized classification metrics (
AUROC,F1-Score,Precision, andRecall). - Conducted hyperparameter tuning for XGBoost using systematically managed cross-validation grids.
-
Tenure Prediction: Trained a baseline
DecisionTreeRegressorand a regularizedXGBRegressorto predict customertenure. -
Leakage Elimination: Excluded
TotalChargesandtenure(target) from the regression feature matrix sinceTotalChargesdynamically correlates withMonthlyCharges * tenure. -
Learning Curves: Computed training and validation curves via
learning_curvewith cross-validation to analyze overfitting tendencies. An unconstrained decision tree demonstrated a Train RMSE$\approx 0$ , highlighting extreme high variance, while the regularized XGBoost model showed superior generalization behavior. - Extrapolation Check: Asserted that tree-based model outputs are strictly bounded by training-range maximums (tree structures cannot extrapolate trends outside training leaf values).
- Analyzed the model using SHAP (SHapley Additive exPlanations) to explain individual and global predictions.
- Generated global summary plots highlighting how contract types (
Month-to-month), internet services (Fiber optic), and tenure lengths govern churn probabilities. - Rendered local force and waterfall plots explaining specific customer predictions.
Make sure you have a Python environment configured with the required dependencies:
pip install numpy pandas scikit-learn imbalanced-learn xgboost shap joblib matplotlib seaborn jupyterTo open the notebook locally and inspect the completed workflows:
jupyter notebook W5_Tree-Based Models_&_Ensembles_Assignment.ipynbThe final model is exported to telco_churn_pipeline_v1.joblib. This artifact packages the entire pipeline end-to-end:
ColumnTransformer(Imputers, scaling, and one-hot encoding).- SMOTE (Oversampler).
- Optimized XGBoost Classifier.
To load and make predictions in production:
import joblib
# Load pipeline
pipeline = joblib.load("telco_churn_pipeline_v1.joblib")
# Predict on new customer data
predictions = pipeline.predict(new_customer_dataframe)