<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/code-for-case-study-customer-churn-with-keras-tensorflow-and-h2o/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://shirinsplayground.netlify.com/2018/12/customer_churn_code/"> Shirin's playgRound</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>This is code that accompanies a <a rel="nofollow" href="https://www.dpunkt.de/buecher/13208/9783864906107-data-science.html" target="_blank">book</a> chapter on customer churn that I have written for the German dpunkt Verlag. The book is in German and will probably appear in February: <a rel="nofollow" href="https://www.dpunkt.de/buecher/13208/9783864906107-data-science.html" target="_blank">https://www.dpunkt.de/buecher/13208/9783864906107-data-science.html</a>.</p>
<p>The code you find below can be used to recreate all figures and analyses from this book chapter. Because the content is exclusively for the book, my descriptions around the code had to be minimal. But I’m sure, you can get the gist, even without the book. <img alt="😉" src="https://s.w.org/images/core/emoji/2.4/72x72/1f609.png" class="wp-smiley"></p>
<div>
<h1>Inspiration &amp; Sources</h1>
<p>Thank you to the following people for providing excellent code examples about customer churn:</p>
<ul>
<li>Matt Dancho: <a rel="nofollow" href="http://www.business-science.io/business/2017/11/28/customer_churn_analysis_keras.html" target="_blank">http://www.business-science.io/business/2017/11/28/customer_churn_analysis_keras.html</a></li>
<li>JJ Allaire: <a rel="nofollow" href="https://github.com/rstudio/keras-customer-churn" target="_blank">https://github.com/rstudio/keras-customer-churn</a></li>
<li>Susan Li: <a rel="nofollow" href="https://towardsdatascience.com/predict-customer-churn-with-r-9e62357d47b4" target="_blank">https://towardsdatascience.com/predict-customer-churn-with-r-9e62357d47b4</a></li>
<li>John Sullivan: <a rel="nofollow" href="https://jtsulliv.github.io/churn-eda/" target="_blank">https://jtsulliv.github.io/churn-eda/</a></li>
</ul>
</div>
<div>
<h1>Setup</h1>
<p>All analyses are done in R using RStudio. For detailed session information including R version, operating system and package versions, see the <code>sessionInfo()</code> output at the end of this document.</p>
<p>All figures are produced with ggplot2.</p>
<ul>
<li>Libraries</li>
</ul>
<pre><code># Load libraries
library(tidyverse) # for tidy data analysis
library(readr)     # for fast reading of input files
library(caret)     # for convenient splitting
library(mice)      # mice package for Multivariate Imputation by Chained Equations (MICE)
library(keras)     # for neural nets
library(lime)      # for explaining neural nets
library(rsample)   # for splitting training and test data
library(recipes)   # for preprocessing
library(yardstick) # for evaluation
library(ggthemes)  # for additional plotting themes
library(corrplot)  # for correlation

theme_set(theme_minimal())</code></pre>
<pre><code># Install Keras if you have not installed it before
# follow instructions if you haven't installed TensorFlow
install_keras()</code></pre>
<p></p>
</div>
<div>
<h1>Data preparation</h1>
<div>
<h2>The dataset</h2>
<p>The Telco Customer Churn data set is the same one that Matt Dancho used in his post (see above). It was downloaded from <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</a></p>
<pre><code>churn_data_raw &lt;- read_csv(&quot;WA_Fn-UseC_-Telco-Customer-Churn.csv&quot;)</code></pre>
<pre><code>glimpse(churn_data_raw)</code></pre>
<pre><code>## Observations: 7,043
## Variables: 21
## $ customerID       </code></pre></div></div>