The purpose of this notebook is to demonstrate how to use Jupyter notebooks on the Azure Machine Learning (ML) platform to develop a model in R and publish a web service based on the model.
For data scientists new to Azure ML and accustomed to doing all analytical work using R on local computers, Azure Machine Learning makes it possible to write R notebooks on the cloud. So anyone with internet access can work with R from a web browser.
If you use R and understand the basics of Azure ML, R notebooks make it possible to develop your models in R and then operationalize them easily.
For data scientists who're comfortable with both R and Azure ML Experiments, R notebooks can be used together with Azure ML Experiments in different ways:
The target audience of this notebook are R users who have a basic understanding of Azure ML. If you are new to Azure ML, Section 2 of the Data Scientists' Guide provides enough information for you to follow this tutorial.
In this example, we'll use the housing data from the R package MASS
. The Boston data contains 506 rows and 14 columns. Available information includes median home price, average number of rooms per dwelling, crime rate by town, etc. Find more information about this dataset by running ?Boston
in an R terminal.
In [ ]:
?Boston
In [ ]:
# load the library to use the Boston dataset
library(MASS)
# fit a model using all variables except medv as predictors
lm1 <- lm(medv ~ ., data = Boston)
# check model performance
summary(lm1)
In [ ]:
pred <- predict(lm1)
error <- pred - Boston$medv
mae <- mean(abs(error))
rmse <- sqrt(mean((error)^2))
rae <- mean(abs(error)) / mean(abs(Boston$medv - mean(Boston$medv)))
rse <- mean((error)^2) / mean((Boston$medv - mean(Boston$medv))^2)
cat("Mean Absolute Error:", round(mae, 6), "\n")
cat("Mean Absolute Error:", round(mae, 6), "\n")
cat("Root Mean Squared Error:", round(rmse, 6), "\n")
cat("Relative Absolute Error:", round(rae, 6), "\n")
cat("Relative Squared Error:", round(rse, 6), "\n")
With the developed model, you can deploy a web service so that others can use it to make predictions. The AzureML
R package will be used for this purpose. You'll need to provide the workspace ID and authorization token for an Azure ML workspace. The two screenshots below show where you can find them in your workspace. Either primary or secondary authorization token can be used.
<img src="https://cloud.githubusercontent.com/assets/9322661/11348789/952a5db4-91f6-11e5-9710-8805451194dd.PNG" width = "800", alt = "Finding your workspace ID">
<img src="https://cloud.githubusercontent.com/assets/9322661/11348692/09746e40-91f6-11e5-9dfa-6ac897e3c426.PNG" width = "800", alt = "Finding your authorization token">
In [ ]:
# load the library
library(AzureML)
# retrieve workspace information
ws <- workspace()
# define predict function
mypredict <- function(newdata){
predict(lm1, newdata)
}
# a sample with predictor information
newdata <- Boston[1:5, ]
# test the prediction function
data.frame(
actual = newdata$medv,
prediction = mypredict(newdata))
In [ ]:
# publish the service
ep <- publishWebService(ws = ws,
fun = mypredict,
name = "HousePricePrediction",
inputSchema = newdata)
str(ep)
After setting up a web service, you can use R scripts to consume it in three ways: one is in-session and two are out-of-session.
If you are consuming the web service in the same session that the web service was set up, you can refer to the endpoint directly.
In [ ]:
pred <- consume(ep, newdata)$ans
data.frame(actual = newdata$medv, prediction = pred)
# Note that this operation may take several seconds to complete.
# You may see several retry attempts, before the service returns with a value
In [ ]:
# Create a temporary file to store the endpoint data
tf <- tempfile(fileext = ".rds")
saveRDS(ep, file = tf)
# Read the endpoint data from file
endpoint <- readRDS(tf)
# consume
pred <- consume(endpoint, newdata)$ans
data.frame(
actual = newdata$medv,
prediction = mypredict(newdata))
In [ ]:
str(ep)
In [ ]:
ep$WebServiceId
In [ ]:
# define test function
mypredictnew <- function(newdata){
predict(lm1, newdata) + 100
}
# update service with the new function
ep_update <- updateWebService(
ws = ws,
fun = mypredictnew,
name = "not necessary", # this does not matter since serviceId is provided
inputSchema = newdata,
serviceId = ep$WebServiceId
)
In this example, you learned how to fit a model, deploy the model on Azure and consume the service.
The AzureML
R package also allows you to read data from Azure ML workspace or experiments, making it possible for users to have a seamless experience between Azure ML experiments and notebooks.
You can find more details in the AzureML
package help.
Created by a Microsoft Employee.
Copyright (C) Microsoft. All Rights Reserved.