<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/churn-prediction-with-automatic-ml/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://mljar.com/blog/churn-prediction-auto-ml/"> MLJAR Blog Category: R</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Sometimes we don’t even realize how common machine learning (ML) is in our daily lives. Various “intelligent” algorithms help us for instance with finding the most important facts (Google), they suggest what movie to watch (Netflix), or influence our shopping decisions (Amazon). The biggest international companies quickly recognized the potential of machine learning and transferred it to business solutions.</p>
<p>Nowadays not only big companies are able to use ML. Imagine — not so abstract — situation when a company tries to predict customer behavior based on some personal data. Just a few years ago, the best strategy to solve this problem would be to hire a good data science team. Nowadays, thanks to growing ML popularity, it is available even for small start-ups.</p>
<p>Today, I would like to present you a demo of how to solve difficult business problems with ML. We will take advantage of mljar.com service and its R API. With just a few lines of code we will be able to achieve very good results.</p>
<p>Typical problem for companies operating on a contractual basis (like Internet, or phone providers) is whether a customer will decide to stay for a next period of time, or churn. It is crucial for a company to focus on customers who are at risk of churning in order to prevent it.</p>
<p>In this example we will help a telecom company to predict, which consumers are likely to renew a contract and which are not. We will base on the data from the past. This dataset in publicly available and can be downloaded for example here: <a rel="nofollow" href="https://github.com/WLOGSolutions/telco-customer-churn-in-r-and-h2o/tree/master/data" target="_blank">https://github.com/WLOGSolutions/telco-customer-churn-in-r-and-h2o/tree/master/data</a>.</p>
<p>It contains descriptors, which can be divided into three different categories:</p>
<ul>
<li>demographical data: gender, age, education, etc.</li>
<li>account information: number of complaints, average call duration, etc.</li>
<li>services used: Internet, voice etc.</li>
</ul>
<p>Finally, we will also have a column with two labels: churn and no churn, which is our target to predict.</p>
<p>Now, that we have the problem set and understand our data, we can move on to the code. mljar.com has both R and Python API, but this time we focus on the former.</p>
<p>First of all, we need to import necessary libraries.</p>
<p><img alt="Libraries" src="https://i2.wp.com/media.mljar.com/blog/churn-prediction/01.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>If you miss one of them, you can always easily install it with install.packages command.</p>
<p>The following lines enable you to read and clean the dataset. In three steps we: get rid of irrelevant columns (time), select only complete records and remove duplicated rows.</p>
<p><img alt="Read and clean the dataset" src="https://i2.wp.com/media.mljar.com/blog/churn-prediction/02.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>It is important to validate our final ML model before publishing, so we split the churn data into training and test set in proportion 7:3.</p>
<p><img alt="Split the data" src="https://i0.wp.com/media.mljar.com/blog/churn-prediction/03.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>For convenience, let’s assign features and labels to separate variables:</p>
<p><img alt="Assign features and labels" src="https://i1.wp.com/media.mljar.com/blog/churn-prediction/04.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>And that’s it! We are ready to use mljar to train our ML models.</p>
<p>(Before, make sure that you have mljar account and your mljar token is set. For more information take a look at: <a rel="nofollow" href="https://github.com/mljar/mljar-api-R/" target="_blank">https://github.com/mljar/mljar-api-R/</a>)</p>
<p>The main advantage of mljar is that it frees the user from thinking about model selection. We can use various different models and compare their performance. In this case, as a performance measure we choose Area Under Curve (<a rel="nofollow" href="https://en.wikipedia.org/wiki/Receiver_operating_characteristic" target="_blank">https://en.wikipedia.org/wiki/Receiver<em>operating</em>characteristic</a>).</p>
<p>We use the following ML algorithms: logistic regression, xgboost, light gradient boosting, extra trees, regularized greedy forest and k-nearest neighbors. Moreover, in R training all these models are as simple as that:</p>
<p><img alt="Run models training" src="https://i2.wp.com/media.mljar.com/blog/churn-prediction/05.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>After running this, when you log in into mljar.com web service and select “Churn” project you should see training data preview with some basic statistics. It is very helpful when you consider which features to use. In this example we decided to take advantage of all of them, but that’s not always the best strategy.</p>
<p><img alt="Data preview" src="https://i1.wp.com/media.mljar.com/blog/churn-prediction/06.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>With a little bit of patience (which depends on your mljar subscription), we got our final model with the best results. All models are also listed at mljar.com in Results card.</p>
<p>mljar<em>fit function returns the best model, which in this case is: ensemble</em>avg.</p>
<p><img alt="Results" src="https://i0.wp.com/media.mljar.com/blog/churn-prediction/07.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>Ensemble average means that this is a combination of various models, which usually performs better together than individual models.</p>
<p>Another useful option of mljar web service is hidden in “Feature Analysis” card. If you select some model there, you will see a graphical illustration of feature importance. For instance for extra trees algorithm we have:</p>
<p><img alt="Feature Analysis" src="https://i0.wp.com/media.mljar.com/blog/churn-prediction/08.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>The graph leads to a conclusion that age, unpaid invoice balance and monthly billed amounts are the most important customer descriptors, whereas number of calls or using some extra services have almost no impact on churning.</p>
<p>To predict labels on the test set, we use mljar_predict command.</p>
<p><img alt="Predict" src="https://i1.wp.com/media.mljar.com/blog/churn-prediction/09.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>Note that the variable y_pr contains probabilities of our churn/no-churn labels. With help of pROC package we will easily find out which threshold is the best in our case:</p>
<p><img alt="Threshold" src="https://i2.wp.com/media.mljar.com/blog/churn-prediction/10.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>In data frame y<em>pr</em>c our classification results are encoded as logical values, so let’s transform them into more readable form:</p>
<p><img alt="Transform classification results" src="https://i1.wp.com/media.mljar.com/blog/churn-prediction/11.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>Finally, we can evaluate our model’s prediction with different measures:</p>
<p><img alt="Compute metrics" src="https://i2.wp.com/media.mljar.com/blog/churn-prediction/12.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>Similar analysis has been performed using another R library for machine learning: H2O (look here: (<a rel="nofollow" href="https://github.com/WLOGSolutions/telco-customer-churn-in-r-and-h2o" target="_blank">https://github.com/WLOGSolutions/telco-customer-churn-in-r-and-h2o</a>). You can see a comparison of results in the table below:</p>
<p><img alt="Comparsion to H2O" src="https://i1.wp.com/media.mljar.com/blog/churn-prediction/13.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>I hope that I convinced you that some complex business problems can be solved easily with machine-learning algorithms using tools like mljar.</p>
<p>(Worth to know: you can repeat all steps of the above analysis with no coding at all, using only mljar web-service. Try it out at mljar.com. If you would like to run code: <a rel="nofollow" href="https://gist.github.com/pplonski/073012dd1be0ed3d3039f755bfa0efb8" target="_blank">it is here</a>.)</p>
<p>For more interesting R resources please check <a rel="nofollow" href="https://r-bloggers.com/" target="_blank">r-bloggers</a>.</p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://mljar.com/blog/churn-prediction-auto-ml/"> MLJAR Blog Category: R</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/7IElyEVGWkc" height="1"></a></div>