Contact Menu
Back

Beginner’s Guide to Python Modeling Using XGBoost Package

November 11, 2019 Explorium Data Science Team Data Science

Everyone knows that having a large number of loyal customers is the key to the success of a business. This statement is even more significant for banks. It is important for banks to retain their customers and prevent churn — the term used to describe when a customer ends their contract or business with an organization.

Can a bank know beforehand that a customer is going to leave? In the past, the answer to this question was based on a few standard rules. This rule-based decision making was prone to error and changing circumstances. However, with the advent of Big Data and machine learning, it has become possible to predict customer churn with a high degree of accuracy. 

Let’s break down how a machine learning algorithm can help predict customer churn.

But first, a little about what we used for this blog post:

The dataset used to train the machine learning algorithm is freely available from Kaggle. Various machine learning libraries have been developed in different programming languages that can be used for training machine learning algorithms. In this article, the XGboost algorithm will be used to predict customer churn. The XGboost algorithm is a part of XGBoost package. The installation instructions for the package are available at this link.  The library is freely available. All the code in this article has been written and tested using Python Jupyter Notebook

Now without further ado, let’s jump into the code.

Feature Generation: The Next Frontier of Data Science

Importing libraries

Before we can use any library in Python, we first need to import it. For data visualization, we will use matplotlib and seaborn libraries. To read and manipulate the dataset, we will use the pandas library. The XGboost library will be used to train the XGBClassifier. In addition, sklearn library will be used for data splitting and evaluating the performance of the trained machine learning model. The following script imports the required libraries.

Importing the dataset

Once the required libraries have been imported, the next step is to import the dataset into the application. To do so, the read_csv() function of the pandas library can be used as shown below:

Let’s see the number of columns and rows in the dataset:

Output:

The output shows that the dataset contains 10,000 rows and 11 columns. 

The dataset columns can be printed via the head() function.

Output:

Customer Churn Table

Let’s understand the data first. The first 10 of the 11 columns contain information about the customers, gathered six months before it was found whether or not the corresponding customers left the bank. After six months, if the customer left the bank a “1” was added in the corresponding row of the 11th column (i.e. “Exited”). If the customer stayed after six months, a “0” was entered in the “Exited” column.

 

Note: The name of the first column is “RowNumber” but due to space issue the name has been truncated. 

To find statistical information about the data the describe() method is used.

Output:

Customer Churn Table

You can see the count and mean standard deviation values for all the numerical columns. For instance, the average age of customers is 38.92.

Exploratory data analysis

The next step is to perform exploratory data analysis to study feature importance. To have a better view, the default size of the plots can be increased with the following script:

Let’s first see the number of customers who left the bank versus those who didn’t. We can use the count plot from the seaborn library for this purpose.

Output:

Customer Churn Exited

You can see that around 2,000 customers left the bank after six months.

Next, we will study the impact of the geography of the customer on customer churn. Let’s first plot the total number of customers per geographical location.

Output:

Customer churn geography

The customers belong to three different geographical locations: France, Spain, and Germany. While almost 50% of the total customers are French, the number of Spanish and German customers is around 25% each.

To find the impact of geography on customer churn, we can plot the count of values from the “Exited” column against a customer’s geography using the following script:

Output:

Customer churn exited by geography

The output shows that among the customers who left the bank, French and German customers have an equal number despite the fact that the total German customers are almost half of the total French customers. It shows that German customers are more likely to leave the bank than French and Spanish customers. 

Next, the collective impact of age and geography on customer churn can be plotted with the box plot as shown below:

Output:

Customer churn box and whisker

The above plot looks tricky but it is actually very simple. The box plot plots the quartile information for a column in boxes. On the left side, you can see three boxes (each for one geographical location) that contain quartile information of age for the customers who didn’t leave the bank. You can see that the customers between the age of 30 and 40 are more likely to stay for all three geographical locations. Similarly, from the three boxes on the right, it can be inferred that customers in the age group of around 40 to 50 are more likely to leave the bank.

In the same way, the relationship between age, gender, and customer churn can be plotted using the box plot as shown below:

Output:

Customer churn box and whisker

Here again, irrespective of the gender, the customer in the age group of 30-40 are likely to stay while those in the age group of 40-50 are likely to leave the bank.

Let’s now study the impact of balance on customer churn. We can do so via the bar plot.

Output:

Customer churn exit

The output shows that the average balance of the customers who left the bank is slightly greater than the average balance of those who stayed. 

In the same way, the relation between estimated salary and the customer churn can be plotted:

Output:

Customer churn exit

It seems that the average salary of the customers who left the bank and those who didn’t is almost the same.

Finally, let’s plot the relationship between the activeness of a customer on customer churn. A customer’s activeness is determined based on the number of transactions in a certain period, the number of logins on the web portal of a bank, etc. The “IsActiveMember” column contains information regarding a customer’s activeness. 

Output:

customer churn active member

You can see that the ratio of customers who exited the bank (orange bar) is greater for inactive customers as compared to the active ones. 

We saw the impact of some of the features on customer churn. Let’s now train our machine learning model on the dataset we have.

Data pre-processing

Before we can train our algorithm, we need to select the features that we want to use for training. The “RowNumber”, “CustomerId”, and “Surname” columns have purely random information and have no impact on customer churn. Nobody leaves or stays in a bank because he has a certain customer id or a specific surname. Let’s remove these columns from the dataset.

Machine learning algorithms expect data in the form of features and labels. The features are used to train the model against the ground truth labels. Our feature set consists of all the columns except the “Exited” column, which contains the labels or the outputs. The following script divides the data into feature and label sets.

Next, we need to convert the columns that contain categorical information such as “Geography” and “Gender” into numeric ones since machine learning algorithms work with numbers. They are not, for example, capable of adding or subtracting “France” to a value in the balance column. One-Hot encoding is a technique used to convert categorical columns to numeric ones. In One-Hot encoding, a numeric column containing “1” or “0” is generated for each unique value in the original categorical column. 

In our dataset, the “Geography” and “Gender” columns have categorical features. The following script performs One-Hot encoding for these categorical columns and replaces the features with their numeric counterparts.  

As the final pre-processing step, we need to divide the dataset into training and test sets. The training set is used to train the machine learning algorithm while the test set is used to evaluate the performance of the trained algorithm.

The dataset has been pre-processed and ready for training the algorithm.

Algorithm training and testing

As I said earlier, we will be using the XGboost algorithm for training our machine learning model. The following script creates an object of  XGboost class (i.e. “model”) and trains it using the training set. 

Yes, it takes two lines to train the model. The training tests and labels are passed to the fit() method of the model object.

To make predictions on a new dataset, the predict() method is used as shown below:

The “y_pred” variable contains the predictions.

To evaluate the performance of a classification algorithm, accuracy and F1 measure are two of the most commonly used metrics. The sklearn library contains built-in classes for these metrics. The following script finds the accuracy and F1 measure values for our algorithm.

Here is the output:

The output shows an accuracy of 86.76%. This means that on average, out of 100 times, our algorithm correctly predicts which customers are likely to leave the bank 86.76 times. We achieve this accuracy with default parameters without fine-tuning our algorithm. Impressive, isn’t it? 

Finding the best features

As the last step, we will find what the best indicators are to decide customer churn, according to our trained model. To do so, we can use the “feature_importance” attribute of our model and print the relative importance of N number of features. Let’s find the five most important features.

Output:

customer churn ML features

The output shows that according to our algorithm, age is the most important feature for predicting customer churn followed by the number of products and the balance of the customer. 

Conclusion

Retaining customers is vital for the survival of a business. It is important for organizations to know which customers are going to leave in the near future so that they can take measures to prevent them from leaving. Given relevant data, machine learning algorithms can help make the process of understanding who will leave easier.

Feature Generation: The Next Frontier of Data Science

Subscribe Today! Get the latest updates with our newsletter.
We promise you'll love it.

Follow us

This site uses cookies to provide you with a great browsing experience. By continuing on our website, you accept our cookies policy. I accept
Some text about the article in TechCrunch
Read more