<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="http://www.r-bloggers.com/using-support-vector-machines-as-flower-finders-name-that-iris/"></div></div><div><a data-url="http://www.r-bloggers.com/using-support-vector-machines-as-flower-finders-name-that-iris/" data-counturl="http://www.r-bloggers.com/using-support-vector-machines-as-flower-finders-name-that-iris/" data-count="horizontal" data-text="Using Support Vector Machines as Flower Finders: Name that Iris!" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://joelcadwell.blogspot.com/2016/05/using-support-vector-machines-as-flower.html"> Engaging Market Research</a></strong>, and kindly contributed to <a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers)</a> </div>
<div>Nature field guides are filled with pictures of plants and animals that teach us what to look for and how to name what we see. For example, a flower finder might display pictures of different iris species, such as the illustrations in the plot below. You have in hand your own specimen from your garden, and you carefully compare it to each of the pictures until you find a good-enough match. The <a target="_blank" href="https://en.wikipedia.org/wiki/Iris_flower_data_set">pictures come from Wikipedia</a>, but the data used to create the plot are from <a target="_blank" href="https://stat.ethz.ch/R-manual/R-devel/library/datasets/html/iris.html">the R dataset iris</a>: sepal and petal length and width measured on 150 flowers equally divided across three species.</div>
<div></div>
<p>I have lifted the code directly from the <a target="_blank" href="http://www.inside-r.org/node/57517">svm function in the R package e1071</a>.
<div>
<div>
<pre><a target="_blank" href="http://inside-r.org/r-doc/base/library"><span>library</span></a><span>(</span><a target="_blank" href="http://inside-r.org/packages/cran/e1071">e1071</a><span>)</span><br><a target="_blank" href="http://inside-r.org/r-doc/utils/data"><span>data</span></a><span>(</span><a target="_blank" href="http://inside-r.org/r-doc/datasets/iris"><span>iris</span></a><span>)</span><br><a target="_blank" href="http://inside-r.org/r-doc/base/attach"><span>attach</span></a><span>(</span><a target="_blank" href="http://inside-r.org/r-doc/datasets/iris"><span>iris</span></a><span>)</span><br> <br><span>## classification mode</span><br><span># default with factor response:</span><br>model &lt;- svm<span>(</span>Species ~ .<span>,</span> <a target="_blank" href="http://inside-r.org/r-doc/utils/data"><span>data</span></a> = <a target="_blank" href="http://inside-r.org/r-doc/datasets/iris"><span>iris</span></a><span>)</span><br> <br><span># alternatively the traditional interface:</span><br>x &lt;- <a target="_blank" href="http://inside-r.org/r-doc/base/subset"><span>subset</span></a><span>(</span><a target="_blank" href="http://inside-r.org/r-doc/datasets/iris"><span>iris</span></a><span>,</span> select = -Species<span>)</span><br>y &lt;- Species<br>model &lt;- svm<span>(</span>x<span>,</span> y<span>)</span> <br> <br><a target="_blank" href="http://inside-r.org/r-doc/base/print"><span>print</span></a><span>(</span>model<span>)</span><br><a target="_blank" href="http://inside-r.org/r-doc/base/summary"><span>summary</span></a><span>(</span>model<span>)</span><br> <br><span># test with train data</span><br>pred &lt;- <a target="_blank" href="http://inside-r.org/r-doc/stats/predict"><span>predict</span></a><span>(</span>model<span>,</span> x<span>)</span><br><span># (same as:)</span><br>pred &lt;- <a target="_blank" href="http://inside-r.org/r-doc/stats/fitted"><span>fitted</span></a><span>(</span>model<span>)</span><br> <br><span># Check accuracy:</span><br><a target="_blank" href="http://inside-r.org/r-doc/base/table"><span>table</span></a><span>(</span>pred<span>,</span> y<span>)</span><br> <br><span># compute decision values and probabilities:</span><br>pred &lt;- <a target="_blank" href="http://inside-r.org/r-doc/stats/predict"><span>predict</span></a><span>(</span>model<span>,</span> x<span>,</span> decision.values = <span>TRUE</span><span>)</span><br><a target="_blank" href="http://inside-r.org/r-doc/base/attr"><span>attr</span></a><span>(</span>pred<span>,</span> <span>&quot;decision.values&quot;</span><span>)</span><span>[</span><span>1</span>:<span>4</span><span>,</span><span>]</span><br> <br><span># visualize (classes by color, SV by crosses):</span><br><a target="_blank" href="http://inside-r.org/r-doc/graphics/plot"><span>plot</span></a><span>(</span><a target="_blank" href="http://inside-r.org/r-doc/stats/cmdscale"><span>cmdscale</span></a><span>(</span><a target="_blank" href="http://inside-r.org/r-doc/stats/dist"><span>dist</span></a><span>(</span><a target="_blank" href="http://inside-r.org/r-doc/datasets/iris"><span>iris</span></a><span>[</span><span>,</span>-<span>5</span><span>]</span><span>)</span><span>)</span><span>,</span><br>     <a target="_blank" href="http://inside-r.org/r-doc/base/col"><span>col</span></a> = <a target="_blank" href="http://inside-r.org/r-doc/base/as.integer"><span>as.integer</span></a><span>(</span><a target="_blank" href="http://inside-r.org/r-doc/datasets/iris"><span>iris</span></a><span>[</span><span>,</span><span>5</span><span>]</span><span>)</span><span>,</span><br>     pch = <a target="_blank" href="http://inside-r.org/r-doc/base/c"><span>c</span></a><span>(</span><span>&quot;o&quot;</span><span>,</span><span>&quot;+&quot;</span><span>)</span><span>[</span><span>1</span>:<span>150</span> %in% model$index + <span>1</span><span>]</span><span>)</span></pre>
</div>
</div>
<p><a title="Created by Pretty R at inside-R.org" target="_blank" href="http://www.inside-r.org/pretty-r">Created by Pretty R at inside-R.org</a></p>
<p>We will focus on the last block of R code that generates the metric multidimensional scaling (MDS) of the distances separating the 150 flowers calculated from sepal and petal length and width (i.e., <a target="_blank" href="https://stat.ethz.ch/R-manual/R-devel/library/stats/html/dist.html">the dist function</a> applied to the first four columns of the iris data). Species plays no role in the MDS with the flowers positioned in a two-dimensional space in order to reproduce the pairwise Euclidean distances. However, species is projected onto the plot using color, and the observations acting as support vectors are indicated with plus signs (+).</p>
<p>The setosa flowers are represented by black circles and black plus signs. These points are separated along the first dimension from the versicolor species in red and virginica in green. The second dimension, on the other hand, seems to reflect some within-species sources of differences that do not differentiate among the three iris types.</p>
<p>We should recall that the dist function calculates pairwise distances in the original space without any kernel transformation. The support vectors, on the other hand, were identified from the svm function using a radial kernel and then projected back onto the original observation space. Of course, we can change the kernel, which defaults to “radial” as in this example from the R package. A linear kernel may do just as well with the iris data, as you can see by adding kernel=”linear” to the svm function in the above code.</p>
<div><a target="_blank" href="https://1.bp.blogspot.com/-_p05w09FNec/V0R3nVsTeYI/AAAAAAAAAZ4/3hM61FuoUhoUDt0Gtz0HnAOl5z8date4wCKgB/s1600/iris%2Bmds%2Bplot.jpg"><img height="360" width="640" src="https://1.bp.blogspot.com/-_p05w09FNec/V0R3nVsTeYI/AAAAAAAAAZ4/3hM61FuoUhoUDt0Gtz0HnAOl5z8date4wCKgB/s640/iris%2Bmds%2Bplot.jpg" border="0"></a></div>
<p>It appears that we do not need all 150 flowers in order to identify the iris species. We know this because the svm function correctly classifies over 97% of the flowers with 51 support vectors (also called “landmarks” as noted in my last post <a target="_blank" href="http://joelcadwell.blogspot.com/2016/05/the-kernel-trick-in-support-vector.html">Seeing Similarity in More Intricate Dimensions</a>). The majority of the +’s are located between the two species with the greatest overlap. I have included the pictures so that the similarity of the red and green categories is obvious. This is where there will be confusion, and this is where the only misclassifications occur. If your iris is a setosa, your identification task is relatively easy and over quickly. But suppose that your iris resembles those in the cluster of red and green pluses between versicolor and virginica. This is where the finer distinctions are being made.</p>
<p>By design, this analysis was kept brief to draw an analogy between support vector machines and finder guides that we have all used to identify unknown plants and animals in the wild. Hopefully, it was a useful comparison that will help you understand how we classify new observations by measuring their distances in a kernel metric from a more limited set of support vectors (a type of global positioning with a minimal number of landmarks or exemplars as satellites).</p>
<p>When you are ready with your own data, you can view the videos from Chapter 9 of <a target="_blank" href="http://www.r-bloggers.com/in-depth-introduction-to-machine-learning-in-15-hours-of-expert-videos/">An Introduction to Statistical Learning with Applications in R</a> to get a more complete outline of all the steps involved. My intent was simply to disrupt the feature mindset that relies on the cumulative contributions of separate attributes (e.g., the relative impact of each independent variable in a prediction equation). As objects become more complex, we stop seeing individual aspects and begin to bundle features into types or categories. We immediately recognize the object by its feature configuration, and these exemplars or landmarks become the new basis for our support vector representation.</p>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="http://www.r-bloggers.com/using-support-vector-machines-as-flower-finders-name-that-iris/"></div></div><div><a data-url="http://www.r-bloggers.com/using-support-vector-machines-as-flower-finders-name-that-iris/" data-counturl="http://www.r-bloggers.com/using-support-vector-machines-as-flower-finders-name-that-iris/" data-count="vertical" data-text="Using Support Vector Machines as Flower Finders: Name that Iris!" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://joelcadwell.blogspot.com/2016/05/using-support-vector-machines-as-flower.html"> Engaging Market Research</a></strong>.</div>
<hr>
<a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="http://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="http://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="http://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="http://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="http://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="http://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="http://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="http://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="http://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="http://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="http://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="http://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="http://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="http://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="http://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="http://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/OTV0acpWjQk"></a></div>