The Data Science & Machine Learning Story
Step-by-step walkthrough of hypothesis formulation, EDA, rigorous preprocessing, classifier benchmarking, and SHAP explainability.
Business Problem & Objective Formulation
Customer churn in telco and SaaS averages 20-30% annually. The goal is to build a robust predictive classifier with Explainable AI (SHAP) to proactively detect at-risk subscribers before they cancel.
df = pd.read_csv('data/customer_churn.csv')
print(f"Dataset: {df.shape[0]} rows, {df.shape[1]} features")- Analyzed 5,000 telco subscription accounts with 14 predictor attributes.
- Zero missing values in primary numerical fields (tenure, MonthlyCharges).
- Target variable Churn is binary ('Yes' / 'No').
Class Imbalance & Evaluation Metric Selection
Evaluating class distribution reveals ~25.8% churn rate (moderate imbalance). Accuracy alone is dangerously deceptive: a trivial naive baseline achieves 74.2% accuracy while missing 100% of churners.
churn_rate = (df['Churn'] == 'Yes').mean() * 100 # Decision: Prioritize ROC-AUC, Precision, Recall, and F1 over Accuracy
- ROC-AUC and Precision-Recall Curves selected as primary selection metrics.
- Established baseline majority class accuracy: 74.2%.
Exploratory Data Analysis: Key Risk Anchors
Bivariate analysis uncovers that Contract agreement length and account tenure are the two most decisive churn predictors in the entire feature space.
sns.countplot(data=df, x='Contract', hue='Churn') sns.boxplot(data=df, x='Churn', y='tenure')
- Month-to-month contracts account for over 78% of all churn events.
- Median tenure of churners is ~10 months, compared to ~38 months for retained customers.
- First 12 months is the critical flight risk window.
Leakage-Free Preprocessing Pipeline
To guarantee zero data leakage between splits, transformers are fitted strictly on the training set (70%) and subsequently applied to validation (15%) and holdout test (15%) partitions.
preprocessor = ColumnTransformer([
('num', StandardScaler(), ['tenure', 'MonthlyCharges', 'TotalCharges']),
('cat', OneHotEncoder(drop='first'), categorical_cols)
])- Stratified 70/15/15 split preserves target distribution across sets.
- Numeric features standardized to zero mean and unit variance for gradient convergence.
Model Benchmarking: Linear vs Trees vs Boosting
Benchmarked Logistic Regression, Random Forest, and Gradient Boosting on the unseen holdout test set.
models = {
'Logistic Regression': LogisticRegression(max_iter=1000),
'Random Forest': RandomForestClassifier(n_estimators=100),
'Gradient Boosting': GradientBoostingClassifier(n_estimators=100)
}- Logistic Regression achieved best ROC-AUC (0.8257) and highest recall on churners (52.8%).
- Gradient Boosting achieved ROC-AUC (0.8111) with superior non-linear feature interaction capture.
- Generalization gap between validation and test was <0.01 across all models.
Explainable AI (SHAP) & Global Feature Importance
Deployed SHAP (SHapley Additive exPlanations) to demystify black-box predictions and attribute individual risk contributions for each customer.
explainer = shap.TreeExplainer(gb_model) shap_values = explainer.shap_values(X_test_proc)
- Top Positive Drivers (Increases Churn): Month-to-month contract, High monthly bill, Fiber optic without tech support.
- Top Negative Drivers (Protects Retention): 2-Year contract, Long tenure, Tech Support active.