search
HomeBackend DevelopmentPython TutorialDecision Tree Classifier Example to Predict Customer Churn

Decision Tree Classifier Example to Predict Customer Churn

Decision Tree Classifier Example to Predict Customer Churn

Overview

This project demonstrates how to predict customer churn (whether a customer leaves a service) using a Decision Tree Classifier. The dataset includes features like age, monthly charges, and customer service calls, with the goal of predicting whether a customer will churn or not.

The model is trained using Scikit-learn's Decision Tree Classifier, and the code visualizes the decision tree to better understand how the model is making decisions.


Technologies Used

  • Python 3.x: Primary language used for building the model.
  • Pandas: For data manipulation and handling datasets.
  • Matplotlib: For data visualization (plotting decision tree).
  • Scikit-learn: For machine learning, including model training and evaluation.

Steps Explained

1. Import Necessary Libraries

import pandas as pd
import matplotlib.pyplot as plt
import warnings
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score
from sklearn import tree
  • Pandas (pd):

    • This is used for data manipulation and loading data into DataFrame format. DataFrames allow you to organize and manipulate structured data like tables (rows and columns).
  • Matplotlib (plt):

    • This is a plotting library used to visualize data. Here, it’s used to plot the decision tree graphically, which helps in understanding how decisions are made at each node of the tree.
  • Warnings (warnings):

    • The warnings module is used to suppress or handle warnings. In this code, we’re ignoring unnecessary warnings to keep the output clean and readable.
  • Scikit-learn libraries:

    • train_test_split: This function splits the dataset into training and testing subsets. Training data is used to fit the model, and testing data is used to evaluate its performance.
    • DecisionTreeClassifier: This is the model that will be used to classify the data and predict customer churn. Decision Trees work by creating a tree-like model of decisions based on the features.
    • accuracy_score: This function calculates the accuracy of the model by comparing the predicted values with the actual values of the target variable (Churn).
    • tree: This module includes functions for visualizing the decision tree once it is trained.

2. Suppressing Warnings

import pandas as pd
import matplotlib.pyplot as plt
import warnings
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score
from sklearn import tree
  • This line tells Python to ignore all warnings. It can be helpful when you're running models and don't want warnings (such as those about deprecated functions) to clutter the output.

3. Creating a Synthetic Dataset

warnings.filterwarnings("ignore")
  • Here, we create a synthetic dataset for the project. This dataset simulates customer information for a telecom company, with features such as Age, MonthlyCharge, CustomerServiceCalls, and the target variable Churn (whether the customer churned or not).

    • CustomerID: Unique identifier for each customer.
    • Age: Customer’s age.
    • MonthlyCharge: Monthly bill of the customer.
    • CustomerServiceCalls: The number of times a customer called customer service.
    • Churn: Whether the customer churned (Yes/No).
  • Pandas DataFrame: The data is structured as a DataFrame (df), a 2-dimensional labeled data structure, allowing easy manipulation and analysis of data.

4. Splitting Data into Features and Target Variable

import pandas as pd
import matplotlib.pyplot as plt
import warnings
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score
from sklearn import tree
  • Features (X): The independent variables that are used to predict the target. In this case, it includes Age, MonthlyCharge, and CustomerServiceCalls.
  • Target variable (y): The dependent variable, which is the value you are trying to predict. Here, it is the Churn column, which indicates whether a customer will churn or not.

5. Splitting the Data into Training and Testing Sets

warnings.filterwarnings("ignore")
  • train_test_split splits the dataset into two parts: a training set (used to train the model) and a testing set (used to evaluate the model).
    • test_size=0.3: 30% of the data is set aside for testing, and the remaining 70% is used for training.
    • random_state=42 ensures reproducibility of results by fixing the seed for the random number generator.

6. Training the Decision Tree Model

data = {
    'CustomerID': range(1, 101),  # Unique ID for each customer
    'Age': [20, 25, 30, 35, 40, 45, 50, 55, 60, 65]*10,  # Age of customers
    'MonthlyCharge': [50, 60, 70, 80, 90, 100, 110, 120, 130, 140]*10,  # Monthly bill amount
    'CustomerServiceCalls': [1, 2, 3, 4, 0, 1, 2, 3, 4, 0]*10,  # Number of customer service calls
    'Churn': ['No', 'No', 'Yes', 'No', 'Yes', 'No', 'Yes', 'Yes', 'No', 'Yes']*10  # Churn status
}

df = pd.DataFrame(data)
print(df.head())
  • DecisionTreeClassifier() initializes the decision tree model.
  • clf.fit(X_train, y_train) trains the model using the training data. The model learns patterns from the X_train features to predict the y_train target variable.

7. Making Predictions

X = df[['Age', 'MonthlyCharge', 'CustomerServiceCalls']]  # Features
y = df['Churn']  # Target Variable
  • clf.predict(X_test): After the model is trained, it is used to make predictions on the test set (X_test). These predicted values are stored in y_pred, and we will compare them with the actual values (y_test) to evaluate the model.

8. Evaluating the Model

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
  • accuracy_score(y_test, y_pred) calculates the accuracy of the model by comparing the predicted churn labels (y_pred) with the actual churn labels (y_test) from the test set.
  • The accuracy is a measure of how many predictions were correct. It is printed out for evaluation.

9. Visualizing the Decision Tree

clf = DecisionTreeClassifier()
clf.fit(X_train, y_train)
  • tree.plot_tree(clf, filled=True): Visualizes the trained decision tree model. The filled=True argument colors the nodes based on the class label (Churn/No Churn).
  • feature_names: Specifies the names of the features (independent variables) to display in the tree.
  • class_names: Specifies the class labels for the target variable (Churn).
  • plt.show(): Displays the tree visualization.

Running the Code

  1. Clone the repository or download the script.
  2. Install dependencies:
import pandas as pd
import matplotlib.pyplot as plt
import warnings
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score
from sklearn import tree
  1. Run the Python script or Jupyter notebook to train the model and visualize the decision tree.

The above is the detailed content of Decision Tree Classifier Example to Predict Customer Churn. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Merging Lists in Python: Choosing the Right MethodMerging Lists in Python: Choosing the Right MethodMay 14, 2025 am 12:11 AM

TomergelistsinPython,youcanusethe operator,extendmethod,listcomprehension,oritertools.chain,eachwithspecificadvantages:1)The operatorissimplebutlessefficientforlargelists;2)extendismemory-efficientbutmodifiestheoriginallist;3)listcomprehensionoffersf

How to concatenate two lists in python 3?How to concatenate two lists in python 3?May 14, 2025 am 12:09 AM

In Python 3, two lists can be connected through a variety of methods: 1) Use operator, which is suitable for small lists, but is inefficient for large lists; 2) Use extend method, which is suitable for large lists, with high memory efficiency, but will modify the original list; 3) Use * operator, which is suitable for merging multiple lists, without modifying the original list; 4) Use itertools.chain, which is suitable for large data sets, with high memory efficiency.

Python concatenate list stringsPython concatenate list stringsMay 14, 2025 am 12:08 AM

Using the join() method is the most efficient way to connect strings from lists in Python. 1) Use the join() method to be efficient and easy to read. 2) The cycle uses operators inefficiently for large lists. 3) The combination of list comprehension and join() is suitable for scenarios that require conversion. 4) The reduce() method is suitable for other types of reductions, but is inefficient for string concatenation. The complete sentence ends.

Python execution, what is that?Python execution, what is that?May 14, 2025 am 12:06 AM

PythonexecutionistheprocessoftransformingPythoncodeintoexecutableinstructions.1)Theinterpreterreadsthecode,convertingitintobytecode,whichthePythonVirtualMachine(PVM)executes.2)TheGlobalInterpreterLock(GIL)managesthreadexecution,potentiallylimitingmul

Python: what are the key featuresPython: what are the key featuresMay 14, 2025 am 12:02 AM

Key features of Python include: 1. The syntax is concise and easy to understand, suitable for beginners; 2. Dynamic type system, improving development speed; 3. Rich standard library, supporting multiple tasks; 4. Strong community and ecosystem, providing extensive support; 5. Interpretation, suitable for scripting and rapid prototyping; 6. Multi-paradigm support, suitable for various programming styles.

Python: compiler or Interpreter?Python: compiler or Interpreter?May 13, 2025 am 12:10 AM

Python is an interpreted language, but it also includes the compilation process. 1) Python code is first compiled into bytecode. 2) Bytecode is interpreted and executed by Python virtual machine. 3) This hybrid mechanism makes Python both flexible and efficient, but not as fast as a fully compiled language.

Python For Loop vs While Loop: When to Use Which?Python For Loop vs While Loop: When to Use Which?May 13, 2025 am 12:07 AM

Useaforloopwheniteratingoverasequenceorforaspecificnumberoftimes;useawhileloopwhencontinuinguntilaconditionismet.Forloopsareidealforknownsequences,whilewhileloopssuitsituationswithundeterminediterations.

Python loops: The most common errorsPython loops: The most common errorsMay 13, 2025 am 12:07 AM

Pythonloopscanleadtoerrorslikeinfiniteloops,modifyinglistsduringiteration,off-by-oneerrors,zero-indexingissues,andnestedloopinefficiencies.Toavoidthese:1)Use'i

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool