🚀
Supervised Learning Capstone Project
00 Sup Learning Capstone Tree Methods
++++
Data Science
May 2026×Notebook lesson

Notebook converted from Jupyter for blog publishing.

00-Sup-Learning-Capstone-Tree-Methods

Driptanil Datta
Driptanil DattaSoftware Developer

Supervised Learning Capstone Project - Tree Methods Focus

Make sure to review the introduction video to understand the 3 ways of approaching this project exercise!


Ways to approach the project:

  1. Open a new notebook, read in the data, and then analyze and visualize whatever you want, then create a predictive model.
  2. Use this notebook as a general guide, completing the tasks in bold shown below.
  3. Skip to the solutions notebook and video, and treat project at a more relaxing code along walkthrough lecture series.


GOAL: Create a model to predict whether or not a customer will Churn .



Complete the Tasks in Bold Below!

Part 0: Imports and Read in the Data

TASK: Run the filled out cells below to import libraries and read in your data. The data file is "Telco-Customer-Churn.csv"

# RUN THESE CELLS TO START THE PROJECT!
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
df = pd.read_csv('../DATA/Telco-Customer-Churn.csv')
df.head()
HTML
MORE
customerID
gender
SeniorCitizen
Partner
Dependents

Part 1: Quick Data Check

TASK: Confirm quickly with .info() methods the datatypes and non-null values in your dataframe.

# CODE HERE

TASK: Get a quick statistical summary of the numeric columns with .describe() , you should notice that many columns are categorical, meaning you will eventually need to convert them to dummy variables.

# CODE HERE

Part 2: Exploratory Data Analysis

General Feature Exploration

TASK: Confirm that there are no NaN cells by displaying NaN values per feature column.

# CODE HERE

TASK:Display the balance of the class labels (Churn) with a Count Plot.

# CODE HERE

TASK: Explore the distrbution of TotalCharges between Churn categories with a Box Plot or Violin Plot.

# CODE HERE

TASK: Create a boxplot showing the distribution of TotalCharges per Contract type, also add in a hue coloring based on the Churn class.

#CODE HERE

TASK: Create a bar plot showing the correlation of the following features to the class label. Keep in mind, for the categorical features, you will need to convert them into dummy variables first, as you can only calculate correlation for numeric features.

['gender', 'SeniorCitizen', 'Partner', 'Dependents','PhoneService', 'MultipleLines', 'OnlineSecurity', 'OnlineBackup', 'DeviceProtection', 'TechSupport', 'InternetService', 'StreamingTV', 'StreamingMovies', 'Contract', 'PaperlessBilling', 'PaymentMethod']

Note, we specifically listed only the features above, you should not check the correlation for every feature, as some features have too many unique instances for such an analysis, such as customerID

#CODE HERE


Part 3: Churn Analysis

This section focuses on segementing customers based on their tenure, creating "cohorts", allowing us to examine differences between customer cohort segments.

TASK: What are the 3 contract types available?

# CODE HERE

TASK: Create a histogram displaying the distribution of 'tenure' column, which is the amount of months a customer was or has been on a customer.

#CODE HERE

TASK: Now use the seaborn documentation as a guide to create histograms separated by two additional features, Churn and Contract.

#CODE HERE

TASK: Display a scatter plot of Total Charges versus Monthly Charges, and color hue by Churn.

#CODE HERE

Creating Cohorts based on Tenure

Let's begin by treating each unique tenure length, 1 month, 2 month, 3 month...N months as its own cohort.

TASK: Treating each unique tenure group as a cohort, calculate the Churn rate (percentage that had Yes Churn) per cohort. For example, the cohort that has had a tenure of 1 month should have a Churn rate of 61.99%. You should have cohorts 1-72 months with a general trend of the longer the tenure of the cohort, the less of a churn rate. This makes sense as you are less likely to stop service the longer you've had it.

#CODE HERE

TASK: Now that you have Churn Rate per tenure group 1-72 months, create a plot showing churn rate per months of tenure.

#CODE HERE

Broader Cohort Groups

TASK: Based on the tenure column values, create a new column called Tenure Cohort that creates 4 separate categories:

  • '0-12 Months'
  • '12-24 Months'
  • '24-48 Months'
  • 'Over 48 Months'
# CODE HERE

TASK: Create a scatterplot of Total Charges versus Monthly Charts,colored by Tenure Cohort defined in the previous task.

#CODE HERE

TASK: Create a count plot showing the churn count per cohort.

# CODE HERE

TASK: Create a grid of Count Plots showing counts per Tenure Cohort, separated out by contract type and colored by the Churn hue.

#CODE HERE

Part 4: Predictive Modeling

Let's explore 4 different tree based methods: A Single Decision Tree, Random Forest, AdaBoost, Gradient Boosting. Feel free to add any other supervised learning models to your comparisons!

Single Decision Tree

TASK : Separate out the data into X features and Y label. Create dummy variables where necessary and note which features are not useful and should be dropped.

#CODE HERE

TASK: Perform a train test split, holding out 10% of the data for testing. We'll use a random_state of 101 in the solutions notebook/video.

#CODE HERE

TASK: Decision Tree Perfomance. Complete the following tasks:

  1. Train a single decision tree model (feel free to grid search for optimal hyperparameters).
  2. Evaluate performance metrics from decision tree, including classification report and plotting a confusion matrix.
  3. Calculate feature importances from the decision tree.
  4. OPTIONAL: Plot your tree, note, the tree could be huge depending on your pruning, so it may crash your notebook if you display it with plot_tree.

Random Forest

TASK: Create a Random Forest model and create a classification report and confusion matrix from its predicted results on the test set.

#CODE HERE

Boosted Trees

TASK: Use AdaBoost or Gradient Boosting to create a model and report back the classification report and plot a confusion matrix for its predicted results

#CODE HERE

TASK: Analyze your results, which model performed best for you?

# With base models, we got best performance from an AdaBoostClassifier, but note, we didn't do any gridsearching AND most models performed about the same on the data set.

Great job!

Drip

Driptanil Datta

Software Developer

Building full-stack systems, one commit at a time. This blog is a centralized learning archive for developers.

Legal Notes
Disclaimer

The content provided on this blog is for educational and informational purposes only. While I strive for accuracy, all information is provided "as is" without any warranties of completeness, reliability, or accuracy. Any action you take upon the information found on this website is strictly at your own risk.

Copyright & IP

Certain technical content, interview questions, and datasets are curated from external educational sources to provide a centralized learning resource. Respect for original authorship is maintained; no copyright infringement is intended. All trademarks, logos, and brand names are the property of their respective owners.

System Operational

© 2026 Driptanil Datta. All rights reserved.