<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/performance-comparison-between-kmeans-and-revoscaler-rxkmeans/"></div></div><div><a data-url="https://www.r-bloggers.com/performance-comparison-between-kmeans-and-revoscaler-rxkmeans/" data-counturl="https://www.r-bloggers.com/performance-comparison-between-kmeans-and-revoscaler-rxkmeans/" data-count="horizontal" data-text="Performance comparison between kmeans and RevoScaleR rxKmeans" 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="https://tomaztsql.wordpress.com/2016/10/12/performance-comparison-between-kmeans-and-revoscaler-rxkmeans/"> R – TomazTsql</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>In my previous <a target="_blank" href="https://tomaztsql.wordpress.com/2016/10/10/comparing-performance-on-dplyr-package-revoscaler-package-and-t-sql-on-simple-data-manipulation-tasks/">blog post</a>, I was focusing on data manipulation tasks with RevoScaleR Package in comparison to other data manipulation packages and at the end conclusions were obvious; RevoScaleR can not (without the help of dplyrXdf) do piping (or chaining) and storing temporary results take time and on top of that, data manipulation can be done easier (cleaner and faster) with dplyr package or data.table package. Another conclusion was, that you should do (as much as possible) all the data manipulation tasks within your client, so you diminish the value of the data sent to computation environment.</p>
<p>In this post, I will do a simple performance comparison between  <a target="_blank" href="https://stat.ethz.ch/R-manual/R-devel/library/stats/html/kmeans.html">kmeans </a>clustering function  available in default <a target="_blank" href="https://stat.ethz.ch/R-manual/R-devel/library/stats/html/00Index.html">stats package</a> and RevoScaleR rxKmeans function for clustering.</p>
<p>Data will be loaded from WideWorldImportersDW.</p>
<pre>library(RODBC)
library(RevoScaleR)
library(ggplot2)
library(dplyr)

myconn &lt;-odbcDriverConnect(&quot;driver={SQL Server};Server=T-KASTRUN;
database=WideWorldImportersDW;trusted_connection=true&quot;)

cust.data &lt;- sqlQuery(myconn, &quot;SELECT 
                      fs.[Sale Key] AS SalesID
                      ,fs.[City Key] AS CityKey
                      ,c.[City] AS City
                      ,c.[State Province] AS StateProvince
                      ,c.[Sales Territory] AS SalesTerritory
                      ,fs.[Customer Key] AS CustomerKey
                      ,fs.[Stock Item Key] AS StockItem
                      ,fs.[Quantity] AS Quantity
                      ,fs.[Total Including Tax] AS Total
                      ,fs.[Profit] AS Profit
                      
                      
                      FROM [Fact].[Sale] AS  fs
                      JOIN dimension.city AS c
                      ON c.[City Key] = fs.[City Key]
                      WHERE
                      fs.[customer key] &lt;> 0 &quot;)

close(myconn)</pre>
<p> </p>
<p>In essence, I will be using same dataset, and comparing same algorithm (Lloyd) with same variables (columns) taken with all parameters the same. So RevoScaleR rxKmeans with following columns:</p>
<pre>rxSalesCluster &lt;- rxKmeans(formula= ~SalesID + CityKey + CustomerKey + 
StockItem + Quantity, data =cust.data, numCluster=9,algorithm = &quot;lloyd&quot;, 
outFile = &quot;SalesCluster.xdf&quot;, outColName = &quot;Cluster&quot;, overwrite = TRUE)</pre>
<p>vs. kmeans example:</p>
<pre>SalesCluster &lt;- kmeans(cust.data[,c(1,2,6,7,8)], 9, nstart = 20, algorithm=&quot;Lloyd&quot;)</pre>
<p>After running 50 iterations on both of the algorithms, measured two metrics. First one was elapsed computation time and second one was the ratio between the “between-cluster sum of squares” and “total within-cluster sum of squares”.</p>
<pre>fit &lt;- rxSalesCluster$betweenss/rxSalesCluster$totss</pre>
<table summary="R valueblock" width="610">
<tbody>
<tr valign="top">
<td>tot.withinss    [<strong>totss</strong>]</td>
<td> Total within-cluster sum of squares, i.e. sum(withinss).</td>
</tr>
<tr valign="top">
<td>betweenss   [<strong>betweenss</strong>]</td>
<td>The between-cluster sum of squares, i.e. totss-tot.withinss.</td>
</tr>
</tbody>
</table>
<p>Graph of the performance over 50 iterations with both clustering functions.</p>
<p><img alt="2016-10-12 12_11_48-Plot Zoom.jpg" class="wp-image-2761" src="https://i1.wp.com/tomaztsql.files.wordpress.com/2016/10/2016-10-12-12_11_48-plot-zoom.jpg?resize=456%2C530&ssl=1" data-recalc-dims="1"></p>
<p>The performance results are very much obvious, rxKmeans function from RevoScaleR package outperforms stats kmeans by almost 3-times, which is at given dataset (143K Rows and 5 variables) a pretty substantiate improvement due to parallel computation.</p>
<p>So in terms of elapsed computation time:</p>
<pre>result %>%
  group_by(cl_met) %>%
  summarize(
    average_score = mean(et)
    ,variance = var(et)
    ,st_dev = sd(et)
  )</pre>
<p><img alt="2016-10-12 12_15_44-RStudio.png" class="wp-image-2769" src="https://i0.wp.com/tomaztsql.files.wordpress.com/2016/10/2016-10-12-12_15_44-rstudio.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>kmeans average computation time is little below 4 seconds and rxKmeans computation time is little over 1 second. Please keep in mind that additional computation is performed in this time for clusters fit but in terms of time difference, it remains the same. From graph and from variance/standard deviations, one can see that rxKmeans has higher deviations which results in spikes on graph and are results of parallelizing the data on file. Every 6th iteration there is additional workload / worker dedicated for additional computation.</p>
<p>But the biggest concern is the results itself. Over 50 iterations I stored also the computation of with-in and between cluster sum of squares calculations. And results are stunning.</p>
<pre>result %>% 
  group_by(cl_met) %>%
  summarize(
             average_score = mean(fit)
            ,variance = var(fit)
            ,st_dev = sd(fit)
           )</pre>
<p><img alt="2016-10-12-12_22_37-rstudio" class="wp-image-2775" src="https://i1.wp.com/tomaztsql.files.wordpress.com/2016/10/2016-10-12-12_22_37-rstudio.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>The difference between average calculation of the fit is absolutely so minor that it is worthless giving any special attention.</p>
<pre>(mean(result[result$cl_met == 'kmeans',]$fit)*100 - mean(result[result$cl_met 
== 'rxKmeans',]$fit)*100)</pre>
<p>is</p>
<pre>0.04407133.</pre>
<p> </p>
<p>With rxKmeans you gain super good performances and the results are equal as to the default kmean clustering function.</p>
<p>Code is availble at <a target="_blank" href="https://github.com/tomaztk/Compare_kmeans_rxKmeans">GitHub</a>.</p>
<p>Happy R-TSQLing!</p>
<p> <a target="_blank" rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/tomaztsql.wordpress.com/2715/"><img alt="" src="http://feeds.wordpress.com/1.0/comments/tomaztsql.wordpress.com/2715/" border="0"></a> <img alt="" src="https://i1.wp.com/pixel.wp.com/b.gif?resize=1%2C1&ssl=1" border="0" data-recalc-dims="1"></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="https://www.r-bloggers.com/performance-comparison-between-kmeans-and-revoscaler-rxkmeans/"></div></div><div><a data-url="https://www.r-bloggers.com/performance-comparison-between-kmeans-and-revoscaler-rxkmeans/" data-counturl="https://www.r-bloggers.com/performance-comparison-between-kmeans-and-revoscaler-rxkmeans/" data-count="vertical" data-text="Performance comparison between kmeans and RevoScaleR rxKmeans" 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="https://tomaztsql.wordpress.com/2016/10/12/performance-comparison-between-kmeans-and-revoscaler-rxkmeans/"> 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 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/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/?s=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/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/CA_Yw8dyEmQ"></a></div>