<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-1r-and-ripper-rule-learners-ediblepoisonous-mushrooms/"></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/mtu9MZVz5nI/machine-learning-classification-with-1r.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 example using 1R and RIPPER rule learners algorithms. 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 classifying edible and poisonous mushrooms.</span></span></span></span></span>
<div><span><a target="_blank" rel="nofollow" href="https://i1.wp.com/2.bp.blogspot.com/-FdB-HV4sl6g/WQoH_-Bp7tI/AAAAAAAAC2A/s0J1f3XUdjYULtVo2FLQkxvqDLzCtbD6QCLcB/s1600/mush1.jpeg?ssl=1"><img src="https://i1.wp.com/2.bp.blogspot.com/-FdB-HV4sl6g/WQoH_-Bp7tI/AAAAAAAAC2A/s0J1f3XUdjYULtVo2FLQkxvqDLzCtbD6QCLcB/s320/mush1.jpeg?resize=215%2C320&ssl=1" border="0" data-recalc-dims="1"></a></span></div>
<p><span><span lang="en"><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 and </span></span></span></span></span></span><span lang="en"><span>including some simple transformations to the dataset.</span></span></span><br><span><span lang="en"><span><span lang="en"><span>For more details on the algorithms it is recommended to check the aforementioned reference.</span></span> </span></span></span></p>
<p><span><span lang="en"><span><br><b>### install and load required packages</b><br>#install.packages(“RWeka”)
<p>library(RWeka)</p>
<p><b>### read and explore the data</b><br>mushrooms &lt;- read.csv(“mushrooms.csv”, stringsAsFactors = TRUE)<br>str(mushrooms)</p>
<p><b>### drop $veil_type column (does not provide)<br>### useful information</b><br>mushrooms$veil_type &lt;- NULL</p>
<p><b>### look at the distribution of the muschroom type<br>### class variable</b><br>table(mushrooms$type)</p>
<p><b>### data transformation
<p>#Below function transforms the $type column<br>transformType &lt;- function(key){<br>  switch (as.character(key),<br>          ‘p’ = ‘poisonous’,<br>          ‘e’ = ‘edible’<br>  )<br>}</p></b>
<p><b>#Below function transforms the $odor column</b><br>transformOdor &lt;- function(key){<br>  switch (as.character(key),<br>          ‘a’ = ‘almond’,<br>          ‘l’ = ‘anise’,<br>          ‘c’ = ‘creosote’,<br>          ‘y’ = ‘fishy’,<br>          ‘f’ = ‘foul’,<br>          ‘m’ = ‘musty’,<br>          ‘n’ = ‘none’,<br>          ‘p’ = ‘pungent’,<br>          ‘s’ = ‘spicy’<br>  )<br>}</p>
<p><b>### apply transformations</b><br>mushrooms$type &lt;- sapply(mushrooms$type, transformType)<br>mushrooms$odor &lt;- sapply(mushrooms$odor, transformOdor)</p>
<p>mushrooms$type &lt;- as.factor(mushrooms$type)<br>mushrooms$odor &lt;- as.factor(mushrooms$odor)<br> <br><span><span lang="en"><span><b>### training a model on the data</b>
<p><b>### use the 1R implementation in the RWeka package<br>### called OneR()</b></p>
<p><b>### consider all possible features to predict $type</b><br>mushroom_1R &lt;- OneR(type ~ ., data = mushrooms)<br>mushroom_1R</p></span>
<p><span><b><span lang="en"><span>### 1R one rule selected:</span></span></b></span><br><span><span lang="en"><span> </span></span></span><br><span><span lang="en"><span></span></span></span><br><span><span lang="en"><span></span></span></span>
<div><span><a target="_blank" rel="nofollow" href="https://i1.wp.com/2.bp.blogspot.com/-Qo8Zn0r47hA/WQoJ-Ma7UUI/AAAAAAAAC2M/5ZdG0p1D-5MZPLnOmysA7ZxYzTwTu_8WgCLcB/s1600/mush2.png?ssl=1"><img src="https://i0.wp.com/2.bp.blogspot.com/-Qo8Zn0r47hA/WQoJ-Ma7UUI/AAAAAAAAC2M/5ZdG0p1D-5MZPLnOmysA7ZxYzTwTu_8WgCLcB/s200/mush2.png?resize=200%2C145&ssl=1" border="0" data-recalc-dims="1"></a></span></div>
<p><span><span lang="en"><span></span></span></span><br><span><span lang="en"><span><br><b>### evaluating model performance</b><br>summary(mushroom_1R)</span></span></span><br><span><span lang="en"><span><br></span></span></span>
<div><span><a target="_blank" rel="nofollow" href="https://i2.wp.com/1.bp.blogspot.com/-Wfaj5SI4F-Q/WQoKaZjWGqI/AAAAAAAAC2Q/KrB3vcZSTmQwATsSDft4ZjRgwtI3E1-XACLcB/s1600/mush3.png?ssl=1"><img src="https://i1.wp.com/1.bp.blogspot.com/-Wfaj5SI4F-Q/WQoKaZjWGqI/AAAAAAAAC2Q/KrB3vcZSTmQwATsSDft4ZjRgwtI3E1-XACLcB/s400/mush3.png?resize=400%2C222&ssl=1" border="0" data-recalc-dims="1"></a></span></div>
<p><span><span lang="en"><span><br></span></span></span><span><span lang="en"><span><b>### improving model performance<br>### JRip() Java-based implementation of the Ripper<br>### rule learning algorithm</b><br>mushroom_JRip &lt;- JRip(type ~ ., data = mushrooms)<br></span></span></span><br><span><span lang="en"><span><b>### RIPPER selected rules (9): </b><br>mushroom_JRip</span></span></span><br><span><span lang="en"><span><br></span></span></span>
<div><span><a target="_blank" rel="nofollow" href="https://i1.wp.com/3.bp.blogspot.com/-ag4mNlvn7vA/WQoK1SQnt0I/AAAAAAAAC2U/sDOPymcxkH4HUaE-Ze6Upy1z1ibzX670gCLcB/s1600/mush4.png?ssl=1"><img src="https://i0.wp.com/3.bp.blogspot.com/-ag4mNlvn7vA/WQoK1SQnt0I/AAAAAAAAC2U/sDOPymcxkH4HUaE-Ze6Upy1z1ibzX670gCLcB/s400/mush4.png?resize=400%2C140&ssl=1" border="0" data-recalc-dims="1"></a></span></div>
<p><span><span lang="en"><span></span></span></span><br><span><br></span><span><span lang="en"><span>You can get the example and the dataset in:</span></span></span><br><span><span><span lang="en"><span><a target="_blank" rel="nofollow" href="https://github.com/pakinja/Data-R-Value/tree/master/MachineLearning_Identifying_Mushrooms_1R_RIPPER">https://github.com/pakinja/Data-R-Value</a></span></span></span></span><br><span><span lang="en"><span><br></span></span></span><img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/DataRValue/~4/mtu9MZVz5nI"></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/mtu9MZVz5nI/machine-learning-classification-with-1r.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/oTgooR4ZeNI"></a></div></span></span></span></span></span>