<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/parallelizing-data-analytics-on-azure-with-the-r-interface-tool/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://blog.revolutionanalytics.com/2016/12/azure-r-interface-tool.html"> Revolutions</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<div>
<p><em>by Le Zhang (Data Scientist, Microsoft) and </em><em>Graham Williams (Director of Data Science, Microsoft)</em></p>
<p>In data science, to develop a model with optimal performance, exploratory experiments on different sets of hyper-parameters are often performed. Preliminary analyses on smaller data can be performed on a single machine, while the experimental one on large-scale data by sweeping multi-sets of parameters can be run on a cluster to boost the computational efforts. Scalable computation resources which can be easily managed are desired for such an application scenario. This blog post shares a walk-through using the Azure Interface tool that</p>
<ul>
<li>operates and manages Azure cloud instances directly from R using the <a target="_blank" rel="nofollow" href="https://github.com/Microsoft/AzureSMR">AzureSMR package</a>, and</li>
<li>executes scalable analytical jobs on deployed instances using customized <a target="_blank" rel="nofollow" href="https://www.microsoft.com/en-us/cloud-platform/r-server">Microsoft R Server</a> computation contexts, which are easily specified in R using an &quot;interface object&quot;.</li>
</ul>
<p>The overall architecture of the Interface Tool is described in the graphic below. The Master script manages Azure instance deployment and operation, as well as settings of analytics execution specifications. The Worker script contains the actual analytics which is submitted onto the Azure instance and run according to the configured context.</p>
<p><a target="_blank" rel="nofollow" href="http://revolution-computing.typepad.com/.a/6a010534b1db25970b01b7c8bf04fa970b-pi"><img title="Predictive-architecture" alt="Predictive-architecture" src="http://revolution-computing.typepad.com/.a/6a010534b1db25970b01b7c8bf04fa970b-800wi" border="0"></a> </p>
<p>A <a target="_blank" rel="nofollow" href="https://docs.microsoft.com/en-us/azure/machine-learning/cortana-analytics-playbook-predictive-maintenance">predictive maintenance</a> example is presented to illustrate the efficacy of the interface tool. Predictive maintenance is widely employed within the airline and manufacturing industries to reduce operational costs. One of the problems in a predictive maintenance scenario is to diagnose whether equipment is working under a healthy status or not by analyzing historical sensor measurements. Based on the data set of sensor measurements, a well-trained model is able to classify a machine as “low risk” or “high risk” of failure. To obtain an optimal model for accurate prediction, hyper-parameters should be swept to perform a search on the domain of definition. There are two critical hyper-parameters in the health status recognition that need experimental analysis for optimal prediction: </p>
<ol>
<li><strong>Length of historical data</strong>. The length of historical data is the period over which the sensor value measures can be used to characterize the health status of the machine. For the high risk group samples, the time series are taken near the end of the life time of a machine (as shown in the illustration below), while for the low risk group samples, the time series are taken near the beginning of the lifetime. The classification model can be subject to over fitting if the length of historical data is too small. However, enlarging the window too much reduces the accuracy of the model as a large window may overlap the data series from two different risk groups.</li>
<li><strong>Lagged feature window</strong>. To consider the time-dependency of the time series data, correlation of sensor value data in the lagged time points with the current time point is also taken into consideration. The strategy is to obtain the statistical characteristics (e.g., rolling mean) of the historical data and aggregate them into the data at the current time point. There are also trade-offs in the lagged feature window. If the window is too small, the progressing degradation of the features over time may not be captured. However, if the window is too large, the dimensionality of the feature set for training grows which may easily lead to poor performance.</li>
</ol>
<p>The graph below depicts the health status metric of a machine during its lifetime, partitioned into the low risk group and high risk group.</p>
<p><strong> <a target="_blank" rel="nofollow" href="http://revolution-computing.typepad.com/.a/6a010534b1db25970b01bb0961ef65970d-pi"><img title="Health metric" alt="Health metric" src="http://revolution-computing.typepad.com/.a/6a010534b1db25970b01bb0961ef65970d-800wi" border="0"></a><br></strong></p>
<p>In our example, we create a Master script to manage Azure resources (virtual machines) and specify the computing environment. We launch five <a target="_blank" rel="nofollow" href="https://docs.microsoft.com/en-us/azure/machine-learning/machine-learning-data-science-provision-vm">Data Science Virtual Machines (DSVMs)</a> on the Azure platform to form a cluster, using the AzureSMR functions below to eliminate the repetitiveness of manual DSVM deployment.</p>
<div>
<pre><span># Authenticate Azure account and start DSVMs in resource group under the subscription.</span>
 
library(AzureSMR)
library(magrittr)
library(dplyr)
 
TID <span>&lt;-</span> <span>&quot;88bf...011d&quot;</span>    <span># Tenant ID from app creation in Active Directory.</span>
CID <span>&lt;-</span> <span>&quot;10e3....d3d1&quot;</span>   <span># Client ID from app creation in Active Directory.</span>
KEY <span>&lt;-</span> <span>&quot;u/cc....53hg&quot;</span>   <span># User key from app creation in Active Directory.</span>
 
sc <span>&lt;-</span> createAzureContext(tenantID<span>=</span>TID, clientID<span>=</span>CID, authKey<span>=</span>KEY)
 
rg_list <span>&lt;-</span> azureListRG(sc)
 
location <span>&lt;-</span> 
  as.character(rg_list <span>%>%</span> filter(name <span>==</span> RG) <span>%>%</span> select(location)) <span>%T>%</span>
  print() 
 
vm_list <span>&lt;-</span> 
  azureListVM(azureActiveContext<span>=</span>sc, resourceGroup<span>=</span>RG) <span>%T>%</span>
  print()
 
vm_names <span>&lt;-</span> as.character(vm_list<span>$</span>name)
 
<span># check the status of VMs.</span>
 
<span>for</span> (vm <span>in</span> vm_names) 
{
  vm_status <span>&lt;-</span> azureVMStatus(azureActiveContext<span>=</span>sc, resourceGroup<span>=</span>RG, vmName<span>=</span>vm)
  print(paste0(vm, vm_status, sep<span>=</span><span>&quot;, &quot;</span>))
}
 
<span># switch on the VMs in the resource group.</span>
 
<span>for</span> (vm <span>in</span> vm_names) 
{
  azureStartVM(azureActiveContext<span>=</span>sc, resourceGroup<span>=</span>RG, vmName<span>=</span>vm)
}
vm_dns_list <span>&lt;-</span> paste0(vm_names, <span>&quot;.&quot;</span>, location, <span>&quot;.cloudapp.azure.com&quot;</span>)
</pre>
</div>
<p>Next, an Interface object is created in the Master script to specify and configure the computing context. Here the “clusterParallel” computing context is used to parallelize the analytical work on the a cluster formed by the 5 running DSVM nodes.</p>
<div>
<pre>index <span>&lt;-</span> sample(x<span>=</span>seq_along(vm_dns_list), <span>1</span>)

MACHINES     <span>&lt;-</span> vm_names
MACHINES_URL <span>&lt;-</span> vm_dns_list
MASTER_URL   <span>&lt;-</span> vm_dns_list[index]
SLAVES_URL   <span>&lt;-</span> vm_dns_list[<span>-</span>index]
 
source(<span>&quot;rInterfaceObject.R&quot;</span>)
 
<span># Create a new interface.</span>
 
interface <span>&lt;-</span> new(<span>&quot;rInterface&quot;</span>)
 
<span># Set the interface.</span>
 
interface <span>&lt;-</span> riSet(object<span>=</span>interface,
                   remote<span>=</span>MASTER_URL,
                   user<span>=</span><span>&quot;&lt;user-name>&quot;</span>
)
 
<span># Configure the interface.</span>
 
interface <span>&lt;-</span> riConfig(object<span>=</span>interface, 
                      machine_list<span>=</span>MACHINES, 
                      data<span>=</span><span>&quot;&lt;reference-to-data-source&quot;</span>,
                      dns_list<span>=</span>MACHINES_URL, 
                      machine_user<span>=</span><span>&quot;&lt;user-name>&quot;</span>,
                      master<span>=</span>MASTER_URL, 
                      slaves<span>=</span>SLAVES_URL, 
                      context<span>=</span><span>&quot;clusterParallel&quot;</span>)
</pre>
</div>
<p>A Worker script which contains the analytic logic is now created. The computation performs a health status prediction given sensor measures from a machine. Note that once the interface object is configured, codes that specify the compute context in the Worker script will be automatically added. (The contents of the Worker script are not shown here but are included in the <a target="_blank" rel="nofollow" href="https://github.com/yueguoguo/Azure-R-Interface/tree/master/scripts/demo-1">Github repository</a>.)</p>
<div>
<pre><span># Create a new worker script.</span>
 
script_path  <span>&lt;-</span> <span>&quot;&lt;path-to-worker-script>&quot;</span>
script_title <span>&lt;-</span> <span>&quot;&lt;name-of-worker-script>&quot;</span>
 
<span>if</span> (<span>!</span>file.exists(file.path(script_path, script_title))) {
  riNewScript(path<span>=</span>script_path, title<span>=</span>script_title)
}
 
<span># Set the script to the interface object, and update the new worker script with config information.</span>
 
interface<span>@</span>script <span>&lt;-</span> file.path(script_path, script_title)
riScript(interface)
file.edit(interface<span>@</span>script)
</pre>
</div>
<p>The engines in the data set (<a target="_blank" rel="nofollow" href="https://ti.arc.nasa.gov/tech/dash/pcoe/prognostic-data-repository/">NASA Turbo Fan Engine Sensor Data Set</a>) are labelled as “high risk” and “low risk”. The length of historical data is selected from two different values to investigate its impact. Similarly, the feature lagged window is also selected from two values. The problem can be regarded as a classification – the boosted tree algorithm is used to train a model which is then tested on a testing data set. The Worker script is run on the header node of the DSVM cluster, with the specification of computation context in the Master script. The computation context is set to be “clusterParallel” which leverages the power of 4 DSVM nodes for computation.</p>
<div>
<pre>result <span>&lt;-</span> riExecute(object<span>=</span>interface,
                    roptions<span>=</span><span>&quot;--verbose&quot;</span>,
                    verbose<span>=</span><span>TRUE</span>)
</pre>
</div>
<p>The model performance is evaluated on four metrics: accuracy, precision, recall, and F-score. The parameter “length of historical data” (LHD) is selected from 40 and 50 cycles while the lagged feature window (LFW) is 3 or 5. Two sets of experiments are performed to sweep only LHD and both LHD and LFW. The results follow.</p>
<table border="1" cellspacing="0">
<tbody>
<tr>
<td>
<p><strong>(LHD, LFW)</strong></p>
</td>
<td>
<p><strong>Accuracy/Precision/Recall/F-1</strong></p>
</td>
<td>
<p><strong>Elapsed time</strong></p>
</td>
</tr>
<tr>
<td>
<p>(40, 3)</p>
</td>
<td>
<p>0.992/0.984/0.999/0.992</p>
</td>
<td rowspan="2">
<p>2.6 min</p>
</td>
</tr>
<tr>
<td>
<p>(50, 3)</p>
</td>
<td>
<p>0.969/0.966/0.972/0.969</p>
</td>
</tr>
<tr>
<td>
<p>(40, 3)</p>
</td>
<td>
<p>0.990/0.981/0.999/0.990</p>
</td>
<td rowspan="4">
<p>2.8 min</p>
</td>
</tr>
<tr>
<td>
<p>(40, 5)</p>
</td>
<td>
<p>0.990/0.984/0.997/0.990</p>
</td>
</tr>
<tr>
<td>
<p>(50, 3)</p>
</td>
<td>
<p>0.982/0.976/0.988/0.982</p>
</td>
</tr>
<tr>
<td>
<p>(50, 5)</p>
</td>
<td>
<p>0.991/0.984/0.998/0.991</p>
</td>
</tr>
</tbody>
</table>
<p>After execution, the optimal combination of parameters can be selected based on the model performance. Note the execution of experimental set 2 which sweeps on both of LHD and LFW only induces a small increase of elapsed time, owing to the high performance computational power of the DSVM cluster. After the analytics are completed the DSVMs can be powered down to reduce their running cost whilst they are not required.</p>
<div>
<pre><span># Switch off the VMs in the resource group.</span>
<span>for</span> (vm <span>in</span> vm_names) 
{
  azureStopVM(azureActiveContext<span>=</span>sc, resourceGroup<span>=</span>RG, vmName<span>=</span>vm))
}
</pre>
</div>
<p>Using the interface tool as presented here can significantly reduce time spent manually configuring DSVMs in a traditional GUI portal and in specifying the compute environment for executing the tasks. As the tool makes it possible to harness everything within an R session it can be easily aggregated into a development pipeline for scalable analytics.</p>
<p>The walk-through presented in this post can be found in our GitHub repository at <a target="_blank" rel="nofollow" href="https://github.com/yueguoguo/Azure-R-Interface">https://github.com/yueguoguo/Azure-R-Interface.</a> This includes the DSVM deployment script, ARM template, Interface object definition, and Master and Worker scripts for the health status diagnosis use case. For any queries, please reach out the authors Le Zhang <a target="_blank" rel="nofollow" href="mailto:zhle@microsoft.com">zhle@microsoft.com</a> and Graham Williams <a target="_blank" rel="nofollow" href="mailto:graham.williams@microsoft.com">graham.williams@microsoft.com</a>.</p>
</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="http://blog.revolutionanalytics.com/2016/12/azure-r-interface-tool.html"> Revolutions</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/RdJdpi4n7Vs"></a></div>