Building and deploying a machine learning model with automated ML on Azure. | by Robbert Brouwers | Towards Data Science

This article goes through the basics of using Azure’s Automated Machine learning functionality. We will train and deploy a machine learning model that is operational. After this article you will be able to build an end to end solution for deploying a machine learning model, without writing a single line of code.
The goal is to help you understand what goes into the basics of building and deploying machine learning models. It won’t cover concepts such as modelling techniques/ best practices, security/authorisation, but functions as a starting point for those who: (1) want to build ML models without coding or (2) want to understand what is possible with automated machine learning and how this can be of value to you and your organisation.
We will use Microsoft Azure’s Automated Machine Learning platform for this exercise. We set up the environment/resources, train multiple models on the dataset and deploy this model on Azure so we can actually use it.
This article is set up in three sections:1. The “Boring Stuff” (Setting up the resources in Azure)2. The “Cool Stuff” (training the model)3. The “I-Can-Show-My-Friends-Now” (deploying the model)
So without further ado, let’s build some cool things (aka on to the Boring Stuff)!
Using services on Azure cost money. There’s ways to get free credits or trial periods (for example: through this link.)
The whole experiment costed me €3.02 (without using any free credits):
At the end of this article we’ll go through how to clean up so we won’t incur any unexpected costs.
Let’s get to it!
(sign up here if you don’t have an azure account yet and make sure you have a payment method set up, should be part of the registration process).
Once signed up, you should see you default Azure Home Page:
A resource group is ‘a container that holds related resources for an Azure Solution’. It will help us to easily distinguish the resources that belong to our ML model and deployment (and will help us easily remove it afterwards).
From the Azure Home page, click “Resource Groups” in the left menu and create a new resource group:
The regions are not important for this exercise: pick the one based on where you are in this world.
Azure Machine Learning services are a collection of resources to help you create, test and deploy machine learning models. Automated ML is a part of this collection and that’s what we are using here. Make sure to allocate the new resource to the above created “autoML” resource group.
The regions are not that important for this exercise: pick the one based on where you are in this world.
In this article, we will build a regression model that predicts the correct price for an AirBnB place in New York City. The data comes from Kaggle and is available via this link.
We can save this file locally for now: we will upload it to Azure in the next section.
We have created an account for Azure, set up a resource group to organize our resources related to this exercise and created an Azure Machine Learning Service Workspace, which we can see from our home page:
Congrats! We’ve done the boring stuff! Let’s quickly head over to do some actual data related work.
On our Azure home page, we can click on the “autoML” resource to go the resource page. We will use the new interface by clicking on the “Launch Preview Now” banner, or go there directly through this link. All the training steps will be executed from the new Azure Machine Learning Interface.
Select the recently created subscription and Azure Machine Learning Resource:
Upload DatasetUpload the unzipped dataset “Output.csv” by navigating to “Datasets” (left menu), then click “Create Dataset”.
Select the file from local machine. All the other settings can remain as they are. After clicking done, the dataset will upload:
Our dataset will be visible from the “Datasets” page now:
From the menu, head over to “Automated ML” and click on the blue “Create Experiment” button:
In the next screen we have to provide a name and training compute. You might wonder what the “training compute” means. The models we want to train need a computer to run on. Azure provides virtual computers, which are located in massive datacenters all around the world. This allows us to basically spin up any size of computer when needed and spin it down again when done, only using what we need.
For this exercise, we’re going to spin up a small compute resource (a virtual machine). Click “create a new compute” and give it a name such as autoML. We’ll use the default STANDARD_DS12_V2 — — 4 vCPUs, 28 GB memory, 56 GB storage (leave additional settings as is):
When we picked an experiment name (I went with -surprise!- “autoML”) and, the new compute is created, make sure to select the training compute we just created and hit next.
Next we select the dataset we previously uploaded and we will be asked for a couple more pieces of input. Now for our use case, the goal is to predict the price (Target Column), based on other information in the dataset.
We can check the profile tab to do a bit of data exploration, but for the sake of brevity, we are going straight to picking columns to include, the prediction task and Target Column:
First we exclude columns we don’t want/need in our model. You might explore/include other columns in the future, but to follow along with this tutorial: pick the same columns as I have done. The columns we include are: neighbourhood_group, neighbourhood, room_type, price (target column), minimum_nights, number_of_reviews, last_review, reviews_per_month.
We are predicting a quantity (price per night), so our prediction task is regression and the Target Column is price.
We could play around with the advanced settings, and you probably will for future experiments, but for now we’re leaving most of it as is.This means we pick ‘Spearman Correlation’ as the primary metric to measure and compare the models on. Let’s set the training job time to 30 minutes.
Scroll down, hit start and congratulate yourself! You are now a data scientist!*
Azure will now spin up the right resources. After that the actual ‘automated’ part of ML begins: Azure will try and fit different models and settings to find the best fit for our data and type of exercise (regression). We will start seeing different models and their accuracy scores soon. Also, we don’t have to stay on any specific page during the run; so feel free to browse around the Azure ML interface.
Bear in mind, it will take some time for Azure to spin up the resources and for the results of the first models to come in so the whole process takes longer than the 30 minutes we have picked as our training time.
*but don’t tell your actual data scientist friends and colleagues because they probably won’t agree.
During and after the experiment run, we can see the results of each run by going to the leaderboard:
By clicking on any of the completed iterations we can see a more extensive set of result metrics and graphs:
Once the experiment has finished we can look around a bit and see what type of models have been ran, how they have performed etc. Afterwards we are heading to the next section: deployment
Cool! We have a leaderboard and a winning model. Now what? We can’t really show off anything yet: we want to give our model input columns (such as the neighbourhood, number of reviews and average reviews per month) and then receive an estimate for the price per night. On to the “I-Can-Show-My-Friends-Now” part of this tutorial!
To receive an estimate for the price per night, we need to “deploy” the model. This will become a “container” that is able to run our model code (the winning model) on a specified environment (python) and handle incoming requests. Azure Machine Learning Services has a pretty awesome functionality included in automated machine learning services that does all the work for us when deploying: “Deploy Best Model”. It’s on the leaderboard page we previously looked at to see how different models are performing.
Use any deployment name and description you’d like and leave the Scoring Script and Environment Script to “Auto Generate”. Hit Deploy, this will take around 20 mins to complete.
When our model is deployed, the model will appear on the “Model” page. A model includes an endpoint. An API endpoint is commonly defined as ‘the end of a communication channel’. That definition by itself doesn’t really help us (at all), but think about it this way: we have a model and we want to communicate with this model by sending input (the columns we included in our model) and getting output (an estimation of the price per night) in return. We can’t just throw anything at our model/endpoint and expect a correct response, so next up is to find our endpoint and send a HTTP request to our model!
When we deployed the model, Azure ML services created a python script that has been deployed in a ‘container’. This container runs somewhere it the Azure cloud and is now ready to receive HTTP requests from us.
For this to work, we’ll need to do three things:
The endpoint is available in the Azure ML interface by going to “Endpoints” from the left menu
We click on our newly deployed model and will find see a list of attributes, of which the relevant one is the “Scoring URI”.
Remember talking about API endpoints? This Scoring URI is our endpoint for the model we have created, meaning we can send (“do a request”) something to this endpoint (input) and will receive a response (output). And if we format the request correctly, we will get back what we want: a price per night estimate given our input columns.
This part might get a bit confusing for those who have no experience working with HTTP requests, but just keep following the steps and you’ll get it to work!
Functionally, what we want to do is send a http request where we input values for the columns we have included in the model (except the target column, which is the one column we want back of course). That means we want to send information about:
“neighbourhood_group”, “neighbourhood”, “room_type”, “minimum_nights”,“number_of_reviews”, “last_review”, “reviews_per_month”.
This information is referred to as the “body” of our http request and Azure describes in their documentation how we should format this information to be interpretable by the model:
The REST API expects the body of the request to be a JSON document with the following structure:
{    "data":        [            <model-specific-data-structure>        ]}
where (an example of) our “model specific data structure” looks like this:
{    "data": [        {            "neighbourhood_group": "Queens",            "neighbourhood": "Ridgewood",            "room_type": "Entire home/apt",            "minimum_nights": 3,            "number_of_reviews": 9,            "last_review": "2019-06-26T00:00:00.000Z",            "reviews_per_month": 2.1        }    ]}
A HTTP request also includes one or more “Headers”, and according to the documentation, we need to add a “Content-Type” header:
“Content-Type” : “application/json”
To summarise, based on: (1) the endpoint/Scoring URI, (2) the body (input) and (3) the headers, we can create a request that will give us back (if everything works correct) an estimated price per night! Let’s put it to the test!
For the curious ones (which I assume is all of you who made it this far): here’s the complete request:
POST /score HTTP/1.1Host: <scoring-uri>Content-Type: application/jsonUser-Agent: PostmanRuntime/7.17.1Accept: */*Cache-Control: no-cachePostman-Token: <token-value>Host: <scoring-uri>Accept-Encoding: gzip, deflateContent-Length: 335Connection: keep-alivecache-control: no-cache{    "data": [        {            "neighbourhood_group": "Queens",            "neighbourhood": "Ridgewood",            "room_type": "Entire home/apt",            "minimum_nights": 3,            "number_of_reviews": 9,            "last_review": "2019-06-26T00:00:00.000Z",            "reviews_per_month": 2.1        }    ]}
There is some information in there that we haven’t covered; don’t worry about it, as it’s not essential for understanding our end to end workflow. On to the last part!
At this moment, you have a deployed model that is ‘callable’ by means of an API endpoint (the scoring URI). Anyone (quite literally anyone since we haven’t enforced any authorization on the API) can use your beautiful model in their apps, notebooks or other (business) applications by utilising the API endpoint. When I send the request from two paragraphs up, I get the following result:
"{\"result\": [125.5851758821278]}"
Awesome! So this tells us that given the model input (Queens, Ridgewood, Entire home/apt, 3 nights, 9 reviews, last review on the 26th of June 2019 and an average of 2.1 reviews per month) estimates that the price per night would be $125.58
Easy peasy! Obviously you want to see this to believe it, so here we go:
Download Postman, a free app for API development. We will use his to send our HTTP request to our model. Once installed. We’ll repeat the three steps of: setting the URL (scoring URI), adding the Content-Type Header and adding the body:
Open up Postman and select “Request” from the “Create New” tab:
Provide a name and create a collection/folder and hit Save:
You’ll see a new screen where we can create API requests.
Set the “http method” to POST, and copy the scoring URI where the “enter request URL” placeholder is placed:
When we go to the “Body” tab in postman, we take two actions:1. Set body type to “raw” and the type to JSON2. Copy the request column and values from previously shown request into the body field:
{    "data": [        {            "neighbourhood_group": "Queens",            "neighbourhood": "Ridgewood",            "room_type": "Entire home/apt",            "minimum_nights": 3,            "number_of_reviews": 9,            "last_review": "2019-06-26T00:00:00.000Z",            "reviews_per_month": 2.1        }    ]}
The header we talked about before (“Content-Type”) is automatically added by postman because we set the type of raw body to “JSON”. You can see this on the header tab.
This means we have covered all parts of the request! Time to hit that big blue send button and do our first request!
You’ve done it! Give yourself a high-five, put on your favorite jam (I recommend DMX — Party Up (Up In Here) for the occasion) and shoot a couple more requests to your very own deployed automated machine learning model!
When you are ready, make sure to delete the resource group so you won’t incur any unexpected charges on Azure:
As you may have noticed by going through this tutorial, there’s quite a bit of work that is not necessarily data science related when it comes to making data science operational. I strongly believe that it is valuable for any data worker to experiment. We have an amazing set of cloud service providers, (open source) frameworks to quickly and easily start testing new ideas. The most important thing is to get started!
There seems to be an ongoing debate about automated machine learning, where people are roughly divided between “That’s not data science!” and “We don’t need data scientists anymore at all!”. The truth is probably somewhere in the middle, but requires a certain amount of nuance.
It will always depend on your specific requirements and available capabilities, but given the amount of options you have when picking autoML vendors/frameworks and the amount of attention it’s getting, we can probably safely assume that it’s here to stay.
I think knowing about and possibly applying automated ML will help organisations leverage new technology, which can be valuable especially when:
You don’t have a data science team or want to leverage the scarce data science resources by automating the ‘simpler’ data science tasks.You want to iterate over the complete end to end process for rapid development of analytics use cases. We should allocate time to all parts of the process and not just the modelling part to ensure we end up with a solution that is not only accurate but also works (operational).You want to establish a ‘baseline model’. The automated ML model doesn’t have to be the final model.
You don’t have a data science team or want to leverage the scarce data science resources by automating the ‘simpler’ data science tasks.
You want to iterate over the complete end to end process for rapid development of analytics use cases. We should allocate time to all parts of the process and not just the modelling part to ensure we end up with a solution that is not only accurate but also works (operational).
You want to establish a ‘baseline model’. The automated ML model doesn’t have to be the final model.
I hope this has been a useful tutorial for you to get started with automated machine learning! Leave your thoughts/suggestions in the replies!
(Feel free to reach out on LinkedIn as well).
Different automated ML solutions — https://medium.com/@santiagof/auto-is-the-new-black-google-automl-microsoft-automated-ml-autokeras-and-auto-sklearn-80d1d3c3005c
Data Science methodology — https://medium.com/towards-artificial-intelligence/the-data-science-methodology-50d60175a06a