Goal
Given a customer dataset with churn (0/1), analyze:
- Overall churn rate
- Churn by segment (plan, region)
- Numeric differences (tenure, usage)
Analysis pipeline
flowchart LR A["Raw customers
(churn 0/1)"] --> B["Rate
(overall churn %)"] B --> C["Segment
(by plan, region)"] C --> D["Visualize
(churn by segment)"] D --> E["Model-ready
(encode, split)"]
Step 1: Load
import pandas as pd
df = pd.read_csv("data/churn.csv")
print(df.shape)
print(df.head())Step 2: Churn rate
rate = df["churn"].mean()
print("Churn rate:", rate)Step 3: Churn by category
import seaborn as sns
import matplotlib.pyplot as plt
plt.figure(figsize=(7, 4))
sns.barplot(data=df, x="plan", y="churn")
plt.title("Churn rate by plan")
plt.tight_layout()
plt.show()Step 4: Numeric differences
import seaborn as sns
import matplotlib.pyplot as plt
plt.figure(figsize=(7, 4))
sns.boxplot(data=df, x="churn", y="tenure")
plt.title("Tenure vs churn")
plt.tight_layout()
plt.show()Visualize it
Step 5: Create a simple model-ready dataset
- Handle missing values
- Encode categories
- Split train/test
This connects back to Phase 4 preprocessing.
Deliverable
Write insights:
- Which plan/segment churns more?
- Which features differ strongly?
- What interventions might reduce churn?
Check your understanding
What does df["churn"].mean() return for a 0/1 column?
The mean of a 0/1 column is a rate.
Show answer
A — The fraction of customers who churned — The mean of a 0/1 column is a rate.
How do you get a churn rate per plan?
The same mean pattern applied per group gives a rate for each plan.
Show answer
A — df.groupby("plan")["churn"].mean() — The same mean pattern applied per group gives a rate for each plan.
Why does the project use a boxplot of tenure by churn?
Tenure is numeric and churn splits customers into two groups, so a box plot shows the difference.
Show answer
A — To compare how a numeric feature differs between churned and retained customers — Tenure is numeric and churn splits customers into two groups, so a box plot shows the difference.
What does preparing a model-ready dataset involve here?
This links back to the Phase 4 preprocessing steps.
Show answer
A — Handle missing values, encode categories and split train/test — This links back to the Phase 4 preprocessing steps.
🧪 Try It Yourself
Exercise 1 – Overall churn rate
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 2 – Churn rate by plan
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 3 – Compare tenure between groups
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.