{"id":10093,"date":"2020-11-25T10:00:50","date_gmt":"2020-11-25T10:00:50","guid":{"rendered":"https:\/\/uruit.com\/blog\/?p=10093"},"modified":"2023-05-31T10:08:47","modified_gmt":"2023-05-31T13:08:47","slug":"learn-how-to-predict-customer-churn-by-building-a-predictive-model","status":"publish","type":"post","link":"https:\/\/uruit.com\/blog\/churn-prediction-machine-learning\/","title":{"rendered":"Learn how to avoid customer churn with a predictive model"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_17 counter-hierarchy counter-decimal ez-toc-grey\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\">Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" style=\"display: none;\"><i class=\"ez-toc-glyphicon ez-toc-icon-toggle\"><\/i><\/a><\/span><\/div>\n<nav><ul class=\"ez-toc-list ez-toc-list-level-1\"><li class=\"ez-toc-page-1 ez-toc-heading-level-1\"><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/uruit.com\/blog\/churn-prediction-machine-learning\/#How_to_avoid_customer_churn_with_a_predictive_model\" title=\"How to avoid customer churn with a predictive model\">How to avoid customer churn with a predictive model<\/a><ul class=\"ez-toc-list-level-2\"><li class=\"ez-toc-heading-level-2\"><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/uruit.com\/blog\/churn-prediction-machine-learning\/#What_is_customer_churn\" title=\"What is customer churn?\">What is customer churn?<\/a><ul class=\"ez-toc-list-level-3\"><li class=\"ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/uruit.com\/blog\/churn-prediction-machine-learning\/#What_is_the_Average_Churn_Rate_for_SaaS\" title=\"What is the Average Churn Rate for SaaS?\">What is the Average Churn Rate for SaaS?<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/uruit.com\/blog\/churn-prediction-machine-learning\/#Voluntary_vs_involuntary_turnover\" title=\"Voluntary vs involuntary turnover\">Voluntary vs involuntary turnover<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/uruit.com\/blog\/churn-prediction-machine-learning\/#Customer_churn_prediction_using_machine_learning\" title=\"Customer churn prediction using machine learning\">Customer churn prediction using machine learning<\/a><\/li><\/ul><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-2\"><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/uruit.com\/blog\/churn-prediction-machine-learning\/#How_to_get_your_churn_prediction_using_Machine_Learning\" title=\"How to get your churn prediction using Machine Learning\">How to get your churn prediction using Machine Learning<\/a><ul class=\"ez-toc-list-level-3\"><li class=\"ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/uruit.com\/blog\/churn-prediction-machine-learning\/#Setting_the_Environment_churn_prediction_with_Kaggle\" title=\"Setting the Environment: churn prediction with Kaggle\">Setting the Environment: churn prediction with Kaggle<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/uruit.com\/blog\/churn-prediction-machine-learning\/#Exploratory_Data_Analysis_EDA_of_the_example_data\" title=\"Exploratory Data Analysis (EDA) of the example data\">Exploratory Data Analysis (EDA) of the example data<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/uruit.com\/blog\/churn-prediction-machine-learning\/#EDA_Distribution_analysis_of_Churn_and_Non-Churn_customers\" title=\"EDA: Distribution analysis of Churn and Non-Churn customers\">EDA: Distribution analysis of Churn and Non-Churn customers<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/uruit.com\/blog\/churn-prediction-machine-learning\/#EDA_Identifying_interactions_using_a_Correlation_Matrix\" title=\"EDA: Identifying interactions using a Correlation Matrix\">EDA: Identifying interactions using a Correlation Matrix<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/uruit.com\/blog\/churn-prediction-machine-learning\/#EDA_Principal_Component_Analysis_PCA\" title=\"EDA: Principal Component Analysis (PCA)\">EDA: Principal Component Analysis (PCA)<\/a><\/li><\/ul><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-2\"><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/uruit.com\/blog\/churn-prediction-machine-learning\/#Prepare_the_dataset\" title=\"Prepare the dataset\u00a0\">Prepare the dataset\u00a0<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-2\"><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/uruit.com\/blog\/churn-prediction-machine-learning\/#Customer_churn_model_training_and_evaluation\" title=\"Customer churn model training and evaluation\">Customer churn model training and evaluation<\/a><ul class=\"ez-toc-list-level-3\"><li class=\"ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/uruit.com\/blog\/churn-prediction-machine-learning\/#Precision\" title=\"Precision\">Precision<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/uruit.com\/blog\/churn-prediction-machine-learning\/#Recall\" title=\"Recall\">Recall<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-16\" href=\"https:\/\/uruit.com\/blog\/churn-prediction-machine-learning\/#Accuracy\" title=\"Accuracy\">Accuracy<\/a><\/li><\/ul><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-2\"><a class=\"ez-toc-link ez-toc-heading-17\" href=\"https:\/\/uruit.com\/blog\/churn-prediction-machine-learning\/#Feature_importance\" title=\"Feature importance\">Feature importance<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-2\"><a class=\"ez-toc-link ez-toc-heading-18\" href=\"https:\/\/uruit.com\/blog\/churn-prediction-machine-learning\/#Production\" title=\"Production\">Production<\/a><ul class=\"ez-toc-list-level-3\"><li class=\"ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-19\" href=\"https:\/\/uruit.com\/blog\/churn-prediction-machine-learning\/#Final_thoughts_on_churn_prediction_with_Machine_Learning\" title=\"Final thoughts on churn prediction with Machine Learning\">Final thoughts on churn prediction with Machine Learning<\/a><\/li><\/ul><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h1><span class=\"ez-toc-section\" id=\"How_to_avoid_customer_churn_with_a_predictive_model\"><\/span><span data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;how to avoid customer churn with a predictive model&quot;}\" data-sheets-userformat=\"{&quot;2&quot;:15165,&quot;3&quot;:{&quot;1&quot;:0},&quot;5&quot;:{&quot;1&quot;:[{&quot;1&quot;:2,&quot;2&quot;:0,&quot;5&quot;:{&quot;1&quot;:2,&quot;2&quot;:0}},{&quot;1&quot;:0,&quot;2&quot;:0,&quot;3&quot;:3},{&quot;1&quot;:1,&quot;2&quot;:0,&quot;4&quot;:1}]},&quot;6&quot;:{&quot;1&quot;:[{&quot;1&quot;:2,&quot;2&quot;:0,&quot;5&quot;:{&quot;1&quot;:2,&quot;2&quot;:0}},{&quot;1&quot;:0,&quot;2&quot;:0,&quot;3&quot;:3},{&quot;1&quot;:1,&quot;2&quot;:0,&quot;4&quot;:1}]},&quot;7&quot;:{&quot;1&quot;:[{&quot;1&quot;:2,&quot;2&quot;:0,&quot;5&quot;:{&quot;1&quot;:2,&quot;2&quot;:0}},{&quot;1&quot;:0,&quot;2&quot;:0,&quot;3&quot;:3},{&quot;1&quot;:1,&quot;2&quot;:0,&quot;4&quot;:1}]},&quot;8&quot;:{&quot;1&quot;:[{&quot;1&quot;:2,&quot;2&quot;:0,&quot;5&quot;:{&quot;1&quot;:2,&quot;2&quot;:0}},{&quot;1&quot;:0,&quot;2&quot;:0,&quot;3&quot;:3},{&quot;1&quot;:1,&quot;2&quot;:0,&quot;4&quot;:1}]},&quot;11&quot;:3,&quot;12&quot;:0,&quot;14&quot;:{&quot;1&quot;:2,&quot;2&quot;:0},&quot;15&quot;:&quot;Calibri&quot;,&quot;16&quot;:11}\">How to avoid customer churn with a predictive model<\/span><span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p><span style=\"font-weight: 400;\">In this tutorial we share how to do churn prediction using Machine Learning. You&#8217;ll will be able to create a predictive model that will help your team to predict and identify customer turnover.<\/span><\/p>\n<figure id=\"attachment_10103\" aria-describedby=\"caption-attachment-10103\" style=\"width: 640px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" class=\"size-large wp-image-10103\" src=\"https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/Churn1-1024x724.jpg\" alt=\"churn prediction machine learning\" width=\"640\" height=\"453\" srcset=\"https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/Churn1-1024x724.jpg 1024w, https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/Churn1-300x212.jpg 300w, https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/Churn1-768x543.jpg 768w, https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/Churn1-1536x1086.jpg 1536w, https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/Churn1-2048x1448.jpg 2048w, https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/Churn1-750x530.jpg 750w, https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/Churn1-1140x806.jpg 1140w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><figcaption id=\"caption-attachment-10103\" class=\"wp-caption-text\">Source: https:\/\/www.displayr.com\/<\/figcaption><\/figure>\n<p><span style=\"font-weight: 400;\">Customer attrition, turnover, defection, or churn, is the loss of clients or customers. As you can imagine, it is a critical\u00a0 metric for companies like <a href=\"https:\/\/li511-13.members.linode.com\/benefits-of-outsourcing-for-saas\/\" class=\"external\" rel=\"nofollow\">SaaS businesses<\/a> that base their value proposition on a subscription-based model. In this post we\u2019ll explain why it\u2019s an advantage to keep track and predict customer turnover. Also, we&#8217;ll share with you the steps needed to develop a predictive model. This tool will help you identify if a customer will churn based on the data.<\/span><\/p>\n<h2><span class=\"ez-toc-section\" id=\"What_is_customer_churn\"><\/span><span style=\"font-weight: 400;\">What is customer churn?<\/span><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">Customer churn is one of the most important business metrics. That&#8217;s because the cost of retaining an existing customer is significantly less than the cost of acquiring a new one. The latter is often referred to as <\/span><b>Customer Acquisition Cost<\/b><span style=\"font-weight: 400;\"> (CAC). Companies use it as a metric to track if a company has a viable business model that can keep profits generating while maintaining a low CAC .\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Historically, big companies like telephone services, internet providers, insurance firms and others performed customer attrition analysis. Nowadays it is often used by SaaS businesses and those adopting a subscription based model. According to <\/span><a href=\"https:\/\/www.profitwell.com\/blog\/average-churn-rate\" class=\"external\" rel=\"nofollow\"><span style=\"font-weight: 400;\">Profitwell analysis<\/span><\/a><span style=\"font-weight: 400;\">, the average monthly revenue churn rate could be anywhere from 1% to 17%. In addition, most studies report that the median monthly churn rate in the 5-10% range.<\/span><\/p>\n<h3><span class=\"ez-toc-section\" id=\"What_is_the_Average_Churn_Rate_for_SaaS\"><\/span><span style=\"font-weight: 400;\">What is the Average Churn Rate for SaaS?<\/span><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><span style=\"font-weight: 400;\">The analysis and predictive analysis of customer attrition for SaaS companies is extremely important! Mainly because monthly recurring revenue is the main source of return. It is crucial to track recurring profit lost by churn, customer acquisition cost and customer lifetime value, which all define how valuable a customer is.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">An important benchmark for SaaS businesses is the <\/span><b>Mythical 5%<\/b><span style=\"font-weight: 400;\"> which states that an <\/span><b>acceptable churn rate is in the 5% &#8211; 7% range annually<\/b><span style=\"font-weight: 400;\">. Simple math supports the logic behind this statement. For instance, in the case of a SaaS business with 1000 customers, the <\/span><b>5% annual <\/b><span style=\"font-weight: 400;\">churn would be a total loss of 50 customers. In contrast, a <\/span><b>5% monthly<\/b><span style=\"font-weight: 400;\"> churn would incur a loss of 460 customers annually, <\/span><b>almost half of the customer base!<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Telecom providers, financial services and insurance firms often have customer service teams to<\/span><b> winning back defecting clients. <\/b>That&#8217;s<span style=\"font-weight: 400;\">\u00a0because recovering long-term customers can be more valuable to a company than newly recruited clients.<\/span><\/p>\n<h3><span class=\"ez-toc-section\" id=\"Voluntary_vs_involuntary_turnover\"><\/span><span style=\"font-weight: 400;\">Voluntary vs involuntary turnover<\/span><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><span style=\"font-weight: 400;\">Companies usually make a distinction between voluntary churn and involuntary churn. Voluntary churn occurs due to a decision by the customer to switch to another company or service provider. On the other hand, involuntary churn occurs due to extenuating circumstances, such as a customer&#8217;s relocation to long-term care, death or relocation to a distant location. In most applications, people exclude involuntary churn from analytical models.\u00a0<\/span><\/p>\n<p><b>Analysts tend to concentrate on voluntary churn<\/b><span style=\"font-weight: 400;\">, because it typically occurs due to factors companies can control, such as how billing interactions are handled or how after-sales support is provided.<\/span><\/p>\n<h3><span class=\"ez-toc-section\" id=\"Customer_churn_prediction_using_machine_learning\"><\/span><span style=\"font-weight: 400;\">Customer churn prediction using machine learning<\/span><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><span style=\"font-weight: 400;\">Predictive analytics use churn prediction models to forecast customer churn by assessing their propensity of risk to churn. Since <\/span><b>these models generate a small prioritized list of potential defectors<\/b><span style=\"font-weight: 400;\">, they are effective at focusing customer retention programs on the customer base that is most vulnerable to churn.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">In the following sections I\u2019ll lead you through a step by step creation of a predictive model that will help your team identify customer turnover rates.<\/span><\/p>\n<h2><span class=\"ez-toc-section\" id=\"How_to_get_your_churn_prediction_using_Machine_Learning\"><\/span><span style=\"font-weight: 400;\">How to get your churn prediction using Machine Learning<\/span><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><span class=\"ez-toc-section\" id=\"Setting_the_Environment_churn_prediction_with_Kaggle\"><\/span><span style=\"font-weight: 400;\">Setting the Environment: churn prediction with Kaggle<\/span><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><span style=\"font-weight: 400;\">For this post we prepared an example available on <\/span><a href=\"https:\/\/www.kaggle.com\/d4v1d3\/churn-predictive-analysis-xgboost\" class=\"external\" rel=\"nofollow\"><span style=\"font-weight: 400;\">Kaggle<\/span><\/a><span style=\"font-weight: 400;\">. <\/span><b>Kaggle<\/b><span style=\"font-weight: 400;\"> is an open data-science platform using an environment called <\/span><b>Jupyter<\/b><span style=\"font-weight: 400;\">. Using this environment, data scientists can collaborate, inspect and transform the data, produce visualization and execute experiments. What you see on the page is often referred to as <\/span><b>Jupyter Notebook<\/b><span style=\"font-weight: 400;\"> or just <\/span><b>Notebook <\/b><span style=\"font-weight: 400;\">and it\u2019s a common data science environment. Here we can explore the data, execute code from different languages like Scala, javascript and R. In our case, we are going to use python and plot graphs to run our experiments.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">A jupyter notebook consists of cells of code that we can run just by selecting the cell and pressing the run button or by using <\/span><b>ctrl+enter<\/b><span style=\"font-weight: 400;\">.<\/span><\/p>\n<p><img loading=\"lazy\" class=\"aligncenter size-large wp-image-10104\" src=\"https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn2-1024x724.jpg\" alt=\"churn prediction machine learning\" width=\"640\" height=\"453\" srcset=\"https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn2-1024x724.jpg 1024w, https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn2-300x212.jpg 300w, https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn2-768x543.jpg 768w, https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn2-1536x1086.jpg 1536w, https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn2-2048x1448.jpg 2048w, https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn2-750x530.jpg 750w, https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn2-1140x806.jpg 1140w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/p>\n<h3><span class=\"ez-toc-section\" id=\"Exploratory_Data_Analysis_EDA_of_the_example_data\"><\/span><span style=\"font-weight: 400;\">Exploratory Data Analysis (EDA) of the example data<\/span><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><span style=\"font-weight: 400;\">As an example for this post, the dataset used for this analysis consists of customer data from a financial service institution. This data is anonymous and it&#8217;s public in the Kaggle platform. It consists of 14 columns and 10000 rows.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">We produced some initial insights trying to identify the cardinality of the data:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">high cardinality columns are <\/span><span style=\"font-weight: 400;\">columns with values that are very uncommon or unique (surname, balance).<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">low cardinality columns have very few unique values and their values are typically status flags, boolean or major classifications such as gender.<\/span><\/li>\n<\/ul>\n<p><img loading=\"lazy\" class=\"aligncenter size-full wp-image-10105\" src=\"https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn.png\" alt=\"\" width=\"860\" height=\"572\" data-wp-editing=\"1\" srcset=\"https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn.png 860w, https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn-300x200.png 300w, https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn-768x511.png 768w, https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn-750x499.png 750w, https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn-20x13.png 20w\" sizes=\"(max-width: 860px) 100vw, 860px\" \/><\/p>\n<p><span style=\"font-weight: 400;\">The departing customers have already been\u00a0 identified by the owner of the dataset and flagged as 1 in the column <\/span><b>Exited<\/b><span style=\"font-weight: 400;\">. If you have not already identified the clients who have historically churned you should do that before you continue.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Using this column we can plot a pie-chart to better illustrate the level of customer attrition in the data.<\/span><\/p>\n<p><img loading=\"lazy\" class=\"aligncenter size-large wp-image-10106\" src=\"https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn3-1024x724.jpg\" alt=\"churn prediction machine learning\" width=\"640\" height=\"453\" srcset=\"https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn3-1024x724.jpg 1024w, https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn3-300x212.jpg 300w, https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn3-768x543.jpg 768w, https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn3-1536x1086.jpg 1536w, https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn3-2048x1448.jpg 2048w, https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn3-750x530.jpg 750w, https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn3-1140x806.jpg 1140w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/p>\n<h3><span class=\"ez-toc-section\" id=\"EDA_Distribution_analysis_of_Churn_and_Non-Churn_customers\"><\/span><span style=\"font-weight: 400;\">EDA: Distribution analysis of Churn and Non-Churn customers<\/span><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><span style=\"font-weight: 400;\">As part of the Exploratory Data Analysis process, in the following step we analyzed the distributions of other variables for the <\/span><b>Leaving (Churn)<\/b><span style=\"font-weight: 400;\"> and <\/span><b>Remaining (Non Churn)<\/b><span style=\"font-weight: 400;\"> customers. This is extremely useful! It provides an insight into the data, identifying if the data contains outliers or if the dataset is unbalanced. We can now start to formulate hypotheses. Categorical data, such as gender or nationality, appears in a pie chart. On the other hand, numerical data such as credit score or balance is shown as a bar chart.<\/span><\/p>\n<pre class=\"lang:default decode:true\">import pandas as pd\r\nimport seaborn as sns#visualization\r\nimport plotly.offline as py#visualization\r\npy.init_notebook_mode(connected=True)#visualization\r\nimport plotly.graph_objs as go#visualization\r\nimport plotly.tools as tls#visualization\r\nimport plotly.figure_factory as ff#visualization\r\nimport matplotlib.pyplot as plt\r\nimport matplotlib as mpl\r\nmpl.style.use('ggplot')\r\nchurn     = df[df[\"Exited\"] == 1]\r\nnot_churn = df[df[\"Exited\"] == 0]\r\n\r\ndef plot_pie(column) :\r\n    \r\n    trace1 = go.Pie(values  = churn[column].value_counts().values.tolist(),\r\n                    labels  = churn[column].value_counts().keys().tolist(),\r\n                    hoverinfo = \"label+percent+name\",\r\n                    domain  = dict(x = [0,.48]),\r\n                    name    = \"Churn\",\r\n                    marker  = dict(line = dict(width = 2,\r\n                                               color = \"rgb(243,243,243)\")\r\n                                  ),\r\n                    hole    = .6\r\n                   )\r\n    trace2 = go.Pie(values  = not_churn[column].value_counts().values.tolist(),\r\n                    labels  = not_churn[column].value_counts().keys().tolist(),\r\n                    hoverinfo = \"label+percent+name\",\r\n                    marker  = dict(line = dict(width = 2,\r\n                                               color = \"rgb(243,243,243)\")\r\n                                  ),\r\n                    domain  = dict(x = [.52,1]),\r\n                    hole    = .6,\r\n                    name    = \"Non churn\" \r\n                   )\r\n\r\n\r\n    layout = go.Layout(dict(title = column + \" distribution in customer attrition \",\r\n                            plot_bgcolor  = \"rgb(243,243,243)\",\r\n                            paper_bgcolor = \"rgb(243,243,243)\",\r\n                            annotations = [dict(text = \"Churn\",\r\n                                                font = dict(size = 13),\r\n                                                showarrow = False,\r\n                                                x = .15, y = .5),\r\n                                           dict(text = \"Non churn\",\r\n                                                font = dict(size = 13),\r\n                                                showarrow = False,\r\n                                                x = .88,y = .5\r\n                                               )\r\n                                          ]\r\n                           )\r\n                      )\r\n    data = [trace2,trace1]\r\n    fig  = go.Figure(data = data,layout = layout)\r\n    py.iplot(fig)\r\n\r\n\r\n#function  for histogram for customer attrition types\r\ndef histogram(column) :\r\n    trace1 = go.Histogram(x  = churn[column],\r\n                          histnorm= \"percent\",\r\n                          name = \"Churn\",\r\n                          marker = dict(line = dict(width = .5,\r\n                                                    color = \"black\"\r\n                                                    )\r\n                                        ),\r\n                         opacity = .9 \r\n                         ) \r\n    \r\n    trace2 = go.Histogram(x  = not_churn[column],\r\n                          histnorm = \"percent\",\r\n                          name = \"Non churn\",\r\n                          marker = dict(line = dict(width = .5,\r\n                                              color = \"black\"\r\n                                             )\r\n                                 ),\r\n                          opacity = .9\r\n                         )\r\n    \r\n    data = [trace2,trace1]\r\n    layout = go.Layout(dict(title =column + \" distribution in customer attrition \",\r\n                            plot_bgcolor  = \"rgb(243,243,243)\",\r\n                            paper_bgcolor = \"rgb(243,243,243)\",\r\n                            xaxis = dict(gridcolor = 'rgb(255, 255, 255)',\r\n                                             title = column,\r\n                                             zerolinewidth=1,\r\n                                             ticklen=5,\r\n                                             gridwidth=2\r\n                                            ),\r\n                            yaxis = dict(gridcolor = 'rgb(255, 255, 255)',\r\n                                             title = \"percent\",\r\n                                             zerolinewidth=1,\r\n                                             ticklen=5,\r\n                                             gridwidth=2\r\n                                            ),\r\n                           )\r\n                      )\r\n    fig  = go.Figure(data=data,layout=layout)\r\n    \r\n    py.iplot(fig)\r\n    \r\n#function  for scatter plot matrix  for numerical columns in data\r\ndef scatter_matrix(df)  :\r\n    \r\n    df  = df.sort_values(by = \"Exited\" ,ascending = False)\r\n    classes = df[\"Exited\"].unique().tolist()\r\n    classes\r\n    \r\n    class_code  = {classes[k] : k for k in range(2)}\r\n    class_code\r\n\r\n    color_vals = [class_code[cl] for cl in df[\"Exited\"]]\r\n    color_vals\r\n\r\n    pl_colorscale = \"Portland\"\r\n\r\n    pl_colorscale\r\n\r\n    text = [df.loc[k,\"Exited\"] for k in range(len(df))]\r\n    text\r\n\r\n    trace = go.Splom(dimensions = [dict(label  = \"Tenure\",\r\n                                       values = df[\"Tenure\"]),\r\n                                  dict(label  = 'Balance',\r\n                                       values = df['Balance']),\r\n                                  dict(label  = 'EstimatedSalary',\r\n                                       values = df['EstimatedSalary'])],\r\n                     text = text,\r\n                     marker = dict(color = color_vals,\r\n                                   colorscale = pl_colorscale,\r\n                                   size = 3,\r\n                                   showscale = False,\r\n                                   line = dict(width = .1,\r\n                                               color='rgb(230,230,230)'\r\n                                              )\r\n                                  )\r\n                    )\r\n    axis = dict(showline  = True,\r\n                zeroline  = False,\r\n                gridcolor = \"#fff\",\r\n                ticklen   = 4\r\n               )\r\n    \r\n    layout = go.Layout(dict(title  = \r\n                            \"Scatter plot matrix for Numerical columns for customer attrition\",\r\n                            autosize = False,\r\n                            height = 800,\r\n                            width  = 800,\r\n                            dragmode = \"select\",\r\n                            hovermode = \"closest\",\r\n                            plot_bgcolor  = 'rgba(240,240,240, 0.95)',\r\n                            xaxis1 = dict(axis),\r\n                            yaxis1 = dict(axis),\r\n                            xaxis2 = dict(axis),\r\n                            yaxis2 = dict(axis),\r\n                            xaxis3 = dict(axis),\r\n                            yaxis3 = dict(axis),\r\n                           )\r\n                      )\r\n    data   = [trace]\r\n    fig = go.Figure(data = data,layout = layout )\r\n    py.iplot(fig)\r\n\r\n    \r\ncat_cols = [\"Geography\", \"Gender\", \"NumOfProducts\",\"HasCrCard\", \"IsActiveMember\"]\r\nnum_cols = [\"Age\", \"Balance\", \"EstimatedSalary\",\"CreditScore\",\"Tenure\"]\r\n#for all categorical columns plot pie\r\nfor i in cat_cols :\r\n    plot_pie(i)\r\n\r\n#for all categorical columns plot histogram    \r\nfor i in num_cols :\r\n    histogram(i)<\/pre>\n<p><img loading=\"lazy\" class=\"aligncenter size-large wp-image-10107\" src=\"https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn4-1024x724.jpg\" alt=\"churn prediction machine learning\" width=\"640\" height=\"453\" srcset=\"https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn4-1024x724.jpg 1024w, https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn4-300x212.jpg 300w, https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn4-768x543.jpg 768w, https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn4-1536x1086.jpg 1536w, https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn4-2048x1448.jpg 2048w, https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn4-750x530.jpg 750w, https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn4-1140x806.jpg 1140w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/> <img loading=\"lazy\" class=\"aligncenter size-large wp-image-10108\" src=\"https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn5-1024x724.jpg\" alt=\"churn prediction machine learning\" width=\"640\" height=\"453\" srcset=\"https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn5-1024x724.jpg 1024w, https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn5-300x212.jpg 300w, https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn5-768x543.jpg 768w, https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn5-1536x1086.jpg 1536w, https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn5-2048x1448.jpg 2048w, https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn5-750x530.jpg 750w, https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn5-1140x806.jpg 1140w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/p>\n<h3><span class=\"ez-toc-section\" id=\"EDA_Identifying_interactions_using_a_Correlation_Matrix\"><\/span><span style=\"font-weight: 400;\">EDA: Identifying interactions using a Correlation Matrix<\/span><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><span style=\"font-weight: 400;\">A correlation matrix is used to visualize the correlations between each column in the dataset.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">As we can see from the first row, the <\/span><b>Exited<\/b><span style=\"font-weight: 400;\"> column has a positive correlation with <\/span><b>Age<\/b><span style=\"font-weight: 400;\"> and <\/span><b>Balance<\/b><span style=\"font-weight: 400;\">, and a negative correlation with <\/span><b>IsActiveMember<\/b><span style=\"font-weight: 400;\"> and <\/span><b>NumberOfProducts.<\/b><\/p>\n<p><span style=\"font-weight: 400;\">This analysis helps us formulate a hypothesis concerning\u00a0 which are the most important features for our problem.<\/span><\/p>\n<pre class=\"lang:default decode:true \">correlation = df.corr()\r\n#tick labels\r\nmatrix_cols = correlation.columns.tolist()\r\n#convert to array\r\ncorr_array  = np.array(correlation)\r\n\r\n#Plotting\r\ntrace = go.Heatmap(z = corr_array,\r\n                   x = matrix_cols,\r\n                   y = matrix_cols,\r\n                   colorscale = \"Viridis\",\r\n                   colorbar   = dict(title = \"Pearson Correlation coefficient\",\r\n                                     titleside = \"right\"\r\n                                    ) ,\r\n                  )\r\n\r\nlayout = go.Layout(dict(title = \"Correlation Matrix for variables\",\r\n                        autosize = False,\r\n                        height  = 720,\r\n                        width   = 800,\r\n                        margin  = dict(r = 0 ,l = 210,\r\n                                       t = 25,b = 210,\r\n                                      ),\r\n                        yaxis   = dict(tickfont = dict(size = 9)),\r\n                        xaxis   = dict(tickfont = dict(size = 9))\r\n                       )\r\n                  )\r\n\r\ndata = [trace]\r\nfig = go.Figure(data=data,layout=layout)\r\npy.iplot(fig)<\/pre>\n<p><img loading=\"lazy\" class=\"aligncenter size-large wp-image-10109\" src=\"https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn6-1024x724.jpg\" alt=\"churn prediction machine learning\" width=\"640\" height=\"453\" srcset=\"https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn6-1024x724.jpg 1024w, https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn6-300x212.jpg 300w, https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn6-768x543.jpg 768w, https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn6-1536x1086.jpg 1536w, https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn6-2048x1448.jpg 2048w, https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn6-750x530.jpg 750w, https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn6-1140x806.jpg 1140w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/p>\n<h3><span class=\"ez-toc-section\" id=\"EDA_Principal_Component_Analysis_PCA\"><\/span><span style=\"font-weight: 400;\">EDA: Principal Component Analysis (PCA)<\/span><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><span style=\"font-weight: 400;\">PCA is an unsupervised learning technique\u00a0 for identifying patterns and clusters, and reducing the dimensionality of a dataset.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Considering that our dataset has 14 columns of which 3 are for identification (<\/span><b>RowNumber, CustomerId, Surname<\/b><span style=\"font-weight: 400;\">)<\/span> <span style=\"font-weight: 400;\">and 1 is the value we want to predict (<\/span><b>Exited<\/b><span style=\"font-weight: 400;\">)<\/span> <span style=\"font-weight: 400;\">we are left with 9 columns.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Visualizing something using 9 dimensions is quite difficult. Therefore, we can simplify this complexity by reducing the dimensionality of the dataset using PCA.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">From the visualizations we can understand that there\u2019s not a clear linear separation between the Churn and Non-Churn customers. This allows us to exclude linear classifiers from our experiment.<\/span><\/p>\n<pre class=\"lang:default decode:true \">from sklearn.decomposition import PCA\r\nfrom sklearn.preprocessing import scale, normalize\r\n\r\npca = PCA(n_components = 2)\r\nId_col = ['RowNumber', 'CustomerId', 'Surname']\r\ntarget_col = [\"Exited\"]\r\nX = df[[i for i in df.columns if i not in Id_col + target_col]]\r\nXscal = scale(X)\r\nXnorm = normalize(X)\r\nY = df[target_col + Id_col]\r\n\r\n\r\ndef plot(X,Y, title):\r\n\r\n    principal_components = pca.fit_transform(X)\r\n    pca_data = pd.DataFrame(principal_components,columns = [\"PC1\",\"PC2\"])\r\n    pca_data = pca_data.merge(Y,left_index=True,right_index=True,how=\"left\")\r\n    pca_data[\"Churn\"] = pca_data[\"Exited\"].replace({1:\"Churn\",0:\"Not Churn\"})\r\n\r\n    \r\n    def pca_scatter(target,color) :\r\n        tracer = go.Scatter(x = pca_data[pca_data[\"Churn\"] == target][\"PC1\"] ,\r\n                            y = pca_data[pca_data[\"Churn\"] == target][\"PC2\"],\r\n                            name = target,mode = \"markers\",\r\n                            marker = dict(color = color,\r\n                                          line = dict(width = .5),\r\n                                          symbol =  \"diamond-open\"),\r\n                            text = (\"Customer Id : \" + \r\n                                    pca_data[pca_data[\"Churn\"] == target]['Surname'])\r\n                           )\r\n        return tracer\r\n\r\n    layout = go.Layout(dict(title = title,\r\n                            plot_bgcolor  = \"rgb(243,243,243)\",\r\n                            paper_bgcolor = \"rgb(243,243,243)\",\r\n                            xaxis = dict(gridcolor = 'rgb(255, 255, 255)',\r\n                                         title = \"principal component 1\",\r\n                                         zerolinewidth=1,ticklen=5,gridwidth=2),\r\n                            yaxis = dict(gridcolor = 'rgb(255, 255, 255)',\r\n                                         title = \"principal component 2\",\r\n                                         zerolinewidth=1,ticklen=5,gridwidth=2),\r\n                            height = 600\r\n                           )\r\n                      )\r\n    trace1 = pca_scatter(\"Churn\",'red')\r\n    trace2 = pca_scatter(\"Not Churn\",'royalblue')\r\n    data = [trace2,trace1]\r\n    fig = go.Figure(data=data,layout=layout)\r\n    py.iplot(fig)\r\n\r\nplot(X,Y, \"Visualizing data with Principal Component Analysis on raw data\")\r\nplot(Xnorm,Y, \"Visualizing data with Principal Component Analysis on normalized data\")\r\nplot(Xscal,Y, \"Visualizing data with Principal Component Analysis on scaled data\")<\/pre>\n<p><img loading=\"lazy\" class=\"aligncenter size-large wp-image-10110\" src=\"https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn7-1024x724.jpg\" alt=\"churn prediction machine learning\" width=\"640\" height=\"453\" srcset=\"https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn7-1024x724.jpg 1024w, https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn7-300x212.jpg 300w, https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn7-768x543.jpg 768w, https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn7-1536x1086.jpg 1536w, https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn7-2048x1448.jpg 2048w, https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn7-750x530.jpg 750w, https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn7-1140x806.jpg 1140w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/p>\n<h2><span class=\"ez-toc-section\" id=\"Prepare_the_dataset\"><\/span><span style=\"font-weight: 400;\">Prepare the dataset\u00a0<\/span><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">In this phase we will prepare our dataset for training. Generally speaking in each ML or DL project we need to select relevant features. Therefore, we can create a training set and an evaluation set that will be used by our model to learn and understand patterns from the data. Here we will select the columns needed for the training process and split our dataset into 2 sets, a training set and a testing set.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">The training data will be used by the model to fit the parameters during the training (learning) process. The test data will be used to evaluate the performance of the model on unknown data.<\/span><\/p>\n<pre class=\"lang:default decode:true \">X = df[['CreditScore', 'Geography','Gender', 'Age', 'Tenure', 'Balance', 'NumOfProducts', 'HasCrCard','IsActiveMember']]\r\ny = df[\"Exited\"]\r\nfrom sklearn.model_selection import train_test_split\r\nX_train, X_test, Y_train, Y_test = train_test_split(X, y, test_size=0.20, random_state=42)<\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Customer_churn_model_training_and_evaluation\"><\/span><span style=\"font-weight: 400;\">Customer churn model training and evaluation<\/span><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">For this problem we will use a gradient boosting technique called <\/span><a href=\"https:\/\/xgboost.ai\/\" class=\"external\" rel=\"nofollow\"><b>xgboost<\/b><\/a><b>.<\/b><\/p>\n<p><span style=\"font-weight: 400;\">XGBoost is an optimized distributed gradient boosting library designed to be highly efficient, flexible and portable. It implements <a href=\"https:\/\/uruit.com\/machine-learning-consulting\/\">machine learning<\/a> algorithms under the Gradient Boosting framework. XGBoost provides a parallel tree boosting (also known as GBDT, GBM) that solves many data science problems quickly and accurately. The same code can run on distributed environments such as Hadoop, SGE, MPI <\/span><span style=\"font-weight: 400;\">and can solve problems that contain billions of examples<\/span><\/p>\n<p><span style=\"font-weight: 400;\">In this section we define the model hyperparameters and convert the datasets to use with xgboost.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">We run the training process for 60 epochs, and evaluate against the testing set using the following metrics:<\/span><\/p>\n<h3><span class=\"ez-toc-section\" id=\"Precision\"><\/span><span style=\"font-weight: 400;\">Precision<\/span><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><span style=\"font-weight: 400;\">This metric evaluates how precise a model is in predicting positive labels. It answers the question, out of the number of times a model predicted positive, how often was it correct?\u00a0<\/span><\/p>\n<p><img loading=\"lazy\" class=\"aligncenter size-large wp-image-10111\" src=\"https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn8-1024x291.jpg\" alt=\"churn prediction machine learning\" width=\"640\" height=\"182\" srcset=\"https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn8-1024x291.jpg 1024w, https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn8-300x85.jpg 300w, https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn8-768x219.jpg 768w, https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn8-1536x437.jpg 1536w, https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn8-2048x583.jpg 2048w, https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn8-750x213.jpg 750w, https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn8-1140x324.jpg 1140w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/p>\n<h3><span class=\"ez-toc-section\" id=\"Recall\"><\/span><span style=\"font-weight: 400;\">Recall<\/span><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><span style=\"font-weight: 400;\">Often called sensitivity, the recall calculates the percentage of actual positives a model correctly identified (True Positive).<\/span><\/p>\n<p><img loading=\"lazy\" class=\"aligncenter size-large wp-image-10112\" src=\"https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn9-1024x291.jpg\" alt=\"churn prediction machine learning\" width=\"640\" height=\"182\" srcset=\"https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn9-1024x291.jpg 1024w, https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn9-300x85.jpg 300w, https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn9-768x219.jpg 768w, https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn9-1536x437.jpg 1536w, https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn9-2048x583.jpg 2048w, https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn9-750x213.jpg 750w, https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn9-1140x324.jpg 1140w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/p>\n<h3><span class=\"ez-toc-section\" id=\"Accuracy\"><\/span><span style=\"font-weight: 400;\">Accuracy<\/span><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><span style=\"font-weight: 400;\">Accuracy is an evaluation metric that allows you to measure the total number of predictions a model gets right.\u00a0<\/span><\/p>\n<p><img loading=\"lazy\" class=\"aligncenter size-large wp-image-10113\" src=\"https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn10-1024x291.jpg\" alt=\"churn prediction machine learning\" width=\"640\" height=\"182\" srcset=\"https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn10-1024x291.jpg 1024w, https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn10-300x85.jpg 300w, https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn10-768x219.jpg 768w, https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn10-1536x437.jpg 1536w, https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn10-2048x583.jpg 2048w, https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn10-750x213.jpg 750w, https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn10-1140x324.jpg 1140w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/p>\n<pre class=\"lang:default decode:true \">import numpy as np\r\nfrom sklearn.metrics import precision_score, recall_score, accuracy_score\r\n\r\npreds = model.predict(D_test)\r\nbest_preds = np.asarray([np.argmax(line) for line in preds])\r\n\r\nprint(\"Precision = {}\".format(precision_score(Y_test, best_preds, average='macro')))\r\nprint(\"Recall = {}\".format(recall_score(Y_test, best_preds, average='macro')))\r\nprint(\"Accuracy = {}\".format(accuracy_score(Y_test, best_preds)))<\/pre>\n<p><img loading=\"lazy\" class=\"aligncenter size-full wp-image-10114\" src=\"https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn2.png\" alt=\"\" width=\"855\" height=\"152\" srcset=\"https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn2.png 855w, https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn2-300x53.png 300w, https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn2-768x137.png 768w, https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn2-750x133.png 750w, https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn2-20x4.png 20w\" sizes=\"(max-width: 855px) 100vw, 855px\" \/><\/p>\n<p><span style=\"font-weight: 400;\">Good job! Our initial model has an accuracy score of 87.30%, precision of 83.01% and a sensitivity of 73.25%. Great news!<\/span><\/p>\n<h2><span class=\"ez-toc-section\" id=\"Feature_importance\"><\/span><span style=\"font-weight: 400;\">Feature importance<\/span><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">One of the characteristics of Xgboost is the ability to understand what are the most important features in the dataset.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">We can do this by plotting the feature importance plot. That way, we can verify if our hypothesis from the data analysis section is correct. <\/span><\/p>\n<pre class=\"lang:default decode:true \"># Feature importance\r\nfrom xgboost import plot_importance\r\nplot_importance(model)<\/pre>\n<p><img loading=\"lazy\" class=\"aligncenter size-large wp-image-10119\" src=\"https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn12-1024x724.jpg\" alt=\"\" width=\"640\" height=\"453\" srcset=\"https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn12-1024x724.jpg 1024w, https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn12-300x212.jpg 300w, https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn12-768x543.jpg 768w, https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn12-1536x1086.jpg 1536w, https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn12-2048x1448.jpg 2048w, https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn12-750x530.jpg 750w, https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn12-1140x806.jpg 1140w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/p>\n<p><span style=\"font-weight: 400;\">We predicted that<\/span><b> Age<\/b><span style=\"font-weight: 400;\">, <\/span><b>Balance, IsActiveMember and NumberOfProducts<\/b><span style=\"font-weight: 400;\"> were the most important columns and we were right for <\/span><b>Balance<\/b><span style=\"font-weight: 400;\"> and <\/span><b>Age <\/b><span style=\"font-weight: 400;\">and partially right on <\/span><b>NumOfProducts <\/b><span style=\"font-weight: 400;\">but we did not see any evidence of the importance of <\/span><b>CreditScore <\/b><span style=\"font-weight: 400;\">or <\/span><b>Tenure.\u00a0<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Using these findings we can iterate over the data preparation \/ training \/ evaluation to optimize the performance of our model. In this case we\u2019re working with a small amount of data. However, in a production environment we could have hundreds of columns describing a client. Therefore, identifying which columns are those describing the problem will be key for the development of a reliable and accurate model.<\/span><\/p>\n<h2><span class=\"ez-toc-section\" id=\"Production\"><\/span><span style=\"font-weight: 400;\">Production<\/span><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">Now that we have our predictive model and can successfully identify if a customer will churn, the next step is to run it in production.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">In order to do this we\u2019re going to run the model on a daily schedule using the latest available data. Remember to apply the same transformations used to generate the training dataset.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">If we analyze the prediction result from\u00a0 the model we can maximize the information obtained.<\/span><\/p>\n<p><img loading=\"lazy\" class=\"aligncenter size-full wp-image-10115\" src=\"https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn3.png\" alt=\"churn prediction machine learning\" width=\"898\" height=\"463\" srcset=\"https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn3.png 898w, https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn3-300x155.png 300w, https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn3-768x396.png 768w, https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn3-750x387.png 750w, https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn3-20x10.png 20w\" sizes=\"(max-width: 898px) 100vw, 898px\" \/><\/p>\n<p><img loading=\"lazy\" class=\"aligncenter size-full wp-image-10116\" src=\"https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn4.png\" alt=\"churn prediction machine learning\" width=\"519\" height=\"252\" srcset=\"https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn4.png 519w, https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn4-300x146.png 300w, https:\/\/uruit.com\/blog\/wp-content\/uploads\/2020\/11\/churn4-20x10.png 20w\" sizes=\"(max-width: 519px) 100vw, 519px\" \/><\/p>\n<p><span style=\"font-weight: 400;\">In the last cell we printed out the prediction matrix from the model. Every element of the array <\/span><b>preds <\/b><span style=\"font-weight: 400;\">describes the probabilities of a customer to be <\/span><b>churn<\/b><span style=\"font-weight: 400;\"> or <\/span><b>not-churn<\/b><span style=\"font-weight: 400;\">. In the example above, the first customer has a 96.23% probability to be <\/span><b>not-churn <\/b><span style=\"font-weight: 400;\">and a 3.76% to be <\/span><b>churn, <\/b><span style=\"font-weight: 400;\">therefore we can state that this customer is a <\/span><b>non-churning<\/b><span style=\"font-weight: 400;\"> customer. The fourth customer in contrast has a 20.06% probability to be <\/span><b>not-churn<\/b><span style=\"font-weight: 400;\"> and a 79.93% to be <\/span><b>churn<\/b><span style=\"font-weight: 400;\"> indicating he\/she is a <\/span><b>churn <\/b><span style=\"font-weight: 400;\">customer.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Using this data we can also identify situations where we are not really sure if a customer will be churning or not, like the last customer on the list, where the <\/span><b>churn <\/b><span style=\"font-weight: 400;\">and <\/span><b>not-churn <\/b><span style=\"font-weight: 400;\">probabilities are very similar (0.59% <\/span><b>not-churn<\/b><span style=\"font-weight: 400;\">, 0.40% c<\/span><b>hurn<\/b><span style=\"font-weight: 400;\">). During this stage we should also identify a threshold value that we can use to determine if a customer is <\/span><b>churn<\/b><span style=\"font-weight: 400;\"> or <\/span><b>not-churn<\/b><span style=\"font-weight: 400;\">.<\/span><\/p>\n<h3><span class=\"ez-toc-section\" id=\"Final_thoughts_on_churn_prediction_with_Machine_Learning\"><\/span>Final thoughts on churn prediction with Machine Learning<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><span style=\"font-weight: 400;\">In production we can generate a daily report of future churning customers and this report can be sent directly to the <\/span><b>customer service team <\/b><span style=\"font-weight: 400;\">who can then contact customers on the list to better understand their needs or propose new offers, different products or whatever \u2018win-back strategy\u2019 is in place.\u00a0\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">To sum up, in this post we showcased churn prediction with Machine Learning by creating a predictive model to identify customer churn. We specifically used a dataset from a financial service firm. However, regardless what industry you\u2019re in, or your strategy to mitigate customer churn, you can stay proactive and anticipate your customer\u2019s next move based on this type of analysis. As with anything in life and business, time is of the essence.<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Contents How to avoid customer churn with a predictive modelWhat is customer churn?What is the Average Churn Rate for SaaS?Voluntary vs involuntary turnoverCustomer churn prediction using machine learningHow to get&#8230;<\/p>\n","protected":false},"author":35,"featured_media":10102,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[290],"tags":[],"_links":{"self":[{"href":"https:\/\/uruit.com\/blog\/wp-json\/wp\/v2\/posts\/10093"}],"collection":[{"href":"https:\/\/uruit.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/uruit.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/uruit.com\/blog\/wp-json\/wp\/v2\/users\/35"}],"replies":[{"embeddable":true,"href":"https:\/\/uruit.com\/blog\/wp-json\/wp\/v2\/comments?post=10093"}],"version-history":[{"count":6,"href":"https:\/\/uruit.com\/blog\/wp-json\/wp\/v2\/posts\/10093\/revisions"}],"predecessor-version":[{"id":11287,"href":"https:\/\/uruit.com\/blog\/wp-json\/wp\/v2\/posts\/10093\/revisions\/11287"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/uruit.com\/blog\/wp-json\/wp\/v2\/media\/10102"}],"wp:attachment":[{"href":"https:\/\/uruit.com\/blog\/wp-json\/wp\/v2\/media?parent=10093"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/uruit.com\/blog\/wp-json\/wp\/v2\/categories?post=10093"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/uruit.com\/blog\/wp-json\/wp\/v2\/tags?post=10093"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}