<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/machine-learning-classification-with-c5-0-decision-tree-algorithm/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://feedproxy.google.com/~r/DataRValue/~3/-F62pDYTJsU/machine-learning-classification-with.html"> Data R Value</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><span><span lang="en"><span>We will develop a classification exercise using C5.0 decision tree algorithm. The exercise was originally published in <i>“Machine Learning in R” </i>by Brett Lantz, PACKT publishing 2015 (open source community experience destilled).</span></span></span></p>
<p><span><span lang="en"><span><span lang="en"><span>The example we will develop is about identifying risky bank loans.</span></span></span></span></span><br><span><br><span lang="en"><span><span lang="en"><span><span lang="en"><span>We will carry out the exercise verbatim as published in the aforementioned reference.</span></span></span></span></span></span></span><br><span><br></span><br><span><b>### install required packages</b><br>#install.packages(“C50”)<br><b><br>### load required packages</b><br>library(C50)<br>library(gmodels)
<p><b>### read and explore the data</b><br>credit &lt;- read.csv(“credit.csv”)<br>str(credit)</p>
<p><b>### look a couple of loan features that seem likely<br>### to predict a default (currency Deutsche Marks DM)</b><br>table(credit$checking_balance)<br>table(credit$savings_balance)</p>
<p><b>### some of the loan’s features are numeric</b><br>summary(credit$months_loan_duration)<br>summary(credit$amount)<br><b><br>### $default indicates whether the loan applicant<br>### went or not into default</b></p>
<p><b>### change levels of $default</b><br>credit$default&lt;- factor(credit$default, levels = c(“1”, “2”),<br>                        labels = c(“no”, “yes”))<br><b> </b><br><span><b>### see frequencies of $default</b><br>table(credit$default)
<p><b>### data preparation<br>### creating random training (90%) and test datasets (10%)
<p>### random sample numbers<br>set.seed(123)<br>train_sample &lt;- sample(1:1000, 900)<br>  <br><b>### split the dataset</b><br>credit_train &lt;- credit[train_sample, ]<br>credit_test &lt;- credit[-train_sample, ]</p></b>
<p><b>### check for about 30% of defaulted loans in each of<br>### the datasets</b><br>prop.table(table(credit_train$default))<br>prop.table(table(credit_test$default))</p>
<p><b>### training a model on the data
<p>### $default column in credit_train is the default class<br>### variable, so we need to exclude it from the training<br>### data frame, but supply it as the target factor vector<br>### for classification<br>credit_model &lt;- C5.0(credit_train[-17], credit_train$default)<br><b><br>### see some basic data about the tree</b><br>credit_model<br>summary(credit_model)</p></b>
<p><b>### evaluating model performance</b><br>credit_pred &lt;- predict(credit_model, credit_test)<br> <br><span>CrossTable(credit_test$default, credit_pred,<br>           prop.chisq = FALSE, prop.c = FALSE, prop.r = FALSE,<br>           dnn = c(‘actual default’, ‘predicted default’))</span></p></span>
<p>
<div><span><a target="_blank" rel="nofollow" href="https://i0.wp.com/1.bp.blogspot.com/-1i20IvGVBDg/WQe1hxt1hqI/AAAAAAAAC1U/Nmuhtk-9qLEf2EFDl9MKVrnT2Fa3mAAYACLcB/s1600/c50_2.png?ssl=1"><img src="https://i1.wp.com/1.bp.blogspot.com/-1i20IvGVBDg/WQe1hxt1hqI/AAAAAAAAC1U/Nmuhtk-9qLEf2EFDl9MKVrnT2Fa3mAAYACLcB/s400/c50_2.png?resize=400%2C392&ssl=1" border="0" data-recalc-dims="1"></a></span></div></span>
<p><span><b>### improving model performance?<br>### boosting the accuracy of decision trees<br>### add an additional trials parameter indicating the<br>### number of separate decision trees to use in the<br>### boosted team</b><br>credit_boost10 &lt;- C5.0(credit_train[-17], credit_train$default,<br>                       trials = 10)
<p>credit_boost10<br>summary(credit_boost10)</p>
<p>credit_boost_pred10 &lt;- predict(credit_boost10, credit_test)<br> <br><span>CrossTable(credit_test$default, credit_boost_pred10,<br>           prop.chisq = FALSE, prop.c = FALSE, prop.r = FALSE,<br>           dnn = c(‘actual default’, ‘predicted default’))</span><br><span><br></span>
<div><span><a target="_blank" rel="nofollow" href="https://i1.wp.com/1.bp.blogspot.com/-JKs-Mc6m2sc/WQe12VwJpXI/AAAAAAAAC1Y/azBQzhG0P_0iQBaH4QzSxbXDdu1dgN2HACLcB/s1600/c50_3.png?ssl=1"><img src="https://i2.wp.com/1.bp.blogspot.com/-JKs-Mc6m2sc/WQe12VwJpXI/AAAAAAAAC1Y/azBQzhG0P_0iQBaH4QzSxbXDdu1dgN2HACLcB/s400/c50_3.png?resize=400%2C378&ssl=1" border="0" data-recalc-dims="1"></a></span></div></span>
<p><span><b>### a way to reduces false negatives</b><br><b>### set penalties to different types of errors<br>### cost matrix</b><br>matrix_dimensions &lt;- list(c(“no”, “yes”), c(“no”, “yes”))<br> </span><br><span>names(matrix_dimensions) &lt;- c(“predicted”, “actual”)
<p>matrix_dimensions<br><b><br>### assign the penalty for the types of errors
<p>### if a loan default costs four times as much as<br>### a missed opportunity</p></b>
<p>error_cost &lt;- matrix(c(0, 1, 4, 0), nrow = 2,<br>                     dimnames = matrix_dimensions)<br> <br><span><b>### false negative has a cost of 4 versus a false<br>### positive’s cost of 1</b></span><br><span>error_cost
<p><b>### now use costs parameter of the C50()</b><br>credit_cost &lt;- C5.0(credit_train[-17], credit_train$default,<br>                    costs = error_cost)<br> <br><span>credit_cost_pred &lt;- predict(credit_cost, credit_test)<br> </span><br><span>CrossTable(credit_test$default, credit_cost_pred,<br>           prop.chisq = FALSE, prop.c = FALSE, prop.r = FALSE,<br>           dnn = c(‘actual default’, ‘predicted default’))</span></p></span>
<p>
<div><span><a target="_blank" rel="nofollow" href="https://i1.wp.com/4.bp.blogspot.com/-Cg3WtJspO_Q/WQe2XSXBg3I/AAAAAAAAC1g/2nsrUdLO6AA4kk0-OVtMN1A4Ln8XwGIBACLcB/s1600/c50_4.png?ssl=1"><img src="https://i1.wp.com/4.bp.blogspot.com/-Cg3WtJspO_Q/WQe2XSXBg3I/AAAAAAAAC1g/2nsrUdLO6AA4kk0-OVtMN1A4Ln8XwGIBACLcB/s400/c50_4.png?resize=400%2C377&ssl=1" border="0" data-recalc-dims="1"></a></span></div></span>
<p><span><br></span><span><b>### false negatives was reduced at the expense of<br>### increasing false positives</b></span></p>
<p><span><span lang="en"><span>You can get the exercise and the dataset in:</span></span><b> </b></span><br><span><span><a target="_blank" rel="nofollow" href="https://github.com/pakinja/Data-R-Value/tree/master/MachineLearning_C5.0_Classification_Algorithm">https://github.com/pakinja/Data-R-Value</a></span></span></p>
<p><img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/DataRValue/~4/-F62pDYTJsU"></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://feedproxy.google.com/~r/DataRValue/~3/-F62pDYTJsU/machine-learning-classification-with.html"> Data R Value</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 title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/jsCMxJwfDK0"></a></div>