Building Models with Azure Machine Learning R Script

Microsoft Azure cloud services has significantly expanded their Machine Learning capabilities, which includes building models with R Script. We’ll show you how this is done and what scripts to use to get started.

Microsoft Azure Machine Learning now supports R Script, which is a simple text file containing nearly the same commands one would enter on an R command line. Azure Machine Learning R Script capabilities allow users to import and run existing R codes within any Azure experiment.

Using the “Create R Model” module, developers can create an untrained model from an R script, then use the “Train Model” module to effectively teach the model on a dataset. Afterwards, the model can be passed into the “Score Model” to make further predictions.

Smartbridge is a Microsoft Azure Partner

Azure Machine Learning R Script

Building Azure Machine Learning R Script Models

This guide uses samples from the Azure AI Gallery to provide an accurate reference. To start, we’ll need to add the “Create R Model”, “Train Model”, and “Score Model” modules in the Azure Machine Learning platform.

Here we have a sample R Script you could use to train your R model. It loads an R package, creates the model, and configures label column accordingly:

library(e1071)
features <- get.feature.columns(dataset)
labels <- as.factor(get.label.column(dataset))
train.data <- data.frame(features, labels)
feature.names <- get.feature.column.names(dataset)
names(train.data) <- c(feature.names, “Class”)
model <- naiveBayes(Class ~ ., train.data)

To explain the example script above, the library (e1071) loads the R package e1071 with the “Naïve Bayes” classifier algorithm. The subsequent lines get the feature and label columns from the dataset, and combine them into a new R data frame called “train.data”.

Next, we have a sample scoring script as show below:

library(e1071)
probabilities <- predict(model, dataset, type=”raw”)[,2]
classes <- as.factor(as.numeric(probabilities >= 0.5))
scores <- data.frame(classes, probabilities)

Let’s break down this particular script:

  • Library(e1071)” loads the package accordingly.

  • Probabilities “<- predict(model, dataset, type=”raw”)[,2]” calculates predicted probabilities for the scoring dataset using the trained model from the training script “model”.

  • Classes “<- as.factor(as.numeric(probabilities >= 0.5))” applies a threshold of 0.5 when assigning predicted class labels.

  • Scores “<- data.frame(classes, probabilities)” combines class labels and probabilities into output data frame scores.

Make the Most of Azure Machine Learning Capabilities!

After running the experiment, you can publish it as a web service whenever you feel the need. Although this is a fairly simple sample, this is a great way to get started creating your own custom models, and further explore the depths of Azure Machine Learning R Script capabilities.

Looking for more on Azure and Systems Modernization?

Explore more insights and expertise at smartbridge.com/modernization

There’s more to explore at Smartbridge.com!

Sign up to be notified when we publish articles, news, videos and more!