Project 2 - Customer Churn Prediction
Customer churn occurs when customers stop using a service. Businesses use churn analysis to understand customer behavior and identify customers who may be at risk of leaving.
This project introduces a binary classification workflow using Scikit-learn.
Assume a prepared dataset named customers.csv contains numerical features and a target column called Churn, where 1 indicates that a customer left and 0 indicates that the customer stayed.
Step 1: Prepare the Data
import pandas as pd
from sklearn.model_selection import train_test_split
df = pd.read_csv("customers.csv")
X = df.drop(columns=["Churn"])
y = df["Churn"]
X_train, X_test, y_train, y_test = train_test_split(
X,
y,
test_size=0.2,
random_state=42,
stratify=y
)
The example assumes that the input features are already numeric and contain no missing values. If the dataset contains categorical columns or missing values, add suitable preprocessing before training.
Step 2: Train a Classification Model
from sklearn.pipeline import Pipeline
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
model = Pipeline([
("imputer", SimpleImputer(strategy="median")),
("scaler", StandardScaler()),
("classifier", LogisticRegression(max_iter=1000))
])
model.fit(X_train, y_train)
predictions = model.predict(X_test)
The pipeline fills missing numerical values with the median, standardizes features, and trains a logistic regression classifier. For mixed data types, use a ColumnTransformer to preprocess numerical and categorical columns separately.
Step 3: Evaluate the Model
from sklearn.metrics import (
classification_report,
confusion_matrix,
roc_auc_score
)
probabilities = model.predict_proba(X_test)[:, 1]
print(classification_report(y_test, predictions))
print(confusion_matrix(y_test, predictions))
print("ROC-AUC:", roc_auc_score(y_test, probabilities))
Accuracy alone may hide poor performance when churn is uncommon. Precision and recall help evaluate the model's ability to identify customers who are likely to leave.
Possible project outcomes:
- Identify customer groups with elevated churn risk.
- Explore factors associated with customer departures.
- Help business teams investigate retention opportunities.
- Compare model performance at different classification thresholds.
A churn prediction is not proof that a customer will leave. The model should support further investigation rather than automatically determine how customers are treated.
Python for Data Science
Vishalini A








