End-to-End Predictive Analytics Solution with Explainable AI
Assemble a machine learning pipeline for customer churn predictions, extract feature importances, compile local waterfall explanation tables, and audit predictions.
Domain / Environment
Telecom Business / Conda VM
Difficulty
Advanced (4/5)
Course Module
ML for Data Science
Deliverables
Global Feature plots & Local explainability logs
Explainable AI in VM sandboxes: Calculating local model explanations using packages like SHAP has complex compilation dependencies (C++ compiler libraries) that often fail inside VirtualBox Linux VMs. To ensure reliability for students, this project implements a tree-based local explanation algorithm that calculates feature contributions directly using decision pathways.
1. System Architecture & Process Workflow
The diagram below displays the Explainable AI (XAI) pipeline. Customer metrics are passed to the Random Forest model to generate churn predictions. A feature contribution analyzer maps the prediction path, outputting a waterfall explanation of why the customer was flagged.
2. Part 1: Step-by-Step Action Items & Key Execution Steps
STEP 1
Activate Python Virtual Environment
Point the terminal execution environment to the course conda sandbox environment.
$ conda activate ds_ai_ml
This targets active python libraries to the isolated virtual sandbox.
STEP 2
Create Project Folders inside Linux VM
Create a dedicated folder for the project files inside your guest VM home folder directory.
$ mkdir -p ~/Projects/churn_analytics && cd ~/Projects/churn_analytics
This sets up the working directory layout for the modeling code files.
STEP 3
Save Customer Churn Dataset
Create a CSV dataset containing customer churn records, tenure, charges, and contract types.
$ nano churn_dataset.csv
This opens nano editor. Paste the CSV data template from Part 2, press **Ctrl + O** and **Enter** to save, and **Ctrl + X** to exit.
STEP 4
Create pipeline script file in VS Code
Launch VS Code and create a new script file inside the project workspace folder.
Launch VS Code via terminal "code ." -> Right-click in explorer tree -> click New File -> Type: churn_solution.py -> Press Enter
This registers an empty file `churn_solution.py` inside the active directory editor workspace.
STEP 5
Load Python predictive logic
Paste the training, prediction, and local feature contribution code blocks into the empty file.
Click churn_solution.py -> Paste python code from Part 2 below -> Save file via Ctrl + S
This populates the script file with training and explainability logic.
STEP 6
Execute predictive analytics script
Run the validation script using the python engine to compare the models.
$ python churn_solution.py
This standardizes features, trains the Random Forest model, runs GridSearchCV, and prints local customer explanations to the terminal.
3. Operational Pipeline Architecture
The flowchart below outlines the explainable predictive analytics pipeline. It details dataset loading, pipeline training, running GridSearchCV, calculating local feature contributions, and saving output reports.
4. Part 2: Complete Deliverable Assets & Production Templates
To run the predictive solution, we need the raw churn CSV dataset and the Python script. Below is a line-by-line explanation of the code, followed by the combined template files.
Step-by-Step Code Construction
Lines 1 - 7
Import tabular and ML modules
Include system packages, Random Forest models, and metrics functions in the script.
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import classification_report, accuracy_score
These imports pull standard Pandas arrays, Random Forest classifiers, and classification evaluation report modules.
Lines 8 - 14
Scale features and split dataset
Load the dataset, separate feature matrices, and split into train and test sets.
df = pd.read_csv("churn_dataset.csv")
X = df[["Tenure", "MonthlyCharges", "ContractType", "TechSupport"]]
y = df["Churn"]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
This partitions feature columns and target variables, splitting them into training and test sets.
Lines 15 - 20
Train Random Forest Classifier
Fit the Random Forest model and print accuracy metrics.