Home > Backend Development > Python Tutorial > Decision Tree Classifier Example to Predict Customer Churn

Decision Tree Classifier Example to Predict Customer Churn

Linda Hamilton
Release: 2024-12-10 13:30:11
Original
911 people have browsed it

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
Copy after login
Copy after login
Copy after login
Copy after login
  • 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
Copy after login
Copy after login
Copy after login
Copy after login
  • 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")
Copy after login
Copy after login
  • 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
Copy after login
Copy after login
Copy after login
Copy after login
  • 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")
Copy after login
Copy after login
  • 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())
Copy after login
  • 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
Copy after login
  • 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)
Copy after login
  • 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)
Copy after login
  • 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
Copy after login
Copy after login
Copy after login
Copy after login
  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!

source:dev.to
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template