Predicting Customer Churn using Apache Spark

Faisal Aldhuwayhi
6 min readSep 10, 2021

--

Churn Rate is an important index for numerous amount of companies. especially, the ones that interact with customers frequently.

If you can accurately identify the churned customers before they leave, your business can offer them discounts and incentives, potentially saving your business millions in revenue. Therefore, predicting the Churn Rate for your service is very beneficial for thriving the business work.

Project Overview

Sparkify is an imaginary, popular, digital music service similar to Spotify or Pandora. Menus of users stream their favorite songs to your service every day either using the free tier or using the premium subscription model.

The given customer’s dataset contains the logs of all the interactions between the users and the service, such as thumbs-up, and downgrade. Also, it includes other user-related and interaction fields, e.g. time-stamp.

The goal of this project is to build a model that predicts customer churn using the users' log data based on their past behavior. This project has utilized a mini subset (128MB) of the full dataset available (12GB). Optionally, you can choose to deploy a Spark cluster on the cloud using AWS or IBM Cloud to analyze a larger amount of data.

In this project, we will manipulate large and realistic datasets with Spark to engineer relevant features for predicting churn. Also, we will use Spark MLlib to build machine learning models with large datasets.

So, let’s get started...

Loading and Cleaning Dataset

The data consists of 286500 rows and 18 columns, and it contains 18 fields. The log data is in JSON format with each observation representing an interaction between the user and service. The schema below shows more details:

The Dataset Schema

We plan to remove the rows with missing or invalid values for the “userId” or “sessionId” fields. It appears that there are no missing values for both, but the userId column has 8346 empty values.

Exploratory Data Analysis

Now, the data is ready for exploration. We defined the Churn column as the label for the model using “Cancellation Confirmation” events. There are 173 active users, interacting with the service, and 52 canceled users.

As a comparison between the two groups of users, it looks like male users tend to cancel their service more than female users. As shown in the figure below:

Gender Distribution by User Status

The active users use the service at a free level more than paid level. While in the opposite, there are more canceled users of paid level than the free level of users, which is quite reasonable.

Level Distribution by User Status

As shown below in the figure, there is almost no difference between the average length of music listened to between the active and canceled groups. So, this might conclude that the music length listening is not a great factor in identifying the customer churn.

Music Length Listening Distribution by User Status

For each action/event in the service, the active users interact most with “Thumps Up”, “Add to Playlist” and “Add Friend” actions. In contrast, the canceled users interact most with “Roll Advert” and “Thumps Down” actions.

Percentage of Events Distribution by User Status

Along the days of the month, the canceled users are most interactive with the service in the first half of the month, as opposed to the second half. Therefore, The vast amount of cancellations occurs at the last of the month, which is rational since the user tries to avert renewal. The figure below elaborate on that more:

Percentage of Events Distribution by User Status Per Month Day

For more exploratory data analysis, visit the Github repository of the project.

Feature Engineering

Features were created as the characteristics of a user churn, and it is represented and labeled by the churn of user or not. The features have been generated to comprise the user-related features and their various interactions with the service. Thus, the total number of collected features is 14, which are:

  1. The gender of the user.
  2. Total time of listening length.
  3. The number of songs listened to.
  4. Average time spent in the platform.
  5. The total number of artists the user has listened to.
  6. The number of thumbs-up events.
  7. The number of thumbs-down events.
  8. The number of friends events.
  9. The number of help events.
  10. The number of error events.
  11. The number of roll advert events.
  12. The number of upgrades events.
  13. The number of downgrades events.
  14. The number of adds to the playlist.

The features have been standardized using StandardScaler to make the data in a more consistent range. Then, they have been consolidated in a vector to be fed in the model for the next part. Spark except for the input of the model to be in vector format.

Modeling

The data is split into two chunks, 80% as training and 20% as testing. Then, three different models have been cross-validated with 2 folds at first to choose one for hyperparameter tuning. The models are:

  1. Support Vector Machines (SVM).
  2. Gradient-Boosted Trees (GBT).
  3. Logistic Regression.

Since the churned users are a fairly small subset, we opt to use the F1 score as the main metric to optimize in addition to the Accuracy score.

Each model scores in the testing dataset as the following:

  • SVM: (F1: 58.4%| Accuracy: 70.5%)
  • GBT: (F1: 68.0%| Accuracy: 70.5%)
  • Log. Regression: (F1: 58.4%| Accuracy: 70.5%)

The Gradient-Boosted Trees Model has the highest F1 score and it performs very well in the training data. So, it is suggested then to apply hyperparameter tuning to this model to achieve better results and remove overfitting.

After extracting the feature importance for the GBT model, we see that listening time, number of adverts and number of friends are having the most effect in predicting the churn of a user. However, this ratio in feature importance is hard to fully agree on due to the unhigh score/performance of the model.

GBT Features Importance

Conclusions

In the end, using the big data tools and machine learning modeling approaches has accomplished the mission of manipulating the user log data and predicting customer churn. After comparing the F1-Score for each model, it has been pointed that the Gradient boosted tree model performed best for predicting customer churn.

Furthermore, there are more features that could be generated to represent the characteristics of the user. There is room for improvement on applying sophisticated hyperparameter tuning to the model for resulting in better outcomes. Also, the process of feeding the input to the model could be pipelined to adapt to the big data settings.

The project results are based mainly on a mini subset of the data (128MB), so it is recommended to finish the project with the full dataset (12GB). It would be beneficial and effective to observe the results with the full dataset and see the differences.

For the full work of the project, please check out the Project Jupyter Notebook in my Github. Feel free to leave any comments or feedback.

--

--