<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/customer-analytics-using-deep-learning-with-keras-to-predict-customer-churn/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.business-science.io/business/2017/11/28/customer_churn_analysis_keras.html"> business-science.io - Articles</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><strong>Customer churn is a problem that all companies need to monitor, especially those that depend on subscription-based revenue streams</strong>. The simple fact is that most organizations have data that can be used to target these individuals and to understand the key drivers of churn, and <strong>we now have Keras for Deep Learning available in R (Yes, in R!!), which predicted customer churn with 82% accuracy</strong>. We’re super excited for this article because we are using the new <a rel="nofollow" href="https://tensorflow.rstudio.com/keras/" target="_blank"><code>keras</code></a> package to produce an <strong>Artificial Neural Network (ANN)</strong> model on the <a rel="nofollow" href="https://www.ibm.com/communities/analytics/watson-analytics-blog/predictive-insights-in-the-telco-customer-churn-data-set/" target="_blank">IBM Watson Telco Customer Churn Data Set</a>! As for most business problems, it’s equally important to <strong>explain what features drive the model</strong>, which is why we’ll use the <a rel="nofollow" href="https://github.com/thomasp85/lime" target="_blank"><code>lime</code></a> package for explainability. We cross-checked the LIME results with a Correlation Analysis using the <a rel="nofollow" href="https://github.com/drsimonj/corrr" target="_blank"><code>corrr</code></a> package. We’re not done yet. In addition, we use <strong>three new packages to assist with Machine Learning (ML)</strong>: <a rel="nofollow" href="https://topepo.github.io/recipes" target="_blank"><code>recipes</code></a> for preprocessing, <a rel="nofollow" href="https://topepo.github.io/rsample/" target="_blank"><code>rsample</code></a> for sampling data and <a rel="nofollow" href="https://github.com/topepo/yardstick" target="_blank"><code>yardstick</code></a> for model metrics. These are relatively new additions to CRAN developed by <a rel="nofollow" href="https://github.com/topepo" target="_blank">Max Kuhn</a> at RStudio (creator of the <a rel="nofollow" href="http://topepo.github.io/caret/index.html" target="_blank"><code>caret</code></a> package). It seems that <em>R is quickly developing ML tools that rival Python</em>. Good news if you’re interested in applying Deep Learning in R! We are so let’s get going!!</p>
<h2>Customer Churn: Hurts Sales, Hurts Company</h2>
<p>Customer churn refers to the situation when a customer ends their relationship with a company, and it’s a costly problem. Customers are the fuel that powers a business. Loss of customers impacts sales. Further, it’s much more difficult and costly to gain new customers than it is to retain existing customers. As a result, <strong>organizations need to focus on reducing customer churn</strong>.</p>
<p>The good news is that <strong>machine learning can help</strong>. For many businesses that offer subscription based services, it’s critical to both predict customer churn and explain what features relate to customer churn. Older techniques such as logistic regression can be less accurate than newer techniques such as deep learning, which is why <strong>we are going to show you how to model an ANN in R with the <a rel="nofollow" href="https://tensorflow.rstudio.com/keras/" target="_blank"><code>keras</code></a> package</strong>.</p>
<h2>Churn Modeling With Artificial Neural Networks (Keras)</h2>
<p>Artificial Neural Networks (ANN) are now a staple within the sub-field of Machine Learning called Deep Learning. <strong>Deep learning algorithms can be vastly superior to traditional regression and classification methods</strong> (e.g. linear and logistic regression) because of the ability to model interactions between features that would otherwise go undetected. The challenge becomes explainability, which is often needed to support the business case. The good news is we get the best of both worlds with <code>keras</code> and <code>lime</code>.</p>
<h3>IBM Watson Dataset (Where We Got The Data)</h3>
<p>The dataset used for this tutorial is <a rel="nofollow" href="https://www.ibm.com/communities/analytics/watson-analytics-blog/predictive-insights-in-the-telco-customer-churn-data-set/" target="_blank">IBM Watson Telco Dataset</a>. According to IBM, the business challenge is…</p>
<blockquote>
<p>A telecommunications company [Telco] is concerned about the number of customers leaving their landline business for cable competitors. They need to understand who is leaving. Imagine that you’re an analyst at this company and you have to find out who is leaving and why.</p>
</blockquote>
<p>The dataset includes information about:</p>
<ul>
<li><strong>Customers who left within the last month</strong>: The column is called Churn</li>
<li><strong>Services that each customer has signed up for</strong>: phone, multiple lines, internet, online security, online backup, device protection, tech support, and streaming TV and movies</li>
<li><strong>Customer account information</strong>: how long they’ve been a customer, contract, payment method, paperless billing, monthly charges, and total charges</li>
<li><strong>Demographic info about customers</strong>: gender, age range, and if they have partners and dependents</li>
</ul>
<h3>Deep Learning With Keras (What We Did With The Data)</h3>
<p>In this example we show you how to use <a rel="nofollow" href="https://tensorflow.rstudio.com/keras/" target="_blank"><code>keras</code></a> to develop a sophisticated and highly accurate deep learning model in R. We walk you through the preprocessing steps, investing time into how to format the data for Keras. We inspect the various classification metrics, and show that <strong>an un-tuned ANN model can easily get 82% accuracy on the unseen data</strong>. Here’s the deep learning training history visualization.</p>
<p><img alt="Deep Learning Training History" src="https://i0.wp.com/www.business-science.io/assets/customer_churn_analysis.png?w=456" data-recalc-dims="1"></p>
<p>We have some fun with preprocessing the data (<em>yes, preprocessing can actually be fun and easy!</em>). We use the new <a rel="nofollow" href="https://topepo.github.io/recipes" target="_blank"><code>recipes</code></a> package to simplify the preprocessing workflow.</p>
<p>We end by showing you how to explain the ANN with the <a rel="nofollow" href="https://github.com/thomasp85/lime" target="_blank"><code>lime</code></a> package. <strong>Neural networks used to be frowned upon because of the “black box” nature</strong> meaning these sophisticated models (ANNs are highly accurate) are difficult to explain using traditional methods. <strong>Not no more with LIME!</strong> Here’s the feature importance visualization.</p>
<p><img alt="LIME Feature Importance" src="https://i1.wp.com/www.business-science.io/assets/customer_churn_analysis_lime.png?w=456" data-recalc-dims="1"></p>
<p>We also cross-checked the LIME results with a <strong>Correlation Analysis</strong> using the <a rel="nofollow" href="https://github.com/drsimonj/corrr" target="_blank"><code>corrr</code></a> package. Here’s the correlation visualization.</p>
<p><img alt="Correltion analysis" src="https://i0.wp.com/www.business-science.io/assets/customer_churn_analysis_corrr.png?w=456" data-recalc-dims="1"></p>
<p>We even built an <strong>ML-Powered Interactive PowerBI Web Application</strong> with a <strong>Customer Scorecard</strong> to monitor customer churn risk and to make recommendations on how to improve customer health! Feel free to take it for a spin.</p>
<p><em>View in Full Screen Mode for best experience</em></p>
<p><iframe width="100%" allowfullscreen="true" src="https://app.powerbi.com/view?r=eyJrIjoiNzRhNGI4M2YtZTZhNy00ZjUwLTllM2MtOWVkNDVmOWMzM2I0IiwidCI6IjkxYzM2OWI1LTFjOWUtNDM5Yy05ODljLTE4NjdlYzYwNjYwMyIsImMiOjZ9" height="600"></iframe></p>
<h3>Credits</h3>
<p>We saw that just last week the same Telco customer churn dataset was used in the article, <a rel="nofollow" href="https://datascienceplus.com/predict-customer-churn-logistic-regression-decision-tree-and-random-forest/" target="_blank">Predict Customer Churn – Logistic Regression, Decision Tree and Random Forest</a>. We thought the article was excellent.</p>
<p>This article takes a different approach with Keras, LIME, Correlation Analysis, and a few other cutting edge packages. We encourage the readers to check out both articles because, although the problem is the same, both solutions are beneficial to those learning data science and advanced modeling.</p>
<h2>Prerequisites</h2>
<p>We use the following libraries in this tutorial:</p>
<ul>
<li><a rel="nofollow" href="https://tensorflow.rstudio.com/keras/" target="_blank"><code>keras</code></a>: Library that ports Keras from Python enabling deep learning in R. Visit the <a rel="nofollow" href="https://tensorflow.rstudio.com/keras/" target="_blank">documentation</a> for more information.</li>
<li><a rel="nofollow" href="https://github.com/thomasp85/lime" target="_blank"><code>lime</code></a>: Used to explain the predictions of black box classifiers. Deep Learning falls into this category.</li>
<li><a rel="nofollow" href="https://business-science.github.io/tidyquant/" target="_blank"><code>tidyquant</code></a>: Loads the <a rel="nofollow" href="https://www.tidyverse.org/" target="_blank"><code>tidyverse</code></a> (<a rel="nofollow" href="http://dplyr.tidyverse.org/" target="_blank"><code>dplyr</code></a>, <a rel="nofollow" href="http://ggplot2.tidyverse.org/" target="_blank"><code>ggplot2</code></a>, etc) and has nice visualization functions with <a rel="nofollow" href="https://business-science.github.io/tidyquant/reference/theme_tq.html" target="_blank"><code>theme_tq()</code></a>. Visit the <a rel="nofollow" href="https://business-science.github.io/tidyquant/" target="_blank">tidyquant documentation</a> and the <a rel="nofollow" href="https://www.tidyverse.org/" target="_blank">tidyverse documentation</a> for more information on the individual packages.</li>
<li><a rel="nofollow" href="https://topepo.github.io/rsample/" target="_blank"><code>rsample</code></a>: New package for generating resamples. Visit the <a rel="nofollow" href="https://topepo.github.io/rsample/" target="_blank">documentation</a> for more information.</li>
<li><a rel="nofollow" href="https://topepo.github.io/recipes" target="_blank"><code>recipes</code></a>: New package for preprocessing machine learning data sets. Visit the <a rel="nofollow" href="https://topepo.github.io/recipes/" target="_blank">documentation</a> for more information.</li>
<li><a rel="nofollow" href="https://github.com/topepo/yardstick" target="_blank"><code>yardstick</code></a>: Tidy methods for measuring model performance. Visit the <a rel="nofollow" href="https://github.com/topepo/yardstick" target="_blank">GitHub Page</a> for more information.</li>
<li><a rel="nofollow" href="https://github.com/drsimonj/corrr" target="_blank"><code>corrr</code></a>: Tidy methods for correlation. Visit the <a rel="nofollow" href="https://github.com/drsimonj/corrr" target="_blank">GitHub Page</a> for more information.</li>
</ul>
<p>Install the following packages with <code>install.packages()</code>.</p>
<figure>
<pre><code data-lang="r"><span>pkgs</span><span> </span><span>&lt;-</span><span> </span><span>c</span><span>(</span><span>&quot;keras&quot;</span><span>,</span><span> </span><span>&quot;lime&quot;</span><span>,</span><span> </span><span>&quot;tidyquant&quot;</span><span>,</span><span> </span><span>&quot;rsample&quot;</span><span>,</span><span> </span><span>&quot;recipes&quot;</span><span>,</span><span> </span><span>&quot;yardstick&quot;</span><span>,</span><span> </span><span>&quot;corrr&quot;</span><span>)</span><span>
</span><span>install.packages</span><span>(</span><span>pkgs</span><span>)</span></code></pre>
</figure>
<h2>Load Libraries</h2>
<p>Load the libraries.</p>
<figure>
<pre><code data-lang="r"><span># Load libraries</span><span>
</span><span>library</span><span>(</span><span>keras</span><span>)</span><span>
</span><span>library</span><span>(</span><span>lime</span><span>)</span><span>
</span><span>library</span><span>(</span><span>tidyquant</span><span>)</span><span>
</span><span>library</span><span>(</span><span>rsample</span><span>)</span><span>
</span><span>library</span><span>(</span><span>recipes</span><span>)</span><span>
</span><span>library</span><span>(</span><span>yardstick</span><span>)</span><span>
</span><span>library</span><span>(</span><span>corrr</span><span>)</span></code></pre>
</figure>
<p>If you have not previously run Keras in R, you will need to install Keras using the <a rel="nofollow" href="https://tensorflow.rstudio.com/keras/reference/install_keras.html" target="_blank"><code>install_keras()</code></a> function.</p>
<figure>
<pre><code data-lang="r"><span># Install Keras if you have not installed before</span><span>
</span><span>install_keras</span><span>()</span></code></pre>
</figure>
<h2>Import Data</h2>
<p>Download the <a rel="nofollow" href="https://www.ibm.com/communities/analytics/watson-analytics-blog/predictive-insights-in-the-telco-customer-churn-data-set/" target="_blank">IBM Watson Telco Data Set here</a>. Next, use <code>read_csv()</code> to import the data into a nice tidy data frame. We use the <a rel="nofollow" href="http://tibble.tidyverse.org/reference/glimpse.html" target="_blank"><code>glimpse()</code></a> function to quickly inspect the data. We have the target “Churn” and all other variables are potential predictors. The raw data set needs to be cleaned and preprocessed for ML.</p>
<figure>
<pre><code data-lang="r"><span># Import data</span><span>
</span><span>churn_data_raw</span><span> </span><span>&lt;-</span><span> </span><span>read_csv</span><span>(</span><span>&quot;WA_Fn-UseC_-Telco-Customer-Churn.csv&quot;</span><span>)</span><span>
</span><span>glimpse</span><span>(</span><span>churn_data_raw</span><span>)</span></code></pre>
</figure>
<figure>
<pre><code data-lang="text">## Observations: 7,043
## Variables: 21
## $ customerID       </code></pre></figure>