Predicting Customer Churn with MindsDB
Introduction
In this tutorial, we’ll create and train a machine learning model, or as we call
it, an AI Table
or a predictor
. By querying the model, we’ll predict the
probability of churn for new customers of a telecoms company.
Make sure you have access to a working MindsDB installation, either locally or at MindsDB Cloud.
If you want to learn how to set up your account at MindsDB Cloud, follow this guide. Another way is to set up MindsDB locally using Docker or Python.
Let’s get started.
Data Setup
Connecting the Data
There are a couple of ways you can get the data to follow through with this tutorial.
You can connect to a demo database that we’ve prepared for you. It contains the data used throughout this tutorial (the example_db.demo_data.customer_churn
table).
Now you can run queries directly on the demo database. Let’s preview the data that we’ll use to train our predictor.
Pay Attention to the Queries
From now on, we’ll use the
files.churn
file as a table. Make sure you replace it with
example_db.demo_data.customer_churn
if you connect the data as a database.
Understanding the Data
We use the customer churn dataset, where each row is one customer, to predict whether the customer is going to stop using the company products.
Below is the sample data stored in the files.churn
table.
Where:
Column | Description | Data Type | Usage |
---|---|---|---|
CustomerId | The identification number of a customer. | character varying | Feature |
Gender | The gender of a customer. | character varying | Feature |
SeniorCitizen | It indicates whether the customer is a senior citizen (1 ) or not (0 ). | integer | Feature |
Partner | It indicates whether the customer has a partner (Yes ) or not (No ). | character varying | Feature |
Dependents | It indicates whether the customer has dependents (Yes ) or not (No ). | character varying | Feature |
Tenure | Number of months the customer has been staying with the company. | integer | Feature |
PhoneService | It indicates whether the customer has a phone service (Yes ) or not (No ). | character varying | Feature |
MultipleLines | It indicates whether the customer has multiple lines (Yes ) or not (No , No phone service ). | character varying | Feature |
InternetService | Customer’s internet service provider (DSL , Fiber optic , No ). | character varying | Feature |
OnlineSecurity | It indicates whether the customer has online security (Yes ) or not (No , No internet service ). | character varying | Feature |
OnlineBackup | It indicates whether the customer has online backup (Yes ) or not (No , No internet service ). | character varying | Feature |
DeviceProtection | It indicates whether the customer has device protection (Yes ) or not (No , No internet service ). | character varying | Feature |
TechSupport | It indicates whether the customer has tech support (Yes ) or not (No , No internet service ). | character varying | Feature |
StreamingTv | It indicates whether the customer has streaming TV (Yes ) or not (No , No internet service ). | character varying | Feature |
StreamingMovies | It indicates whether the customer has streaming movies (Yes ) or not (No , No internet service ). | character varying | Feature |
Contract | The contract term of the customer (Month-to-month , One year , Two year ). | character varying | Feature |
PaperlessBilling | It indicates whether the customer has paperless billing (Yes ) or not (No ). | character varying | Feature |
PaymentMethod | Customer’s payment method (Electronic check , Mailed check , Bank transfer (automatic) , Credit card (automatic) ). | character varying | Feature |
MonthlyCharges | The monthly charge amount. | money | Feature |
TotalCharges | The total amount charged to the customer. | money | Feature |
Churn | It indicates whether the customer churned (Yes ) or not (No ). | character varying | Label |
Labels and Features
A label is a column whose values will be predicted (the y variable in simple linear regression).
A feature is a column used to train the model (the x variable in simple linear regression).
Training a Predictor
Let’s create and train the machine learning model. For that, we use the
CREATE MODEL
statement and specify the
input columns used to train FROM
(features) and what we want to
PREDICT
(labels).
We use all of the columns as features, except for the Churn
column, whose
values will be predicted.
Status of a Predictor
A predictor may take a couple of minutes for the training to complete. You can monitor the status of the predictor by using this SQL command:
If we run it right after creating a predictor, we get this output:
A bit later, this is the output:
And at last, this should be the output:
Now, if the status of our predictor says complete
, we can start making
predictions!
Making Predictions
Making a Single Prediction
You can make predictions by querying the predictor as if it were a table. The
SELECT
statement lets you make predictions for the label
based on the chosen features.
On execution, we get:
To get more accurate predictions, we should provide as much data as possible in
the WHERE
clause. Let’s run another query.
On execution, we get:
MindsDB predicted the probability of this customer churning with confidence of around 82%. The previous query predicted it with confidence of around 79%. So providing more data improved the confidence level of predictions.
Making Batch Predictions
Also, you can make bulk predictions by joining a data table with your predictor
using JOIN
.
On execution, we get:
What’s Next?
Have fun while trying it out yourself!
- Bookmark MindsDB repository on GitHub.
- Sign up for a free MindsDB account.
- Engage with the MindsDB community on Slack or GitHub to ask questions and share your ideas and thoughts.
If this tutorial was helpful, please give us a GitHub star here.