<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/robust-iris-dataset/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://tomaztsql.wordpress.com/2017/10/25/robust-iris-dataset/"> R – TomazTsql</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>This blog post was born out of pure curiosity about the robustness of the <a rel="nofollow" href="https://archive.ics.uci.edu/ml/datasets/iris" target="_blank">IRIS Dataset</a>. Biological datasets do not need to be that big in comparison to datasets of customers, consumption, stock and anything that might be volatile.</p>
<p>When still at the university, on one occasion I can remember, we were measuring the length of the frog legs and other frogy features. And after just a couple of measures, the further prediction was steady. Also, any kind of sampling was (RS and SRS, cluster/stratified sampling, sampling with replacements and many other creative ways of sampling) proven to be rigid, robust and would converge quickly to a good result.</p>
<p>Therefore, I have decided to put the IRIS dataset to the test, using a simple classification method. Calculating first the simple euclidian distance, following by finding the neighbour and based on that checking the membership of the type of flowers with the labels.</p>
<p>Accuracy of the prediction was tested by mapping the original species with predicted ones. And the test was, how large can a train dataset be in order to still get a good result.</p>
<p>After some Python and R code, the results were in.</p>
<p>I have tested following pairs (train:test sample size):</p>
<ul>
<li>80% – 20%</li>
<li>60% – 40%</li>
<li>50% – 50%</li>
<li>30% – 70%</li>
<li>10% – 90%</li>
</ul>
<p>Note, that the IRIS dataset has 150 observations, each evenly distributed among three species. Following Python code loop through the calculation of euclidean distance.</p>
<pre>for x in range(3000):
    exec(open(&quot;./classification.py&quot;).read(), globals())
    x += 1</pre>
<p>At the end I have generated the file:</p>
<p><img data-orig-file="https://tomaztsql.files.wordpress.com/2017/10/predictions.png?w=159&amp;h=520&amp;fit=159%2C520&amp;resize=159%2C520" data-orig-size="192,628" data-medium-file="https://tomaztsql.files.wordpress.com/2017/10/predictions.png?w=159&amp;h=520&amp;fit=159%2C520&amp;resize=159%2C520?w=92" data-attachment-id="4827" data-image-meta="{" src="https://tomaztsql.files.wordpress.com/2017/10/predictions.png?w=159&h=520&fit=159%2C520&resize=159%2C520" data-image-description="" data-permalink="https://tomaztsql.wordpress.com/2017/10/25/robust-iris-dataset/predictions/#main" data-large-file="https://tomaztsql.files.wordpress.com/2017/10/predictions.png?w=159&amp;h=520&amp;fit=159%2C520&amp;resize=159%2C520?w=192" alt="predictions" data-image-title="predictions" sizes="(max-width: 159px) 100vw, 159px" class="wp-image-4827" data-comments-opened="1" data-recalc-dims="1"></p>
<p>With these results, simple R code to generate the scatter plot was used:</p>
<pre>library(ggplot2)
setwd(&quot;C:\\Predictions\\&quot;)
df_pred &lt;- data.frame(read.table(&quot;results_split.txt&quot;, sep=&quot;;&quot;))
p &lt;- ggplot(df_pred, aes(df_pred$V3, df_pred$V1)) 
p &lt;- p + geom_point(aes(df_pred$V3))
p &lt;- p + labs(x=&quot;Accuracy (%) of predictions&quot;, y=&quot;Size of training subset&quot;)
p</pre>
<p>Which resulted as:</p>
<p><img data-orig-file="https://tomaztsql.files.wordpress.com/2017/10/plot.png?w=456" data-orig-size="748,551" data-medium-file="https://tomaztsql.files.wordpress.com/2017/10/plot.png?w=456?w=300" data-attachment-id="4837" data-image-meta="{" src="https://tomaztsql.files.wordpress.com/2017/10/plot.png?w=456" data-image-description="" data-permalink="https://tomaztsql.wordpress.com/2017/10/25/robust-iris-dataset/plot/#main" data-large-file="https://tomaztsql.files.wordpress.com/2017/10/plot.png?w=456?w=700" alt="Plot" data-image-title="Plot" sizes="(max-width: 700px) 100vw, 700px" class="wp-image-4837" data-comments-opened="1" data-recalc-dims="1"></p>
<p>The graph clearly shows that 10% of training set (10% out of 150 observations) can generate very accurate predictions every 1,35x times.</p>
<p>Other pairs, when taking 30% or 50% of training set, will for sure give close to 100% accuracy almost every time.</p>
<p>Snippet of Python code to generate euclidean distance:</p>
<pre>def eucl_dist(set1, set2, length):
    distance = 0
    for x in range(length):
        distance += pow(set1[x] - set2[x], 2)
    return math.sqrt(distance)</pre>
<p>and neighbours:</p>
<pre>def find_neighbors(train, test, nof_class):
    distances = []
    length_dist = len(test) - 1
    for x in range(len(train)):
        dist = eucl_dist(test, train[x], length_dist)
        distances.append((train[x],dist))
    distances.sort(key=operator.itemgetter(1))
    neighbour = []
    for x in range(nof_class):
        neighbour.append(distances[x][0])
    return neighbour</pre>
<p> </p>
<p>Conclusion, IRIS dataset is – due to the nature of the measurments and observations – robust and rigid; one can get very good accuracy results on a small training set. Everything beyond 30% for training the model, is for this particular case, just additional overload.</p>
<p>Happy R &amp; Python coding! <img alt="🙂" src="https://i0.wp.com/s0.wp.com/wp-content/mu-plugins/wpcom-smileys/twemoji/2/72x72/1f642.png?w=456&ssl=1" class="wp-smiley" data-recalc-dims="1"></p>
<p><a rel="nofollow" href="https://feeds.wordpress.com/1.0/gocomments/tomaztsql.wordpress.com/4800/" target="_blank"><img border="0" alt="" src="https://feeds.wordpress.com/1.0/comments/tomaztsql.wordpress.com/4800/"></a> <img border="0" alt="" src="https://i1.wp.com/pixel.wp.com/b.gif?resize=1%2C1&ssl=1" data-recalc-dims="1"></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="https://tomaztsql.wordpress.com/2017/10/25/robust-iris-dataset/"> R – TomazTsql</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 alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/LhGV19wNwl8" height="1"></a></div>