<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/building-deep-neural-nets-with-h2o-and-rsparkling-that-predict-arrhythmia-of-the-heart/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://shiring.github.io/machine_learning/2017/02/27/h2o"> Shirin's playgRound</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><a target="_blank" rel="nofollow" href="https://shiring.github.io/machine_learning/2017/02/19/food_spark">Last week, I introduced how to run machine learning applications on Spark from within R, using the <strong>sparklyr</strong> package.</a> This week, I am showing how to build feed-forward deep neural networks or multilayer perceptrons. The models in this example are built to classify ECG data into being either from <em>healthy</em> hearts or from someone suffering from <em>arrhythmia</em>. I will show how to prepare a dataset for modeling, setting weights and other modeling parameters and finally, how to evaluate model performance with the <strong>h2o</strong> package via <strong>rsparkling</strong>.</p>
<p></p>
<h3>Deep learning with neural networks</h3>
<p>Deep learning with neural networks is arguably one of the most rapidly growing applications of machine learning and AI today. They allow building complex models that consist of multiple hidden layers within artifiical networks and are able to find non-linear patterns in unstructured data. Deep neural networks are usually feed-forward, which means that each layer feeds its output to subsequent layers, but recurrent or feed-back neural networks can also be built. Feed-forward neural networks are also called multilayer perceptrons (MLPs).</p>
<p></p>
<h3>H2O and Sparkling Water</h3>
<p>The R package h2o provides a convenient interface to H2O, which is an open-source machine learning and deep learning platform. <a target="_blank" rel="nofollow" href="http://www.h2o.ai/h2o/">H2O</a> can be integrated with Apache Spark (<a target="_blank" rel="nofollow" href="http://www.h2o.ai/sparkling-water/"><strong>Sparkling Water</strong></a>) and therefore allows the implementation of complex or big models in a fast and scalable manner. H2O distributes a wide range of common machine learning algorithms for classification, regression and deep learning.</p>
<p><a target="_blank" rel="nofollow" href="http://spark.rstudio.com/h2o.html"><strong>Sparkling Water</strong> can be accessed from R</a> with the <strong>rsparkling</strong> extension package to <strong>sparklyr</strong> and <strong>h2o</strong>. Check the documentation for <strong>rsparkling</strong> to find out which H2O, Sparkling Water and Spark versions are compatible.</p>
<p></p>
<h3>Preparing the R session</h3>
<p>First, we need to load the packages and connect to the Spark instance (for demonstration purposes, I am using a local instance).</p>
<div>
<pre><code><span>library</span><span>(</span><span>rsparkling</span><span>)</span><span>
</span><span>options</span><span>(</span><span>rsparkling.sparklingwater.version</span><span> </span><span>=</span><span> </span><span>&quot;2.0.3&quot;</span><span>)</span><span>
</span><span>library</span><span>(</span><span>h</span><span>2</span><span>o</span><span>)</span><span>
</span><span>library</span><span>(</span><span>dplyr</span><span>)</span><span>
</span><span>library</span><span>(</span><span>sparklyr</span><span>)</span><span>
</span><span>sc</span><span> </span><span>&lt;-</span><span> </span><span>spark_connect</span><span>(</span><span>master</span><span> </span><span>=</span><span> </span><span>&quot;local&quot;</span><span>,</span><span> </span><span>version</span><span> </span><span>=</span><span> </span><span>&quot;2.0.0&quot;</span><span>)</span><span>
</span></code></pre>
</div>
<p>I am also preparing my custom plotting theme.</p>
<div>
<pre><code><span>library</span><span>(</span><span>ggplot2</span><span>)</span><span>
</span><span>library</span><span>(</span><span>ggrepel</span><span>)</span><span>
</span><span>my_theme</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>base_size</span><span> </span><span>=</span><span> </span><span>12</span><span>,</span><span> </span><span>base_family</span><span> </span><span>=</span><span> </span><span>&quot;sans&quot;</span><span>){</span><span>
</span><span>theme_minimal</span><span>(</span><span>base_size</span><span> </span><span>=</span><span> </span><span>base_size</span><span>,</span><span> </span><span>base_family</span><span> </span><span>=</span><span> </span><span>base_family</span><span>)</span><span> </span><span>+</span><span>
</span><span>theme</span><span>(</span><span>
</span><span>axis.text</span><span> </span><span>=</span><span> </span><span>element_text</span><span>(</span><span>size</span><span> </span><span>=</span><span> </span><span>12</span><span>),</span><span>
</span><span>axis.title</span><span> </span><span>=</span><span> </span><span>element_text</span><span>(</span><span>size</span><span> </span><span>=</span><span> </span><span>14</span><span>),</span><span>
</span><span>panel.grid.major</span><span> </span><span>=</span><span> </span><span>element_line</span><span>(</span><span>color</span><span> </span><span>=</span><span> </span><span>&quot;grey&quot;</span><span>),</span><span>
</span><span>panel.grid.minor</span><span> </span><span>=</span><span> </span><span>element_blank</span><span>(),</span><span>
</span><span>panel.background</span><span> </span><span>=</span><span> </span><span>element_rect</span><span>(</span><span>fill</span><span> </span><span>=</span><span> </span><span>&quot;aliceblue&quot;</span><span>),</span><span>
</span><span>strip.background</span><span> </span><span>=</span><span> </span><span>element_rect</span><span>(</span><span>fill</span><span> </span><span>=</span><span> </span><span>&quot;darkgrey&quot;</span><span>,</span><span> </span><span>color</span><span> </span><span>=</span><span> </span><span>&quot;grey&quot;</span><span>,</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>1</span><span>),</span><span>
</span><span>strip.text</span><span> </span><span>=</span><span> </span><span>element_text</span><span>(</span><span>face</span><span> </span><span>=</span><span> </span><span>&quot;bold&quot;</span><span>,</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>12</span><span>,</span><span> </span><span>color</span><span> </span><span>=</span><span> </span><span>&quot;white&quot;</span><span>),</span><span>
</span><span>legend.position</span><span> </span><span>=</span><span> </span><span>&quot;right&quot;</span><span>,</span><span>
</span><span>legend.justification</span><span> </span><span>=</span><span> </span><span>&quot;top&quot;</span><span>,</span><span> </span><span>panel.border</span><span> </span><span>=</span><span> </span><span>element_rect</span><span>(</span><span>color</span><span> </span><span>=</span><span> </span><span>&quot;grey&quot;</span><span>,</span><span> </span><span>fill</span><span> </span><span>=</span><span> </span><span>NA</span><span>,</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>0.5</span><span>)</span><span>
</span><span>)</span><span>
</span><span>}</span><span>
</span></code></pre>
</div>
<p></p>
<h3>Arrhythmia data</h3>
<p>The data I am using to demonstrate the building of neural nets is the arrhythmia dataset from <a target="_blank" rel="nofollow" href="https://archive.ics.uci.edu/ml/datasets/Arrhythmia">UC Irvine’s machine learning database</a>. It contains 279 features from ECG heart rhythm diagnostics and one output column. I am not going to rename the feature columns because they are too many and the descriptions are too complex. Also, we don’t need to know specifically which features we are looking at for building the models. For a description of each feature, see <a target="_blank" rel="nofollow" href="https://archive.ics.uci.edu/ml/machine-learning-databases/arrhythmia/arrhythmia.names">https://archive.ics.uci.edu/ml/machine-learning-databases/arrhythmia/arrhythmia.names</a>. The output column defines 16 classes: class 1 samples are from healthy ECGs, the remaining classes belong to different types of arrhythmia, with class 16 being all remaining arrhythmia cases that didn’t fit into distinct classes.</p>
<div>
<pre><code><span>arrhythmia</span><span> </span><span>&lt;-</span><span> </span><span>read.table</span><span>(</span><span>&quot;arrhythmia.data.txt&quot;</span><span>,</span><span> </span><span>sep</span><span> </span><span>=</span><span> </span><span>&quot;,&quot;</span><span>)</span><span>
</span><span># making sure, that all feature columns are numeric
</span><span>arrhythmia</span><span>[</span><span>-280</span><span>]</span><span> </span><span>&lt;-</span><span> </span><span>lapply</span><span>(</span><span>arrhythmia</span><span>[</span><span>-280</span><span>],</span><span> </span><span>as.numeric</span><span>)</span><span>
</span><span># renaming output column and converting to factor
</span><span>colnames</span><span>(</span><span>arrhythmia</span><span>)[</span><span>280</span><span>]</span><span> </span><span>&lt;-</span><span> </span><span>&quot;class&quot;</span><span>
</span><span>arrhythmia</span><span>$</span><span>class</span><span> </span><span>&lt;-</span><span> </span><span>as.factor</span><span>(</span><span>arrhythmia</span><span>$</span><span>class</span><span>)</span><span>
</span></code></pre>
</div>
<p>As usual, I want to get acquainted with the data and explore it’s properties before I am building any model. So, I am first going to look at the distribution of classes and of healthy and arrhythmia samples.</p>
<div>
<pre><code><span>p</span><span>1</span><span> </span><span>&lt;-</span><span> </span><span>ggplot</span><span>(</span><span>arrhythmia</span><span>,</span><span> </span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>class</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_bar</span><span>(</span><span>fill</span><span> </span><span>=</span><span> </span><span>&quot;navy&quot;</span><span>,</span><span> </span><span>alpha</span><span> </span><span>=</span><span> </span><span>0.7</span><span>)</span><span> </span><span>+</span><span>
</span><span>my_theme</span><span>()</span><span>
</span></code></pre>
</div>
<p>Because I am interested in distinguishing healthy from arrhythmia ECGs, I am converting the output to binary format by combining all arrhythmia cases into one class.</p>
<div>
<pre><code><span>arrhythmia</span><span>$</span><span>diagnosis</span><span> </span><span>&lt;-</span><span> </span><span>ifelse</span><span>(</span><span>arrhythmia</span><span>$</span><span>class</span><span> </span><span>==</span><span> </span><span>1</span><span>,</span><span> </span><span>&quot;healthy&quot;</span><span>,</span><span> </span><span>&quot;arrhythmia&quot;</span><span>)</span><span>
</span><span>arrhythmia</span><span>$</span><span>diagnosis</span><span> </span><span>&lt;-</span><span> </span><span>as.factor</span><span>(</span><span>arrhythmia</span><span>$</span><span>diagnosis</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code><span>p</span><span>2</span><span> </span><span>&lt;-</span><span> </span><span>ggplot</span><span>(</span><span>arrhythmia</span><span>,</span><span> </span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>diagnosis</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_bar</span><span>(</span><span>fill</span><span> </span><span>=</span><span> </span><span>&quot;navy&quot;</span><span>,</span><span> </span><span>alpha</span><span> </span><span>=</span><span> </span><span>0.7</span><span>)</span><span> </span><span>+</span><span>
</span><span>my_theme</span><span>()</span><span>
</span></code></pre>
</div>
<div>
<pre><code><span>library</span><span>(</span><span>gridExtra</span><span>)</span><span>
</span><span>library</span><span>(</span><span>grid</span><span>)</span><span>
</span><span>grid.arrange</span><span>(</span><span>p</span><span>1</span><span>,</span><span> </span><span>p</span><span>2</span><span>,</span><span> </span><span>ncol</span><span> </span><span>=</span><span> </span><span>2</span><span>)</span><span>
</span></code></pre>
</div>
<p><img src="https://i2.wp.com/shiring.github.io/machine_learning/2017/02/27/h2o_files/figure-markdown_github/unnamed-chunk-8-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>With binary classification, we have almost the same numbers of healthy and arrhythmia cases in our dataset.</p>
<p>I am also interested in how much the normal and arrhythmia cases cluster in a Principal Component Analysis (PCA). I am first preparing the PCA plotting function and then run it on the feature data.</p>
<div>
<pre><code><span>library</span><span>(</span><span>pcaGoPromoter</span><span>)</span><span>
</span><span>pca_func</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>pcaOutput2</span><span>,</span><span> </span><span>group_name</span><span>){</span><span>
</span><span>centroids</span><span> </span><span>&lt;-</span><span> </span><span>aggregate</span><span>(</span><span>cbind</span><span>(</span><span>PC1</span><span>,</span><span> </span><span>PC2</span><span>)</span><span> </span><span>~</span><span> </span><span>groups</span><span>,</span><span> </span><span>pcaOutput2</span><span>,</span><span> </span><span>mean</span><span>)</span><span>
</span><span>conf.rgn</span><span> </span><span>&lt;-</span><span> </span><span>do.call</span><span>(</span><span>rbind</span><span>,</span><span> </span><span>lapply</span><span>(</span><span>unique</span><span>(</span><span>pcaOutput2</span><span>$</span><span>groups</span><span>),</span><span> </span><span>function</span><span>(</span><span>t</span><span>)</span><span>
</span><span>data.frame</span><span>(</span><span>groups</span><span> </span><span>=</span><span> </span><span>as.character</span><span>(</span><span>t</span><span>),</span><span>
</span><span>ellipse</span><span>(</span><span>cov</span><span>(</span><span>pcaOutput2</span><span>[</span><span>pcaOutput2</span><span>$</span><span>groups</span><span> </span><span>==</span><span> </span><span>t</span><span>,</span><span> </span><span>1</span><span>:</span><span>2</span><span>]),</span><span>
</span><span>centre</span><span> </span><span>=</span><span> </span><span>as.matrix</span><span>(</span><span>centroids</span><span>[</span><span>centroids</span><span>$</span><span>groups</span><span> </span><span>==</span><span> </span><span>t</span><span>,</span><span> </span><span>2</span><span>:</span><span>3</span><span>]),</span><span>
</span><span>level</span><span> </span><span>=</span><span> </span><span>0.95</span><span>),</span><span>
</span><span>stringsAsFactors</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>)))</span><span>
</span><span>plot</span><span> </span><span>&lt;-</span><span> </span><span>ggplot</span><span>(</span><span>data</span><span> </span><span>=</span><span> </span><span>pcaOutput2</span><span>,</span><span> </span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>PC1</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>PC2</span><span>,</span><span> </span><span>group</span><span> </span><span>=</span><span> </span><span>groups</span><span>,</span><span> </span><span>color</span><span> </span><span>=</span><span> </span><span>groups</span><span>))</span><span> </span><span>+</span><span> </span><span>geom_polygon</span><span>(</span><span>data</span><span> </span><span>=</span><span> </span><span>conf.rgn</span><span>,</span><span> </span><span>aes</span><span>(</span><span>fill</span><span> </span><span>=</span><span> </span><span>groups</span><span>),</span><span> </span><span>alpha</span><span> </span><span>=</span><span> </span><span>0.2</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_point</span><span>(</span><span>size</span><span> </span><span>=</span><span> </span><span>2</span><span>,</span><span> </span><span>alpha</span><span> </span><span>=</span><span> </span><span>0.5</span><span>)</span><span> </span><span>+</span><span> </span><span>labs</span><span>(</span><span>color</span><span> </span><span>=</span><span> </span><span>paste</span><span>(</span><span>group_name</span><span>),</span><span>
</span><span>fill</span><span> </span><span>=</span><span> </span><span>paste</span><span>(</span><span>group_name</span><span>),</span><span>
</span><span>x</span><span> </span><span>=</span><span> </span><span>paste0</span><span>(</span><span>&quot;PC1: &quot;</span><span>,</span><span> </span><span>round</span><span>(</span><span>pcaOutput</span><span>$</span><span>pov</span><span>[</span><span>1</span><span>],</span><span> </span><span>digits</span><span> </span><span>=</span><span> </span><span>2</span><span>)</span><span> </span><span>*</span><span> </span><span>100</span><span>,</span><span> </span><span>&quot;% variance&quot;</span><span>),</span><span>
</span><span>y</span><span> </span><span>=</span><span> </span><span>paste0</span><span>(</span><span>&quot;PC2: &quot;</span><span>,</span><span> </span><span>round</span><span>(</span><span>pcaOutput</span><span>$</span><span>pov</span><span>[</span><span>2</span><span>],</span><span> </span><span>digits</span><span> </span><span>=</span><span> </span><span>2</span><span>)</span><span> </span><span>*</span><span> </span><span>100</span><span>,</span><span> </span><span>&quot;% variance&quot;</span><span>))</span><span> </span><span>+</span><span>
</span><span>my_theme</span><span>()</span><span>
</span><span>return</span><span>(</span><span>plot</span><span>)</span><span>
</span><span>}</span><span>
</span></code></pre>
</div>
<div>
<pre><code><span>pcaOutput</span><span> </span><span>&lt;-</span><span> </span><span>pca</span><span>(</span><span>t</span><span>(</span><span>arrhythmia</span><span>[</span><span>-</span><span>c</span><span>(</span><span>280</span><span>,</span><span> </span><span>281</span><span>)]),</span><span> </span><span>printDropped</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>,</span><span> </span><span>scale</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>,</span><span> </span><span>center</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span>
</span><span>pcaOutput2</span><span> </span><span>&lt;-</span><span> </span><span>as.data.frame</span><span>(</span><span>pcaOutput</span><span>$</span><span>scores</span><span>)</span><span>
</span><span>pcaOutput2</span><span>$</span><span>groups</span><span> </span><span>&lt;-</span><span> </span><span>arrhythmia</span><span>$</span><span>class</span><span>
</span><span>p</span><span>1</span><span> </span><span>&lt;-</span><span> </span><span>pca_func</span><span>(</span><span>pcaOutput2</span><span>,</span><span> </span><span>group_name</span><span> </span><span>=</span><span> </span><span>&quot;class&quot;</span><span>)</span><span>
</span><span>pcaOutput2</span><span>$</span><span>groups</span><span> </span><span>&lt;-</span><span> </span><span>arrhythmia</span><span>$</span><span>diagnosis</span><span>
</span><span>p</span><span>2</span><span> </span><span>&lt;-</span><span> </span><span>pca_func</span><span>(</span><span>pcaOutput2</span><span>,</span><span> </span><span>group_name</span><span> </span><span>=</span><span> </span><span>&quot;diagnosis&quot;</span><span>)</span><span>
</span><span>grid.arrange</span><span>(</span><span>p</span><span>1</span><span>,</span><span> </span><span>p</span><span>2</span><span>,</span><span> </span><span>ncol</span><span> </span><span>=</span><span> </span><span>2</span><span>)</span><span>
</span></code></pre>
</div>
<p><img alt="" src="https://i2.wp.com/shiring.github.io/machine_learning/2017/02/27/h2o_files/figure-markdown_github/unnamed-chunk-10-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>The PCA shows that there is a big overlap between healthy and arrhythmia samples, i.e. there does not seem to be major global differences in all features. The class that is most distinct from all others seems to be class 9. I want to give the arrhythmia cases that are very different from the rest a stronger weight in the neural network, so I define a weight column where every sample outside the central PCA cluster will get a “2”, they will in effect be used twice in the model.</p>
<div>
<pre><code><span>weights</span><span> </span><span>&lt;-</span><span> </span><span>ifelse</span><span>(</span><span>pcaOutput2</span><span>$</span><span>PC1</span><span> </span><span>&lt;</span><span> </span><span>-5</span><span> </span><span>&amp;</span><span> </span><span>abs</span><span>(</span><span>pcaOutput2</span><span>$</span><span>PC2</span><span>)</span><span> </span><span>></span><span> </span><span>10</span><span>,</span><span> </span><span>2</span><span>,</span><span> </span><span>1</span><span>)</span><span>
</span></code></pre>
</div>
<p>I also want to know what the variance is within features.</p>
<div>
<pre><code><span>library</span><span>(</span><span>matrixStats</span><span>)</span><span>
</span><span>colvars</span><span> </span><span>&lt;-</span><span> </span><span>data.frame</span><span>(</span><span>feature</span><span> </span><span>=</span><span> </span><span>colnames</span><span>(</span><span>arrhythmia</span><span>[</span><span>-</span><span>c</span><span>(</span><span>280</span><span>,</span><span> </span><span>281</span><span>)]),</span><span>
</span><span>variance</span><span> </span><span>=</span><span> </span><span>colVars</span><span>(</span><span>as.matrix</span><span>(</span><span>arrhythmia</span><span>[</span><span>-</span><span>c</span><span>(</span><span>280</span><span>,</span><span> </span><span>281</span><span>)])))</span><span>
</span><span>subset</span><span>(</span><span>colvars</span><span>,</span><span> </span><span>variance</span><span> </span><span>></span><span> </span><span>50</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>feature</span><span> </span><span>=</span><span> </span><span>factor</span><span>(</span><span>feature</span><span>,</span><span> </span><span>levels</span><span> </span><span>=</span><span> </span><span>colnames</span><span>(</span><span>arrhythmia</span><span>[</span><span>-</span><span>c</span><span>(</span><span>280</span><span>,</span><span> </span><span>281</span><span>)])))</span><span> </span><span>%>%</span><span>
</span><span>ggplot</span><span>(</span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>feature</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>variance</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_bar</span><span>(</span><span>stat</span><span> </span><span>=</span><span> </span><span>&quot;identity&quot;</span><span>,</span><span> </span><span>fill</span><span> </span><span>=</span><span> </span><span>&quot;navy&quot;</span><span>,</span><span> </span><span>alpha</span><span> </span><span>=</span><span> </span><span>0.7</span><span>)</span><span> </span><span>+</span><span>
</span><span>my_theme</span><span>()</span><span> </span><span>+</span><span>
</span><span>theme</span><span>(</span><span>axis.text.x</span><span> </span><span>=</span><span> </span><span>element_text</span><span>(</span><span>angle</span><span> </span><span>=</span><span> </span><span>90</span><span>,</span><span> </span><span>vjust</span><span> </span><span>=</span><span> </span><span>0.5</span><span>,</span><span> </span><span>hjust</span><span> </span><span>=</span><span> </span><span>1</span><span>))</span><span>
</span></code></pre>
</div>
<p><img src="https://i0.wp.com/shiring.github.io/machine_learning/2017/02/27/h2o_files/figure-markdown_github/unnamed-chunk-12-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>Features with low variance are less likely to strongly contribute to a differentiation between healthy and arrhythmia cases, so I am going to remove them. I am also concatenating the weights column:</p>
<div>
<pre><code><span>arrhythmia_subset</span><span> </span><span>&lt;-</span><span> </span><span>cbind</span><span>(</span><span>weights</span><span>,</span><span> </span><span>arrhythmia</span><span>[,</span><span> </span><span>c</span><span>(</span><span>281</span><span>,</span><span> </span><span>280</span><span>,</span><span> </span><span>which</span><span>(</span><span>colvars</span><span>$</span><span>variance</span><span> </span><span>></span><span> </span><span>50</span><span>))])</span><span>
</span></code></pre>
</div>
<p></p>
<h3>Working with rsparkling and h2o</h3>
<p>Now that I have my final data frame for modeling, I copy it to the Spark instance. For working with <strong>h2o</strong> functions, the data needs to be converted from a Spark DataFrame to an H2O Frame. This is done with the <em>as_h2o_frame()</em> function.</p>
<div>
<pre><code><span>arrhythmia_sc</span><span> </span><span>&lt;-</span><span> </span><span>copy_to</span><span>(</span><span>sc</span><span>,</span><span> </span><span>arrhythmia_subset</span><span>)</span><span>
</span><span>arrhythmia_hf</span><span> </span><span>&lt;-</span><span> </span><span>as_h2o_frame</span><span>(</span><span>sc</span><span>,</span><span> </span><span>arrhythmia_sc</span><span>,</span><span> </span><span>strict_version_check</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>)</span><span>
</span></code></pre>
</div>
<p>We can now access all functions from the <strong>h2o</strong> package that are built to work on H2O Frames. A useful such function is <em>h2o.describe()</em>. It is similar to base R’s <em>summary()</em> function but outputs many more descriptive measures for our data. To get a good overview about these measures, I am going to plot them.</p>
<div>
<pre><code><span>library</span><span>(</span><span>tidyr</span><span>)</span><span> </span><span># for gathering
</span><span>h</span><span>2</span><span>o.describe</span><span>(</span><span>arrhythmia_hf</span><span>[,</span><span> </span><span>-1</span><span>])</span><span> </span><span>%>%</span><span> </span><span># excluding the weights column
</span><span> </span><span>gather</span><span>(</span><span>x</span><span>,</span><span> </span><span>y</span><span>,</span><span> </span><span>Zeros</span><span>:</span><span>Sigma</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>group</span><span> </span><span>=</span><span> </span><span>ifelse</span><span>(</span><span>x</span><span> </span><span>%in%</span><span> </span><span>c</span><span>(</span><span>&quot;Min&quot;</span><span>,</span><span> </span><span>&quot;Max&quot;</span><span>,</span><span> </span><span>&quot;Mean&quot;</span><span>),</span><span> </span><span>&quot;min, mean, max&quot;</span><span>,</span><span> </span><span>ifelse</span><span>(</span><span>x</span><span> </span><span>%in%</span><span> </span><span>c</span><span>(</span><span>&quot;NegInf&quot;</span><span>,</span><span> </span><span>&quot;PosInf&quot;</span><span>),</span><span> </span><span>&quot;Inf&quot;</span><span>,</span><span> </span><span>&quot;sigma, zeros&quot;</span><span>)))</span><span> </span><span>%>%</span><span> </span><span># separating them into facets makes them easier to see
</span><span> </span><span>mutate</span><span>(</span><span>Label</span><span> </span><span>=</span><span> </span><span>factor</span><span>(</span><span>Label</span><span>,</span><span> </span><span>levels</span><span> </span><span>=</span><span> </span><span>colnames</span><span>(</span><span>arrhythmia_hf</span><span>[,</span><span> </span><span>-1</span><span>])))</span><span> </span><span>%>%</span><span>
</span><span>ggplot</span><span>(</span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>Label</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>as.numeric</span><span>(</span><span>y</span><span>),</span><span> </span><span>color</span><span> </span><span>=</span><span> </span><span>x</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_point</span><span>(</span><span>size</span><span> </span><span>=</span><span> </span><span>4</span><span>,</span><span> </span><span>alpha</span><span> </span><span>=</span><span> </span><span>0.6</span><span>)</span><span> </span><span>+</span><span>
</span><span>scale_color_brewer</span><span>(</span><span>palette</span><span> </span><span>=</span><span> </span><span>&quot;Set1&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>my_theme</span><span>()</span><span> </span><span>+</span><span>
</span><span>theme</span><span>(</span><span>axis.text.x</span><span> </span><span>=</span><span> </span><span>element_text</span><span>(</span><span>angle</span><span> </span><span>=</span><span> </span><span>90</span><span>,</span><span> </span><span>vjust</span><span> </span><span>=</span><span> </span><span>0.5</span><span>,</span><span> </span><span>hjust</span><span> </span><span>=</span><span> </span><span>1</span><span>))</span><span> </span><span>+</span><span>
</span><span>facet_grid</span><span>(</span><span>group</span><span> </span><span>~</span><span> </span><span>.</span><span>,</span><span> </span><span>scales</span><span> </span><span>=</span><span> </span><span>&quot;free&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>&quot;Feature&quot;</span><span>,</span><span>
</span><span>y</span><span> </span><span>=</span><span> </span><span>&quot;Value&quot;</span><span>,</span><span>
</span><span>color</span><span> </span><span>=</span><span> </span><span>&quot;&quot;</span><span>)</span><span>
</span></code></pre>
</div>
<p><img src="https://i1.wp.com/shiring.github.io/machine_learning/2017/02/27/h2o_files/figure-markdown_github/unnamed-chunk-16-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>I am also interested in the correlation between features and the output. We can use the <em>h2o.cor()</em> function to calculate the correlation matrix. It is again much easier to understand the data when we visualize it, so I am going to create another plot.</p>
<div>
<pre><code><span>library</span><span>(</span><span>reshape2</span><span>)</span><span> </span><span># for melting
</span><span>
</span><span>arrhythmia_hf</span><span>[,</span><span> </span><span>2</span><span>]</span><span> </span><span>&lt;-</span><span> </span><span>h</span><span>2</span><span>o.asfactor</span><span>(</span><span>arrhythmia_hf</span><span>[,</span><span> </span><span>2</span><span>])</span><span> </span><span># diagnosis is now a characer column and we need to convert it again
</span><span>arrhythmia_hf</span><span>[,</span><span> </span><span>3</span><span>]</span><span> </span><span>&lt;-</span><span> </span><span>h</span><span>2</span><span>o.asfactor</span><span>(</span><span>arrhythmia_hf</span><span>[,</span><span> </span><span>3</span><span>])</span><span> </span><span># same for class
</span><span>
</span><span>cor</span><span> </span><span>&lt;-</span><span> </span><span>h</span><span>2</span><span>o.cor</span><span>(</span><span>arrhythmia_hf</span><span>[,</span><span> </span><span>-</span><span>c</span><span>(</span><span>1</span><span>,</span><span> </span><span>3</span><span>)])</span><span>
</span><span>rownames</span><span>(</span><span>cor</span><span>)</span><span> </span><span>&lt;-</span><span> </span><span>colnames</span><span>(</span><span>cor</span><span>)</span><span>
</span><span>melt</span><span>(</span><span>cor</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>Var2</span><span> </span><span>=</span><span> </span><span>rep</span><span>(</span><span>rownames</span><span>(</span><span>cor</span><span>),</span><span> </span><span>nrow</span><span>(</span><span>cor</span><span>)))</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>Var2</span><span> </span><span>=</span><span> </span><span>factor</span><span>(</span><span>Var2</span><span>,</span><span> </span><span>levels</span><span> </span><span>=</span><span> </span><span>colnames</span><span>(</span><span>cor</span><span>)))</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>variable</span><span> </span><span>=</span><span> </span><span>factor</span><span>(</span><span>variable</span><span>,</span><span> </span><span>levels</span><span> </span><span>=</span><span> </span><span>colnames</span><span>(</span><span>cor</span><span>)))</span><span> </span><span>%>%</span><span>
</span><span>ggplot</span><span>(</span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>variable</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>Var2</span><span>,</span><span> </span><span>fill</span><span> </span><span>=</span><span> </span><span>value</span><span>))</span><span> </span><span>+</span><span> </span><span>geom_tile</span><span>(</span><span>width</span><span> </span><span>=</span><span> </span><span>0.9</span><span>,</span><span> </span><span>height</span><span> </span><span>=</span><span> </span><span>0.9</span><span>)</span><span> </span><span>+</span><span>
</span><span>scale_fill_gradient2</span><span>(</span><span>low</span><span> </span><span>=</span><span> </span><span>&quot;white&quot;</span><span>,</span><span> </span><span>high</span><span> </span><span>=</span><span> </span><span>&quot;red&quot;</span><span>,</span><span> </span><span>name</span><span> </span><span>=</span><span> </span><span>&quot;Cor.&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>my_theme</span><span>()</span><span> </span><span>+</span><span>
</span><span>theme</span><span>(</span><span>axis.text.x</span><span> </span><span>=</span><span> </span><span>element_text</span><span>(</span><span>angle</span><span> </span><span>=</span><span> </span><span>90</span><span>,</span><span> </span><span>vjust</span><span> </span><span>=</span><span> </span><span>0.5</span><span>,</span><span> </span><span>hjust</span><span> </span><span>=</span><span> </span><span>1</span><span>))</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>&quot;&quot;</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>&quot;&quot;</span><span>)</span><span>
</span></code></pre>
</div>
<p><img src="https://i2.wp.com/shiring.github.io/machine_learning/2017/02/27/h2o_files/figure-markdown_github/unnamed-chunk-17-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p></p>
<h3>Training, test and validation data</h3>
<p>Now we can use the <em>h2o.splitFrame()</em> function to split the data into training, validation and test data. Here, I am using 70% for training and 15% each for validation and testing. We could also just split the data into two sections, a training and test set but when we have sufficient samples, it is a good idea to evaluate model performance on an independent test set on top of training with a validation set. Because we can easily overfit a model, we want to get an idea about how generalizable it is – this we can only assess by looking at how well it works on previously unknown data.</p>
<p>I am also defining response, feature and weight column names now.</p>
<div>
<pre><code><span>splits</span><span> </span><span>&lt;-</span><span> </span><span>h</span><span>2</span><span>o.splitFrame</span><span>(</span><span>arrhythmia_hf</span><span>,</span><span> </span><span>ratios</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>0.7</span><span>,</span><span> </span><span>0.15</span><span>),</span><span> </span><span>seed</span><span> </span><span>=</span><span> </span><span>1</span><span>)</span><span>
</span><span>train</span><span> </span><span>&lt;-</span><span> </span><span>splits</span><span>[[</span><span>1</span><span>]]</span><span>
</span><span>valid</span><span> </span><span>&lt;-</span><span> </span><span>splits</span><span>[[</span><span>2</span><span>]]</span><span>
</span><span>test</span><span> </span><span>&lt;-</span><span> </span><span>splits</span><span>[[</span><span>3</span><span>]]</span><span>
</span><span>response</span><span> </span><span>&lt;-</span><span> </span><span>&quot;diagnosis&quot;</span><span>
</span><span>weights</span><span> </span><span>&lt;-</span><span> </span><span>&quot;weights&quot;</span><span>
</span><span>features</span><span> </span><span>&lt;-</span><span> </span><span>setdiff</span><span>(</span><span>colnames</span><span>(</span><span>train</span><span>),</span><span> </span><span>c</span><span>(</span><span>response</span><span>,</span><span> </span><span>weights</span><span>,</span><span> </span><span>&quot;class&quot;</span><span>))</span><span>
</span></code></pre>
</div>
<div>
<pre><code><span>summary</span><span>(</span><span>train</span><span>$</span><span>diagnosis</span><span>,</span><span> </span><span>exact_quantiles</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code>##  diagnosis      
##  healthy   :163 
##  arrhythmia:155
</code></pre>
</div>
<div>
<pre><code><span>summary</span><span>(</span><span>valid</span><span>$</span><span>diagnosis</span><span>,</span><span> </span><span>exact_quantiles</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code>##  diagnosis     
##  healthy   :43 
##  arrhythmia:25
</code></pre>
</div>
<div>
<pre><code><span>summary</span><span>(</span><span>test</span><span>$</span><span>diagnosis</span><span>,</span><span> </span><span>exact_quantiles</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code>##  diagnosis     
##  healthy   :39 
##  arrhythmia:27
</code></pre>
</div>
<p>If we had more categorical features, we could use the <em>h2o.interaction()</em> function to define interaction terms, but since we only have numeric features here, we don’t need this.</p>
<p>We can also run a PCA on the training data, using the <em>h2o.prcomp()</em> function to calculate the singular value decomposition of the Gram matrix with the power method.</p>
<div>
<pre><code><span>pca</span><span> </span><span>&lt;-</span><span> </span><span>h</span><span>2</span><span>o.prcomp</span><span>(</span><span>training_frame</span><span> </span><span>=</span><span> </span><span>train</span><span>,</span><span>
</span><span>x</span><span> </span><span>=</span><span> </span><span>features</span><span>,</span><span>
</span><span>validation_frame</span><span> </span><span>=</span><span> </span><span>valid</span><span>,</span><span>
</span><span>transform</span><span> </span><span>=</span><span> </span><span>&quot;NORMALIZE&quot;</span><span>,</span><span>
</span><span>k</span><span> </span><span>=</span><span> </span><span>3</span><span>,</span><span>
</span><span>seed</span><span> </span><span>=</span><span> </span><span>42</span><span>)</span><span>
</span><span>pca</span><span>
</span></code></pre>
</div>
<div>
<pre><code>## Model Details:
## ==============
## 
## H2ODimReductionModel: pca
## Model ID:  PCA_model_R_1488113493291_1 
## Importance of components: 
##                             pc1      pc2      pc3
## Standard deviation     0.598791 0.516364 0.424850
## Proportion of Variance 0.162284 0.120680 0.081695
## Cumulative Proportion  0.162284 0.282965 0.364660
## 
## 
## H2ODimReductionMetrics: pca
## 
## No model metrics available for PCA
## H2ODimReductionMetrics: pca
## 
## No model metrics available for PCA
</code></pre>
</div>
<div>
<pre><code><span>eigenvec</span><span> </span><span>&lt;-</span><span> </span><span>as.data.frame</span><span>(</span><span>pca</span><span>@</span><span>model</span><span>$</span><span>eigenvectors</span><span>)</span><span>
</span><span>eigenvec</span><span>$</span><span>label</span><span> </span><span>&lt;-</span><span> </span><span>features</span><span>
</span><span>ggplot</span><span>(</span><span>eigenvec</span><span>,</span><span> </span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>pc1</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>pc2</span><span>,</span><span> </span><span>label</span><span> </span><span>=</span><span> </span><span>label</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_point</span><span>(</span><span>color</span><span> </span><span>=</span><span> </span><span>&quot;navy&quot;</span><span>,</span><span> </span><span>alpha</span><span> </span><span>=</span><span> </span><span>0.7</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_text_repel</span><span>()</span><span> </span><span>+</span><span>
</span><span>my_theme</span><span>()</span><span>
</span></code></pre>
</div>
<p><img alt="" src="https://i1.wp.com/shiring.github.io/machine_learning/2017/02/27/h2o_files/figure-markdown_github/unnamed-chunk-23-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p></p>
<h3>Modeling</h3>
<p>Now, we can build a deep neural network model. We can specify quite a few parameters, like</p>
<ul>
<li>
<p><strong>Cross-validation</strong>: Cross validation can tell us the training and validation errors for each model. The final model will be overwritten with the best model, if we don’t specify otherwise.</p>
</li>
<li>
<p><strong>Adaptive learning rate</strong>: For deep learning with h2o, we by default use stochastic gradient descent optimization with an an adaptive learning rate. The two corresponding parameters <em>rho</em> and <em>epsilon</em> help us find global (or near enough) optima.</p>
</li>
<li>
<p><strong>Activation function</strong>: The activation function defines the node output relative to a given set of inputs. We want our activation function to be non-linear and continuously differentiable.</p>
</li>
<li>
<p><strong>Hidden nodes</strong>: Defines the number of hidden layers and the number of nodes per layer.</p>
</li>
<li>
<p><strong>Epochs</strong>: Increasing the number of epochs (one full training cycle on all training samples) can increase model performance, but we also run the risk of overfitting. To determine the optimal number of epochs, we need to use early stopping.</p>
</li>
<li>
<p><strong>Early stopping</strong>: By default, early stopping is enabled. This means that training will be stopped when we reach a certain validation error to prevent overfitting.</p>
</li>
</ul>
<p>Of course, you need quite a bit of experience and intuition to hit on a good combination of parameters. That’s why it usually makes sense to do a grid search for hyper-parameter tuning. Here, I want to focus on building and evaluating deep learning models, though. I will cover grid search in next week’s post.</p>
<div>
<pre><code><span>dl_model</span><span> </span><span>&lt;-</span><span> </span><span>h</span><span>2</span><span>o.deeplearning</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>features</span><span>,</span><span>
</span><span>y</span><span> </span><span>=</span><span> </span><span>response</span><span>,</span><span>
</span><span>weights_column</span><span> </span><span>=</span><span> </span><span>weights</span><span>,</span><span>
</span><span>model_id</span><span> </span><span>=</span><span> </span><span>&quot;dl_model&quot;</span><span>,</span><span>
</span><span>training_frame</span><span> </span><span>=</span><span> </span><span>train</span><span>,</span><span>
</span><span>validation_frame</span><span> </span><span>=</span><span> </span><span>valid</span><span>,</span><span>
</span><span>nfolds</span><span> </span><span>=</span><span> </span><span>15</span><span>,</span><span> </span><span># 10x cross validation
</span><span> </span><span>keep_cross_validation_fold_assignment</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>,</span><span>
</span><span>fold_assignment</span><span> </span><span>=</span><span> </span><span>&quot;Stratified&quot;</span><span>,</span><span>
</span><span>activation</span><span> </span><span>=</span><span> </span><span>&quot;RectifierWithDropout&quot;</span><span>,</span><span>
</span><span>score_each_iteration</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>,</span><span>
</span><span>hidden</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>200</span><span>,</span><span> </span><span>200</span><span>,</span><span> </span><span>200</span><span>,</span><span> </span><span>200</span><span>,</span><span> </span><span>200</span><span>),</span><span> </span><span># 5 hidden layers, each of 200 neurons
</span><span> </span><span>epochs</span><span> </span><span>=</span><span> </span><span>100</span><span>,</span><span>
</span><span>variable_importances</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>,</span><span>
</span><span>export_weights_and_biases</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>,</span><span>
</span><span>seed</span><span> </span><span>=</span><span> </span><span>42</span><span>)</span><span>
</span></code></pre>
</div>
<p>Because training can take a while, depending on how many samples, features, nodes and hidden layers you are training on, it is a good idea to save your model.</p>
<div>
<pre><code><span>h</span><span>2</span><span>o.saveModel</span><span>(</span><span>dl_model</span><span>,</span><span> </span><span>path</span><span> </span><span>=</span><span> </span><span>&quot;dl_model&quot;</span><span>)</span><span>
</span></code></pre>
</div>
<p>We can then re-load the model again any time to check the model quality and make predictions on new data.</p>
<div>
<pre><code><span>dl_model</span><span> </span><span>&lt;-</span><span> </span><span>h</span><span>2</span><span>o.loadModel</span><span>(</span><span>&quot;dl_model/dl_model&quot;</span><span>)</span><span>
</span></code></pre>
</div>
<p></p>
<h3>Model performance</h3>
<p>We now want to know how our model performed on the validation data. The <em>summary()</em> function will give us a detailed overview of our model. I am not showing the output here, because it is quite extensive.</p>
<div>
<pre><code><span>summary</span><span>(</span><span>dl_model</span><span>)</span><span>
</span></code></pre>
</div>
<p>One performance metric we are usually interested in is the mean per class error for training and validation data.</p>
<div>
<pre><code><span>h</span><span>2</span><span>o.mean_per_class_error</span><span>(</span><span>dl_model</span><span>,</span><span> </span><span>train</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>,</span><span> </span><span>valid</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>,</span><span> </span><span>xval</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code>##           train          valid            xval 
## 0.0304878 0.2232558 0.2257781
</code></pre>
</div>
<p>The confusion matrix tells us, how many classes have been predicted correctly and how many predictions were accurate. Here, we see the errors in predictions on validation data</p>
<div>
<pre><code><span>h</span><span>2</span><span>o.confusionMatrix</span><span>(</span><span>dl_model</span><span>,</span><span> </span><span>valid</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code>## Confusion Matrix (vertical: actual; across: predicted)  for max f1 @ threshold = 0.00425904880659062:
##            arrhythmia healthy    Error    Rate
## arrhythmia         15      10 0.400000  =10/25
## healthy             2      41 0.046512   =2/43
## Totals             17      51 0.176471  =12/68
</code></pre>
</div>
<p>We can also plot the classification error over all epochs or samples.</p>
<div>
<pre><code><span>plot</span><span>(</span><span>dl_model</span><span>,</span><span>
</span><span>timestep</span><span> </span><span>=</span><span> </span><span>&quot;epochs&quot;</span><span>,</span><span>
</span><span>metric</span><span> </span><span>=</span><span> </span><span>&quot;classification_error&quot;</span><span>)</span><span>
</span></code></pre>
</div>
<p><img src="https://i0.wp.com/shiring.github.io/machine_learning/2017/02/27/h2o_files/figure-markdown_github/unnamed-chunk-33-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<div>
<pre><code><span>plot</span><span>(</span><span>dl_model</span><span>,</span><span>
</span><span>timestep</span><span> </span><span>=</span><span> </span><span>&quot;samples&quot;</span><span>,</span><span>
</span><span>metric</span><span> </span><span>=</span><span> </span><span>&quot;classification_error&quot;</span><span>)</span><span>
</span></code></pre>
</div>
<p><img src="https://i0.wp.com/shiring.github.io/machine_learning/2017/02/27/h2o_files/figure-markdown_github/unnamed-chunk-34-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>Next to the classification error, we are usually interested in the logistic loss (negative log-likelihood or log loss). It describes the sum of errors for each sample in the training or validation data or the negative logarithm of the likelihood of error for a given prediction/ classification. Simply put, the lower the loss, the better the model (if we ignore potential overfitting).</p>
<div>
<pre><code><span>plot</span><span>(</span><span>dl_model</span><span>,</span><span>
</span><span>timestep</span><span> </span><span>=</span><span> </span><span>&quot;epochs&quot;</span><span>,</span><span>
</span><span>metric</span><span> </span><span>=</span><span> </span><span>&quot;logloss&quot;</span><span>)</span><span>
</span></code></pre>
</div>
<p><img src="https://i2.wp.com/shiring.github.io/machine_learning/2017/02/27/h2o_files/figure-markdown_github/unnamed-chunk-36-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>We can also plot the mean squared error (MSE). The MSE tells us the average of the prediction errors squared, i.e. the estimator’s variance and bias. The closer to zero, the better a model.</p>
<div>
<pre><code><span>plot</span><span>(</span><span>dl_model</span><span>,</span><span>
</span><span>timestep</span><span> </span><span>=</span><span> </span><span>&quot;epochs&quot;</span><span>,</span><span>
</span><span>metric</span><span> </span><span>=</span><span> </span><span>&quot;rmse&quot;</span><span>)</span><span>
</span></code></pre>
</div>
<p><img src="https://i0.wp.com/shiring.github.io/machine_learning/2017/02/27/h2o_files/figure-markdown_github/unnamed-chunk-37-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>Next, we want to know the area under the curve (AUC). AUC is an important metric for measuring binary classification model performances. It gives the area under the curve, i.e. the integral, of true positive vs false positive rates. The closer to 1, the better a model. AUC is especially useful, when we have unbalanced datasets (meaning datasets where one class is much more common than the other), because it is independent of class labels.</p>
<div>
<pre><code><span>h</span><span>2</span><span>o.auc</span><span>(</span><span>dl_model</span><span>,</span><span> </span><span>train</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code>## [1] 0.9864582
</code></pre>
</div>
<div>
<pre><code><span>h</span><span>2</span><span>o.auc</span><span>(</span><span>dl_model</span><span>,</span><span> </span><span>valid</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code>## [1] 0.852093
</code></pre>
</div>
<div>
<pre><code><span>h</span><span>2</span><span>o.auc</span><span>(</span><span>dl_model</span><span>,</span><span> </span><span>xval</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code>## [1] 0.8577735
</code></pre>
</div>
<p>The weights for connecting two adjacent layers and per-neuron biases that we specified the model to save, can be accessed with:</p>
<div>
<pre><code><span>w</span><span> </span><span>&lt;-</span><span> </span><span>h</span><span>2</span><span>o.weights</span><span>(</span><span>dl_model</span><span>,</span><span> </span><span>matrix_id</span><span> </span><span>=</span><span> </span><span>1</span><span>)</span><span>
</span><span>b</span><span> </span><span>&lt;-</span><span> </span><span>h</span><span>2</span><span>o.biases</span><span>(</span><span>dl_model</span><span>,</span><span> </span><span>vector_id</span><span> </span><span>=</span><span> </span><span>1</span><span>)</span><span>
</span></code></pre>
</div>
<p>Variable importance can be extracted as well (but keep in mind, that variable importance in deep neural networks is difficult to assess and should be considered only as rough estimates).</p>
<div>
<pre><code><span>h</span><span>2</span><span>o.varimp</span><span>(</span><span>dl_model</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code>## Variable Importances: 
##   variable relative_importance scaled_importance percentage
## 1     V169            1.000000          1.000000   0.013925
## 2     V239            0.987290          0.987290   0.013748
## 3     V103            0.913953          0.913953   0.012727
## 4      V15            0.907422          0.907422   0.012636
## 5      V91            0.904267          0.904267   0.012592
## 
## ---
##    variable relative_importance scaled_importance percentage
## 85      V88            0.717914          0.717914   0.009997
## 86     V269            0.715800          0.715800   0.009968
## 87     V137            0.712923          0.712923   0.009928
## 88     V168            0.711402          0.711402   0.009906
## 89      V33            0.707356          0.707356   0.009850
## 90     V219            0.696149          0.696149   0.009694
</code></pre>
</div>
<div>
<pre><code><span>#h2o.varimp_plot(dl_model)
</span></code></pre>
</div>
<p></p>
<h4>Test data</h4>
<p>Now that we have a good idea about model performance on validation data, we want to know how it performed on unseen test data. A good model should find an optimal balance between accuracy on training and test data. A model that has 0% error on the training data but 40% error on the test data is in effect useless. It overfit on the training data and is thus not able to generalize to unknown data.</p>
<div>
<pre><code><span>perf</span><span> </span><span>&lt;-</span><span> </span><span>h</span><span>2</span><span>o.performance</span><span>(</span><span>dl_model</span><span>,</span><span> </span><span>test</span><span>)</span><span>
</span><span>perf</span><span>
</span></code></pre>
</div>
<div>
<pre><code>## H2OBinomialMetrics: deeplearning
## 
## MSE:  0.2154912
## RMSE:  0.4642103
## LogLoss:  1.378809
## Mean Per-Class Error:  0.2250712
## AUC:  0.7796771
## Gini:  0.5593542
## 
## Confusion Matrix (vertical: actual; across: predicted) for F1-optimal threshold:
##            arrhythmia healthy    Error      Rate
## arrhythmia         19       8 0.296296    =8/27
## healthy                6      33 0.153846   =6/39
## Totals                 25      41 0.212121   =14/66
## 
## Maximum Metrics: Maximum metrics at their respective thresholds
##                         metric threshold    value idx
## 1                       max f1  0.132564 0.825000  40
## 2                       max f2  0.000002 0.894495  61
## 3                 max f0point5  0.132564 0.812808  40
## 4                 max accuracy  0.132564 0.787879  40
## 5                max precision  0.982938 1.000000   0
## 6                   max recall  0.000002 1.000000  61
## 7              max specificity  0.982938 1.000000   0
## 8             max absolute_mcc  0.132564 0.557317  40
## 9   max min_per_class_accuracy  0.837616 0.743590  34
## 10 max mean_per_class_accuracy  0.132564 0.774929  40
## 
## Gains/Lift Table: Extract with `h2o.gainsLift(&lt;model>, &lt;data>)` or `h2o.gainsLift(&lt;model>, valid=&lt;T/F>, xval=&lt;T/F>)`
</code></pre>
</div>
<p>Plotting the test performance’s AUC plot shows us approximately how good the predictions are.</p>
<div>
<pre><code><span>plot</span><span>(</span><span>perf</span><span>)</span><span>
</span></code></pre>
</div>
<p><img src="https://i2.wp.com/shiring.github.io/machine_learning/2017/02/27/h2o_files/figure-markdown_github/unnamed-chunk-46-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>We also want to know the log loss, MSE and AUC values, as well as other model metrics for the test data:</p>
<div>
<pre><code><span>h</span><span>2</span><span>o.logloss</span><span>(</span><span>perf</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code>## [1] 1.378809
</code></pre>
</div>
<div>
<pre><code><span>h</span><span>2</span><span>o.mse</span><span>(</span><span>perf</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code>## [1] 0.2154912
</code></pre>
</div>
<div>
<pre><code><span>h</span><span>2</span><span>o.auc</span><span>(</span><span>perf</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code>## [1] 0.7796771
</code></pre>
</div>
<div>
<pre><code><span>head</span><span>(</span><span>h</span><span>2</span><span>o.metric</span><span>(</span><span>perf</span><span>))</span><span>
</span></code></pre>
</div>
<div>
<pre><code>## Metrics for Thresholds: Binomial metrics as a function of classification thresholds
##   threshold       f1       f2 f0point5 accuracy precision   recall
## 1  0.982938 0.050000 0.031847 0.116279 0.424242  1.000000 0.025641
## 2  0.982811 0.097561 0.063291 0.212766 0.439394  1.000000 0.051282
## 3  0.982460 0.095238 0.062893 0.196078 0.424242  0.666667 0.051282
## 4  0.982256 0.139535 0.093750 0.272727 0.439394  0.750000 0.076923
## 5  0.982215 0.181818 0.124224 0.338983 0.454545  0.800000 0.102564
## 6  0.981170 0.177778 0.123457 0.317460 0.439394  0.666667 0.102564
##   specificity absolute_mcc min_per_class_accuracy mean_per_class_accuracy
## 1    1.000000     0.103203               0.025641                0.512821
## 2    1.000000     0.147087               0.051282                0.525641
## 3    0.962963     0.033624               0.051282                0.507123
## 4    0.962963     0.082188               0.076923                0.519943
## 5    0.962963     0.121754               0.102564                0.532764
## 6    0.925926     0.048725               0.102564                0.514245
##   tns fns fps tps      tnr      fnr      fpr      tpr idx
## 1  27  38   0   1 1.000000 0.974359 0.000000 0.025641   0
## 2  27  37   0   2 1.000000 0.948718 0.000000 0.051282   1
## 3  26  37   1   2 0.962963 0.948718 0.037037 0.051282   2
## 4  26  36   1   3 0.962963 0.923077 0.037037 0.076923   3
## 5  26  35   1   4 0.962963 0.897436 0.037037 0.102564   4
## 6  25  35   2   4 0.925926 0.897436 0.074074 0.102564   5
</code></pre>
</div>
<p>The confusion matrix alone can be seen with the <em>h2o.confusionMatrix()</em> function, but is is also part of the performance summary.</p>
<div>
<pre><code><span>h</span><span>2</span><span>o.confusionMatrix</span><span>(</span><span>dl_model</span><span>,</span><span> </span><span>test</span><span>)</span><span>
</span></code></pre>
</div>
<p>The final predictions with probabilities can be extracted with the <em>h2o.predict()</em> function. Beware though, that the number of correct and wrong classifications can be slightly different from the confusion matrix above. Here, I combine the predictions with the actual test diagnoses and classes into a data frame. For plotting I also want to have a column, that tells me whether the predictions were correct. By default, a prediction probability above 0.5 will get scored as a prediction for the respective category. I find it often makes sense to be more stringent with this, though and set a higher threshold. Therefore, I am creating another column with stringent predictions, where I only count predictions that were made with more than 80% probability. Everything that does not fall within this range gets scored as “uncertain”. For these stringent predictions, I am also creating a column that tells me whether they were accurate.</p>
<div>
<pre><code><span>finalRf_predictions</span><span> </span><span>&lt;-</span><span> </span><span>data.frame</span><span>(</span><span>class</span><span> </span><span>=</span><span> </span><span>as.vector</span><span>(</span><span>test</span><span>$</span><span>class</span><span>),</span><span> </span><span>actual</span><span> </span><span>=</span><span> </span><span>as.vector</span><span>(</span><span>test</span><span>$</span><span>diagnosis</span><span>),</span><span> </span><span>as.data.frame</span><span>(</span><span>h</span><span>2</span><span>o.predict</span><span>(</span><span>object</span><span> </span><span>=</span><span> </span><span>dl_model</span><span>,</span><span> </span><span>newdata</span><span> </span><span>=</span><span> </span><span>test</span><span>)))</span><span>
</span><span>finalRf_predictions</span><span>$</span><span>accurate</span><span> </span><span>&lt;-</span><span> </span><span>ifelse</span><span>(</span><span>finalRf_predictions</span><span>$</span><span>actual</span><span> </span><span>==</span><span> </span><span>finalRf_predictions</span><span>$</span><span>predict</span><span>,</span><span> </span><span>&quot;yes&quot;</span><span>,</span><span> </span><span>&quot;no&quot;</span><span>)</span><span>
</span><span>finalRf_predictions</span><span>$</span><span>predict_stringent</span><span> </span><span>&lt;-</span><span> </span><span>ifelse</span><span>(</span><span>finalRf_predictions</span><span>$</span><span>arrhythmia</span><span> </span><span>></span><span> </span><span>0.8</span><span>,</span><span> </span><span>&quot;arrhythmia&quot;</span><span>,</span><span> </span><span>ifelse</span><span>(</span><span>finalRf_predictions</span><span>$</span><span>healthy</span><span> </span><span>></span><span> </span><span>0.8</span><span>,</span><span> </span><span>&quot;healthy&quot;</span><span>,</span><span> </span><span>&quot;uncertain&quot;</span><span>))</span><span>
</span><span>finalRf_predictions</span><span>$</span><span>accurate_stringent</span><span> </span><span>&lt;-</span><span> </span><span>ifelse</span><span>(</span><span>finalRf_predictions</span><span>$</span><span>actual</span><span> </span><span>==</span><span> </span><span>finalRf_predictions</span><span>$</span><span>predict_stringent</span><span>,</span><span> </span><span>&quot;yes&quot;</span><span>,</span><span> </span><span>ifelse</span><span>(</span><span>finalRf_predictions</span><span>$</span><span>predict_stringent</span><span> </span><span>==</span><span> </span><span>&quot;uncertain&quot;</span><span>,</span><span> </span><span>&quot;na&quot;</span><span>,</span><span> </span><span>&quot;no&quot;</span><span>))</span><span>
</span></code></pre>
</div>
<div>
<pre><code><span>finalRf_predictions</span><span> </span><span>%>%</span><span>
</span><span>group_by</span><span>(</span><span>actual</span><span>,</span><span> </span><span>predict</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>summarise</span><span>(</span><span>n</span><span> </span><span>=</span><span> </span><span>n</span><span>())</span><span>
</span></code></pre>
</div>
<div>
<pre><code>## Source: local data frame [4 x 3]
## Groups: actual [?]
## 
##       actual    predict     n
##       &lt;fctr>     &lt;fctr> &lt;int>
## 1 arrhythmia arrhythmia    16
## 2 arrhythmia    healthy    11
## 3    healthy arrhythmia     6
## 4    healthy    healthy    33
</code></pre>
</div>
<div>
<pre><code><span>finalRf_predictions</span><span> </span><span>%>%</span><span>
</span><span>group_by</span><span>(</span><span>actual</span><span>,</span><span> </span><span>predict_stringent</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>summarise</span><span>(</span><span>n</span><span> </span><span>=</span><span> </span><span>n</span><span>())</span><span>
</span></code></pre>
</div>
<div>
<pre><code>## Source: local data frame [6 x 3]
## Groups: actual [?]
## 
##       actual predict_stringent     n
##       &lt;fctr>             &lt;chr> &lt;int>
## 1 arrhythmia        arrhythmia    19
## 2 arrhythmia           healthy     6
## 3 arrhythmia         uncertain     2
## 4    healthy        arrhythmia     8
## 5    healthy           healthy    29
## 6    healthy         uncertain     2
</code></pre>
</div>
<p>To get a better overview, I am going to plot the predictions (default and stringent):</p>
<div>
<pre><code><span>p</span><span>1</span><span> </span><span>&lt;-</span><span> </span><span>finalRf_predictions</span><span> </span><span>%>%</span><span>
</span><span>ggplot</span><span>(</span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>actual</span><span>,</span><span> </span><span>fill</span><span> </span><span>=</span><span> </span><span>accurate</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_bar</span><span>(</span><span>position</span><span> </span><span>=</span><span> </span><span>&quot;dodge&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>scale_fill_brewer</span><span>(</span><span>palette</span><span> </span><span>=</span><span> </span><span>&quot;Set1&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>my_theme</span><span>()</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>fill</span><span> </span><span>=</span><span> </span><span>&quot;Were\npredictions\naccurate?&quot;</span><span>,</span><span>
</span><span>title</span><span> </span><span>=</span><span> </span><span>&quot;Default predictions&quot;</span><span>)</span><span>
</span><span>p</span><span>2</span><span> </span><span>&lt;-</span><span> </span><span>finalRf_predictions</span><span> </span><span>%>%</span><span>
</span><span>subset</span><span>(</span><span>accurate_stringent</span><span> </span><span>!=</span><span> </span><span>&quot;na&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>ggplot</span><span>(</span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>actual</span><span>,</span><span> </span><span>fill</span><span> </span><span>=</span><span> </span><span>accurate_stringent</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_bar</span><span>(</span><span>position</span><span> </span><span>=</span><span> </span><span>&quot;dodge&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>scale_fill_brewer</span><span>(</span><span>palette</span><span> </span><span>=</span><span> </span><span>&quot;Set1&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>my_theme</span><span>()</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>fill</span><span> </span><span>=</span><span> </span><span>&quot;Were\npredictions\naccurate?&quot;</span><span>,</span><span>
</span><span>title</span><span> </span><span>=</span><span> </span><span>&quot;Stringent predictions&quot;</span><span>)</span><span>
</span><span>grid.arrange</span><span>(</span><span>p</span><span>1</span><span>,</span><span> </span><span>p</span><span>2</span><span>,</span><span> </span><span>ncol</span><span> </span><span>=</span><span> </span><span>2</span><span>)</span><span>
</span></code></pre>
</div>
<p><img alt="" src="https://i1.wp.com/shiring.github.io/machine_learning/2017/02/27/h2o_files/figure-markdown_github/unnamed-chunk-53-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>Being more stringent with the prediction threshold slightly reduced the number of errors but not by much.</p>
<p>I also want to know whether there are certain classes of arrhythmia that are especially prone to being misclassified:</p>
<div>
<pre><code><span>p</span><span>1</span><span> </span><span>&lt;-</span><span> </span><span>subset</span><span>(</span><span>finalRf_predictions</span><span>,</span><span> </span><span>actual</span><span> </span><span>==</span><span> </span><span>&quot;arrhythmia&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>ggplot</span><span>(</span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>predict</span><span>,</span><span> </span><span>fill</span><span> </span><span>=</span><span> </span><span>class</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_bar</span><span>(</span><span>position</span><span> </span><span>=</span><span> </span><span>&quot;dodge&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>my_theme</span><span>()</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>title</span><span> </span><span>=</span><span> </span><span>&quot;Prediction accuracy of arrhythmia cases&quot;</span><span>,</span><span>
</span><span>subtitle</span><span> </span><span>=</span><span> </span><span>&quot;Default predictions&quot;</span><span>,</span><span>
</span><span>x</span><span> </span><span>=</span><span> </span><span>&quot;predicted to be&quot;</span><span>)</span><span>
</span><span>p</span><span>2</span><span> </span><span>&lt;-</span><span> </span><span>subset</span><span>(</span><span>finalRf_predictions</span><span>,</span><span> </span><span>actual</span><span> </span><span>==</span><span> </span><span>&quot;arrhythmia&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>ggplot</span><span>(</span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>predict_stringent</span><span>,</span><span> </span><span>fill</span><span> </span><span>=</span><span> </span><span>class</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_bar</span><span>(</span><span>position</span><span> </span><span>=</span><span> </span><span>&quot;dodge&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>my_theme</span><span>()</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>title</span><span> </span><span>=</span><span> </span><span>&quot;Prediction accuracy of arrhythmia cases&quot;</span><span>,</span><span>
</span><span>subtitle</span><span> </span><span>=</span><span> </span><span>&quot;Stringent predictions&quot;</span><span>,</span><span>
</span><span>x</span><span> </span><span>=</span><span> </span><span>&quot;predicted to be&quot;</span><span>)</span><span>
</span><span>grid.arrange</span><span>(</span><span>p</span><span>1</span><span>,</span><span> </span><span>p</span><span>2</span><span>,</span><span> </span><span>ncol</span><span> </span><span>=</span><span> </span><span>2</span><span>)</span><span>
</span></code></pre>
</div>
<p><img alt="" src="https://i1.wp.com/shiring.github.io/machine_learning/2017/02/27/h2o_files/figure-markdown_github/unnamed-chunk-54-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>There are no obvious biases towards some classes but with the small number of samples for most classes, this is difficult to assess.</p>
<p></p>
<h3>Final conclusions: How useful is the model?</h3>
<p>Most samples were classified correctly, but the total error was not particularly good. Moreover, when evaluating the usefulness of a specific model, we need to keep in mind what we want to achieve with it and which questions we want to answer. If we wanted to deploy this model in a clinical setting, it should assist with diagnosing patients. So, we need to think about what the consequences of wrong classifications would be. Would it be better to optimize for high sensitivity, in this example as many arrhythmia cases as possible get detected – with the drawback that we probably also diagnose a few healthy people? Or do we want to maximize precision, meaning that we could be confident that a patient who got predicted to have arrhythmia does indeed have it, while accepting that a few arrhythmia cases would remain undiagnosed? When we consider stringent predictions, this model correctly classified 19 out of 27 arrhythmia cases, but 6 were misdiagnosed. This would mean that some patients who were actually sick, wouldn’t have gotten the correct treatment (if decided solely based on this model). For real-life application, this is obviously not sufficient!</p>
<p>Next week, I’ll be trying to improve the model by doing a grid search for hyper-parameter tuning.</p>
<p>So, stay tuned… (sorry, couldn’t resist ;-))</p>
<hr>
<p>Other machine learning topics I have covered include</p>
<ul>
<li>how to run machine learning with Apache Spark in R: <a target="_blank" rel="nofollow" href="https://shiring.github.io/machine_learning/2017/02/19/food_spark">Predicting food preferences with sparklyr (machine learning)<br>
</a></li>
<li>a basic machine learning workflow: <a target="_blank" rel="nofollow" href="https://shiring.github.io/machine_learning/2016/11/27/flu_outcome_ML_post">Can we predict flu deaths with Machine Learning and R?</a></li>
<li>extreme gradient boosting: <a target="_blank" rel="nofollow" href="https://shiring.github.io/machine_learning/2016/12/02/flu_outcome_ML_2_post">Extreme Gradient Boosting and Preprocessing in Machine Learning – Addendum to predicting flu outcome with R</a></li>
<li>feature selection: <a target="_blank" rel="nofollow" href="https://shiring.github.io/machine_learning/2017/01/15/rfe_ga_post">Feature Selection in Machine Learning (Breast Cancer Datasets)</a></li>
</ul>
<hr>
<p></p>
<div>
<pre><code>## R version 3.3.2 (2016-10-31)
## Platform: x86_64-apple-darwin13.4.0 (64-bit)
## Running under: macOS Sierra 10.12.3
## 
## locale:
## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
## 
## attached base packages:
##  [1] stats4    parallel  grid      stats     graphics  grDevices utils    
##  [8] datasets  methods   base     
## 
## other attached packages:
##  [1] reshape2_1.4.2       tidyr_0.6.1          matrixStats_0.51.0  
##  [4] pcaGoPromoter_1.18.0 Biostrings_2.42.1    XVector_0.14.0      
##  [7] IRanges_2.8.1        S4Vectors_0.12.1     BiocGenerics_0.20.0 
## [10] ellipse_0.3-8        gridExtra_2.2.1      ggrepel_0.6.5       
## [13] ggplot2_2.2.1        sparklyr_0.5.2       dplyr_0.5.0         
## [16] h2o_3.10.3.6         rsparkling_0.1.0    
## 
## loaded via a namespace (and not attached):
##  [1] Rcpp_0.12.9          RColorBrewer_1.1-2   plyr_1.8.4          
##  [4] zlibbioc_1.20.0      bitops_1.0-6         base64enc_0.1-3     
##  [7] tools_3.3.2          digest_0.6.12        memoise_1.0.0       
## [10] RSQLite_1.1-2        jsonlite_1.2         evaluate_0.10       
## [13] tibble_1.2           gtable_0.2.0         DBI_0.5-1           
## [16] yaml_2.1.14          withr_1.0.2          httr_1.2.1          
## [19] stringr_1.2.0        knitr_1.15.1         rappdirs_0.3.1      
## [22] rprojroot_1.2        Biobase_2.34.0       R6_2.2.0            
## [25] AnnotationDbi_1.36.0 rmarkdown_1.3        magrittr_1.5        
## [28] backports_1.0.5      scales_0.4.1         htmltools_0.3.5     
## [31] assertthat_0.1       colorspace_1.3-2     labeling_0.3        
## [34] config_0.2           stringi_1.1.2        RCurl_1.95-4.8      
## [37] lazyeval_0.2.0       munsell_0.4.3
</code></pre>
</div>
<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://shiring.github.io/machine_learning/2017/02/27/h2o"> Shirin's playgRound</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/3URi-K6ysMo"></a></div>