This class brings together every single skill you have mastered across our 50-class curriculum into a unified, production-grade Capstone Project: building an Enterprise Customer Churn & Revenue Retention Engine from scratch.
ColumnTransformer applying StandardScaler to numerics and OneHotEncoder to categories.# train.py - Master Capstone Model Pipeline
import joblib
import numpy as np
import pandas as pd
from sklearn.compose import ColumnTransformer
from sklearn.model_selection import StratifiedKFold, cross_val_score, train_test_split
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import OneHotEncoder, StandardScaler
from xgboost import XGBClassifier
print("Step 1: Ingesting dataset...")
np.random.seed(42)
n_records = 5000
df = pd.DataFrame({
'tenure_months': np.random.randint(1, 72, n_records),
'monthly_charges': np.random.uniform(20.0, 140.0, n_records),
'support_tickets': np.random.poisson(lam=2, size=n_records),
'contract_type': np.random.choice(['Month-to-Month', 'One-Year', 'Two-Year'], n_records, p=[0.55, 0.25, 0.20]),
'paperless_billing': np.random.choice(['Yes', 'No'], n_records),
'payment_method': np.random.choice(['CreditCard', 'BankTransfer', 'ElectronicCheck'], n_records)
})
# Churn logic: High charges + high tickets + month-to-month contract = churn
risk_score = (df['monthly_charges'] * 0.02) + (df['support_tickets'] * 0.4) - (df['tenure_months'] * 0.04)
risk_score += np.where(df['contract_type'] == 'Month-to-Month', 0.8, -0.6)
df['churned'] = (1 / (1 + np.exp(-risk_score)) > 0.50).astype(int)
X = df.drop(columns=['churned'])
y = df['churned']
print("Step 2: Building ColumnTransformer...")
num_cols = ['tenure_months', 'monthly_charges', 'support_tickets']
cat_cols = ['contract_type', 'paperless_billing', 'payment_method']
preprocessor = ColumnTransformer(transformers=[
('num', StandardScaler(), num_cols),
('cat', OneHotEncoder(drop='first', sparse_output=False), cat_cols)
])
print("Step 3: Assembling Pipeline with XGBoost...")
model_pipeline = Pipeline(steps=[
('preprocessor', preprocessor),
('classifier', XGBClassifier(n_estimators=120, max_depth=4, learning_rate=0.08, eval_metric='logloss', random_state=42))
])
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, stratify=y, random_state=42)
# Stratified 5-Fold Cross-Validation
cv = StratifiedKFold(n_splits=5, shuffle=True, random_state=42)
cv_scores = cross_val_score(model_pipeline, X_train, y_train, cv=cv, scoring='roc_auc')
print(f"5-Fold CV ROC-AUC: {cv_scores.mean():.4f} (± {cv_scores.std():.4f})")
# Fit final model on complete training set
model_pipeline.fit(X_train, y_train)
# Evaluate on held-out test set
test_score = model_pipeline.score(X_test, y_test)
print(f"Held-out Test Accuracy: {test_score * 100:.2f}%")
# Step 4: Serialize Pipeline Artifact
joblib.dump(model_pipeline, "capstone_churn_pipeline.joblib", compress=3)
print("Model pipeline successfully saved to capstone_churn_pipeline.joblib!")
Combine this trained pipeline with the Streamlit app from Class 30 and the Dockerfile from Class 44. You now have a complete, production-grade, portfolio-ready project to link directly on your LinkedIn profile and resume!
Broaden your statistical analysis, Python data wrangling, and production engineering expertise:
Warm up with smaller focused labs before starting your full capstone.
Package and deploy your capstone project using Docker and Streamlit.
Feature your capstone repository on GitHub and LinkedIn for recruiters.
Add model interpretability and fairness audits to your capstone project.