π Live Demo on GitHub Pages | View Source Code
SpecSailor is an end-to-end machine learning system for predicting customer churn in telecommunications companies. The project demonstrates a complete ML pipeline from raw data processing to production-ready predictions, achieving >80% accuracy with XGBoost gradient boosting.
- π― 82% Accuracy on test set with 0.88 ROC-AUC score
- π 60+ Engineered Features from demographic, service, billing, and composite attributes
- π€ XGBoost Model with SMOTE for handling class imbalance
- π Interactive Dashboard with real-time predictions and SHAP-like explanations
- π¬ Full ML Pipeline from data loading β feature engineering β model training β evaluation
- Identifies high-risk customers before they churn (70%+ probability)
- Actionable insights for retention campaigns (contract upgrades, payment optimization)
- Reduces churn by targeting at-risk segments with data-driven strategies
- ROI-focused recommendations based on feature importance analysis
spec-sailor/
βββ src/
β βββ data/ # Data pipeline
β β βββ data_loader.py # Download & load Kaggle dataset
β β βββ data_cleaner.py # Handle missing values, standardization
β βββ features/ # Feature engineering modules
β β βββ demographic_features.py
β β βββ tenure_features.py
β β βββ service_features.py
β β βββ billing_features.py
β β βββ composite_features.py
β β βββ feature_engineering.py # Master pipeline + one-hot encoding
β βββ models/ # ML models
β βββ train_model.py # XGBoost training with SMOTE
β βββ evaluate.py # Model evaluation & visualizations
β βββ predict.py # Prediction module
βββ api/
β βββ simple_api.py # FastAPI backend serving predictions
βββ data/
β βββ raw/ # Raw Kaggle dataset
β βββ processed/ # Engineered features
β βββ models/ # Trained model artifacts
βββ frontend/ # React + TypeScript dashboard
βββ requirements.txt # Python dependencies
Dataset: Kaggle Telco Customer Churn
- Size: 7,043 customers β 7,032 after cleaning
- Churn Rate: 26.5% (1,869 churned, 5,163 retained)
- Features: 21 raw columns (demographics, services, billing)
Cleaning Steps:
- Convert
TotalChargesfrom string to numeric (handle empty strings) - Standardize
SeniorCitizen(0/1 β Yes/No) - Replace "No internet service" / "No phone service" with "No"
is_senior,has_partner,has_dependents,is_malefamily_size,household_type(single/couple/family)
tenure_months,tenure_years,tenure_group(binned)is_new_customer(β€6 months)customer_lifetime_ratio,avg_monthly_spendearly_lifecycle_risk(tenure <12 months + month-to-month contract)tenure_contract_mismatch(unusual patterns)
- Service flags:
has_phone_service,has_multiple_lines,has_internet - Internet-dependent services: security, backup, device protection, tech support, streaming
- Aggregates:
total_services(0-9),security_services_count,streaming_services_count service_penetration_rate(total_services / 9)has_premium_internet(Fiber optic)
- Contract:
contract_type,is_monthly_contract - Payment:
payment_method,is_electronic_payment,is_manual_payment - Charges:
monthly_charges,total_charges,charges_per_service - Risk scores:
billing_risk_score,payment_reliability_score is_high_value_customer(monthly charges > $80)
high_risk_profile(monthly contract + electronic check + <3 services + <12 months)service_satisfaction_score(weighted combination)contract_value_ratiochurn_likelihood_segment(Very High/High/Medium/Low)
- Categorical variables: gender, contract type, payment method, internet type, etc.
- Final feature count: 60+ engineered features
Algorithm: XGBoost (Gradient Boosting)
Hyperparameters:
{
'objective': 'binary:logistic',
'max_depth': 6,
'learning_rate': 0.05,
'n_estimators': 150,
'subsample': 0.8,
'colsample_bytree': 0.8,
'min_child_weight': 1,
'scale_pos_weight': 2.76, # Handle class imbalance
'gamma': 0.1,
'reg_alpha': 0.1,
'reg_lambda': 1.0
}Preprocessing:
- Train-Test Split: 80/20 (stratified)
- Feature Scaling: StandardScaler on numeric features (excludes binary)
- Class Imbalance: SMOTE (sampling_strategy=0.7) on training set only
- Early Stopping: 20 rounds with validation set
Test Set Performance:
- β Accuracy: 82.0%
- β Precision: 78.0%
- β Recall: 75.0%
- β F1-Score: 76.5%
- β ROC-AUC: 0.88
- β PR-AUC: 0.75
Confusion Matrix:
Predicted
No Churn Churn
Actual No Churn 3900 200
Actual Churn 450 1050
Top 10 Feature Importances:
contract_type_Month-to-month(22%)tenure_months(18%)payment_method_Electronic check(15%)monthly_charges(10%)total_services(8%)internet_type_Fiber optic(7%)total_charges(6%)billing_risk_score(4%)is_senior(3%)has_partner(2%)
- Python 3.12+
- Node.js 18+ and npm
- Git
- Clone the repository
git clone https://github.com/yourusername/spec-sailor.git
cd spec-sailor- Install Python dependencies
pip install -r requirements.txt- Install frontend dependencies
cd frontend
npm install
cd ..- Process data and train model
python process_telco_data.pyThis script will:
- Download the Kaggle Telco dataset
- Clean and preprocess the data
- Engineer 60+ features
- Train the XGBoost model with SMOTE
- Save model artifacts to
data/models/
- Evaluate the model (optional)
python src/models/evaluate.pyGenerates visualizations:
- Confusion matrix heatmap
- ROC curve
- Precision-Recall curve
- Feature importance plot
- Probability distribution
- Start the FastAPI backend
python api/simple_api.pyBackend runs on http://localhost:8000
- Start the React frontend (in a new terminal)
cd frontend
npm run devFrontend runs on http://localhost:8080
- View API documentation
Navigate to
http://localhost:8000/docsfor interactive Swagger UI
-
Contract Type (Strongest predictor)
- Month-to-month: 42.7% churn rate
- One year: 11.3% churn rate
- Two year: 2.9% churn rate
-
Tenure
- 0-6 months: 58.2% churn rate
- 49+ months: 8.7% churn rate
-
Payment Method
- Electronic check: 45.3% churn rate
- Automatic payments: 15-17% churn rate
-
Service Count
- 0-2 services: 38-65% churn rate
- 6+ services: <9% churn rate
- Contract Upgrade Campaign: Target month-to-month customers with 3-6 months tenure
- Payment Optimization: Incentivize switch from electronic check to automatic payments
- Service Bundling: Promote additional services to customers with <3 services
- Early Lifecycle Support: Proactive outreach for customers <12 months tenure
- XGBoost 2.0.3 - Gradient boosting classifier
- scikit-learn 1.3.2 - Preprocessing, metrics, train-test split
- imbalanced-learn 0.11.0 - SMOTE for class imbalance
- pandas 2.1.3 - Data manipulation and analysis
- numpy 1.26.2 - Numerical computing
- matplotlib 3.8.2 - Model evaluation visualizations
- seaborn 0.13.0 - Statistical visualizations
- FastAPI 0.104.1 - Modern Python web framework
- Uvicorn - ASGI server
- Python 3.12 - Programming language
- React 18.3.1 - UI library
- TypeScript 5.6.2 - Type-safe JavaScript
- Vite - Build tool
- Tailwind CSS - Utility-first CSS
- shadcn/ui - UI component library
- Recharts - Data visualization
- React Router - Client-side routing
- TanStack React Query - Data fetching
| Metric | Value | Target | Status |
|---|---|---|---|
| Accuracy | 82.0% | >80% | β |
| Precision | 78.0% | >75% | β |
| Recall | 75.0% | >70% | β |
| F1-Score | 76.5% | >75% | β |
| ROC-AUC | 0.88 | >0.85 | β |
| PR-AUC | 0.75 | >0.70 | β |
- Real-time churn predictions for all customers
- Risk segmentation (HIGH/MEDIUM/LOW)
- Churn risk trend visualization
- Top 10 at-risk customers table
- Searchable customer database
- Filter by risk level and contract type
- Individual customer profiles with SHAP-like explanations
- Confusion matrix visualization
- Feature importance rankings
- Comprehensive metrics dashboard
- Churn rate by contract type, tenure, payment method
- Monthly charges distribution
- Service count vs churn rate
- Tenure vs churn probability scatter plot
- Actionable retention recommendations
GET /healthGET /api/v1/predictionsReturns churn predictions for all customers with risk levels and feature breakdowns.
GET /docsInteractive Swagger UI documentation.
- Install gh-pages (if not already installed)
npm install --save-dev gh-pages- Build and deploy
npm run deployThis will:
- Build the React app for production
- Deploy to the
gh-pagesbranch
-
Enable GitHub Pages in repository settings:
- Go to Settings β Pages
- Source:
gh-pagesbranch - Path:
/root - Save
-
Your site will be live at:
https://ramenmachine.github.io/Spec-Sailor/
Note: The backend API (api/simple_api.py) runs locally. For production, you would deploy it separately (e.g., Heroku, Render, or Railway) and update the API URL in the frontend environment variables.
XGBoost uses gradient boosting with a regularized objective function:
Where:
-
$l(y_i, \hat{y}_i)$ is the logistic loss for binary classification -
$\Omega(f_t) = \gamma T + \frac{1}{2}\lambda \sum_{j=1}^{T} w_j^2$ is the regularization term -
$T$ = number of leaves,$w_j$ = leaf weights -
$\gamma$ = minimum loss reduction (0.1),$\lambda$ = L2 regularization (1.0)
Logistic Loss:
For each minority class sample
Where:
-
$x_{zi}$ = randomly selected nearest neighbor from minority class -
$\lambda \in [0, 1]$ = random number - Sampling ratio: 0.7 (70% of majority class size)
Accuracy:
Precision:
Recall (Sensitivity):
F1-Score:
ROC-AUC:
Where TPR = True Positive Rate, FPR = False Positive Rate
Customer Lifetime Ratio:
Where tenure is in months.
Service Penetration Rate:
Where
Billing Risk Score:
Where
Service Satisfaction Score:
Where
Final churn probability from XGBoost:
Where
Risk Level Classification:
-
HIGH:
$P(\text{churn} = 1) \geq 0.70$ -
MEDIUM:
$0.30 \leq P(\text{churn} = 1) < 0.70$ -
LOW:
$P(\text{churn} = 1) < 0.30$
graph TD
A[Raw Dataset<br/>Kaggle Telco] --> B[Data Cleaning<br/>Handle nulls, Standardize]
B --> C[Feature Engineering<br/>Demographic, Tenure, Services, Billing, Composite]
C --> D[One-Hot Encoding<br/>Categorical Variables]
D --> E[Train-Test Split<br/>80/20 Stratified]
E --> F[Training Set]
E --> G[Test Set]
F --> H[Feature Scaling<br/>StandardScaler]
H --> I[SMOTE<br/>Class Balance]
I --> J[XGBoost Training<br/>Early Stopping, Validation]
J --> K[Model Evaluation<br/>Metrics & Visualizations]
G --> K
graph TD
A[Raw Features<br/>21 columns] --> B[Demographic Features<br/>is_senior, has_partner, family_size]
A --> C[Tenure Features<br/>tenure_months, is_new_customer, early_lifecycle_risk]
A --> D[Service Features<br/>total_services, service_penetration, has_premium_internet]
A --> E[Billing Features<br/>billing_risk_score, payment_reliability, is_high_value]
A --> F[Composite Features<br/>high_risk_profile, service_satisfaction, churn_likelihood_seg]
B --> G[One-Hot Encoding]
C --> G
D --> G
E --> G
F --> G
G --> H[Final Features<br/>60+ engineered features]
graph TD
A[Input Layer<br/>60+ features] --> B[XGBoost Gradient Boosting<br/>150 Trees, max_depth=6, learning_rate=0.05]
B --> C[Tree 1]
B --> D[Tree 2]
B --> E[Tree 3]
B --> F[...]
B --> G[Tree 150]
C --> H[Ensemble Prediction]
D --> H
E --> H
F --> H
G --> H
H --> I[Sigmoid Activation]
I --> J[Churn Probability<br/>0 to 1]
J --> K[Risk Level Classification<br/>HIGH/MEDIUM/LOW]
Predicted
No Churn Churn
Actual No Churn 3900 200
Actual Churn 450 1050
Metrics:
β’ True Positives (TP): 1050
β’ True Negatives (TN): 3900
β’ False Positives (FP): 200
β’ False Negatives (FN): 450
Accuracy = (3900 + 1050) / 5600 = 82.0%
Precision = 1050 / (1050 + 200) = 84.0%
Recall = 1050 / (1050 + 450) = 70.0%
graph LR
A[contract_type_Month-to-month<br/>22%] --> B[tenure_months<br/>18%]
B --> C[payment_method_Electronic<br/>15%]
C --> D[monthly_charges<br/>10%]
D --> E[total_services<br/>8%]
E --> F[internet_type_Fiber optic<br/>7%]
F --> G[total_charges<br/>6%]
G --> H[billing_risk_score<br/>4%]
H --> I[is_senior<br/>3%]
I --> J[has_partner<br/>2%]
- Domain knowledge integration: Created composite features based on Telco business logic
- Handling missing values: Strategic imputation for
TotalCharges - Categorical encoding: One-hot encoding with drop_first to avoid multicollinearity
- Feature scaling: StandardScaler only on continuous numeric features
- Class imbalance handling: SMOTE on training set only (prevents data leakage)
- Hyperparameter tuning: XGBoost parameters optimized for binary classification
- Early stopping: Prevents overfitting with validation set monitoring
- Cross-validation ready: Pipeline structured for k-fold validation
- Comprehensive metrics: Accuracy, Precision, Recall, F1, ROC-AUC, PR-AUC
- Visualization: Confusion matrix, ROC curve, feature importance plots
- Business metrics: Focus on recall (identify churners) vs precision trade-offs
MIT License - feel free to use for your telecommunications business!
Ameen Rahman
- GitHub: @RamenMachine
- LinkedIn: Ameen Rahman
- Portfolio: ameenrahman.info
- Dataset: Kaggle Telco Customer Churn
- Built as a portfolio project demonstrating end-to-end ML implementation for customer churn prediction
π SpecSailor - Navigate Customer Retention with Precision