Deploy ML models with Azure Machine Learning

Deploy ML model with Azure Machine Learning

In this tutorial, you will create an inference pipeline and deploy a regression model as a service in Azure Machine Learning Designer. Firstly, you should follow the instructions provided in the article “Predict CO2 emissions from cars with Azure Machine Learning” to create a linear regression model that predicts carbon dioxide emissions from cars.

In this tutorial, you will learn how to:

  • Create a real-time inference pipeline.
  • Deploy a regression model as a service in Azure Machine Learning.

Create an inference pipeline

Firstly, you will create a real-time inference pipeline that uses the existing trained model to predict label values. This pipeline performs the same data transformations for new data and includes web service inputs and outputs for data to be submitted or returned, respectively.

  1. In your Azure Machine Learning workspace, select the Designer in the left pane and then open the “Predict CO2 Emissions from passenger cars” pipeline.
  2. Select Create inference pipeline > Real-time inference pipeline.
    Create real-tme inference pipeline
  3. After a few seconds, a new pipeline will be opened. Your real-time inference pipeline should look like this:
    Real-time inference pipeline
  4. Delete the Evaluate Model module, because is not useful when making predictions from new data.
  5. Delete the connection between the Score Model module and Web Service Output. In the left pane, expand the Python Language section and drag an Execute Python Script module onto the canvas.
  6. Connect the output port of the Score Model module to the left-most input port of the Execute Python Script module and the left output port of the new module to the input port of Web Service Output.
    Add an Execute Python Script module
  7. Replace the default script with the following Python code. This code selects only the Scored Labels column and renames it to Predicted CO2 Emissions.
import pandas as pd
def azureml_main(dataframe1 = None, dataframe2 = None):

    scored_results = dataframe1[['Scored Labels']]
    scored_results.rename(columns={'Scored Labels':'Predicted CO2 Emissions'},
    return scored_results
  1. Once you are ready, submit the pipeline. In the Set up pipeline run window, select Create new to create a new experiment named co2-emissions-inference. Click Submit and wait for the operation to finish. The first pipeline run may take 15-20 minutes to be completed.
    Set  up pipeline run
  2. When the pipeline run has finished, select Execute Python Script > Outputs + logs > Result Dataset > Visualize.
    Result dataset visualization
  3. Verify that the Predicted CO2 Emissions column is displayed.
    Result dataset visualization

Deploy a predictive service

Now you are ready to publish the predictive service!

  1. In the “Predict CO2 Emissions from passenger cars-real time inference” pipeline, select Deploy.
  2. Then deploy a new real-time endpoint named predict-co2-emissions and select Azure Container Instance as Compute type.
    Deploy a new real-time endpoint

Set up a compute instance

You will create a compute instance in order to test your model.

  1. In your Azure Machine Learning workspace, select the Compute tab in the left pane.
  2. In the Compute Instances tab, select Create and create a new compute instance with the following settings:
    • Virtual machine type: CPU
    • Virtual machine size: Standard_DS11_v2

      Under Virtual machine size, choose Select from all options and in the search field type Standard_DS11_v2.

    • Name: compute-instance-demo or another enter a unique name
    Create a compute instance
    Create a compute instance

Test your service

  1. On the Endpoints page, select the predict-co2-emissions real-time endpoint and in the Consume tab save the REST endpoint and the Primary key of your service. You will need this information in the next steps.
    Real-time endpoint keys
  2. On the Notebooks page, create a new Python Notebook name Test-co2-emissions.ipynb.
  3. In the first cell of the new notebook, paste the following code (this code is derived from the Microsoft Learn module Create a Regression Model with Azure Machine Learning designer). The following code submits the details of a new car and returns the predicted CO2 emissions. Note that we need to include the label column (CO2 Emissions), because the schema of the new data must meet the schema of the training data. However, the label value won’t be considered when predicting the CO2 emissions of this car.
endpoint = '<your REST endpoint>'
key = '<your primary key'

import urllib.request
import json
import os

# Prepare the input data
data = {
    "Inputs": {
                    'Year': 2019,
                    'Make': 'MINI',
                    'Model': "Cooper 5 Door",
                    'Vehicle Class': "Subcompact",
                    'Engine Size (L)': 15,
                    'Cylinders': 3,
                    'Transmission': "M6",
                    'Fuel Type': "Z",
                    'Fuel Consumption City': 85,
                    'Fuel Consumption Hwy': 67,
                    'Fuel Consumption Combined': 77,
                    'Fuel Consumption Combined (mpg)': 36,
                    'CO2 Emissions': 0,
    "GlobalParameters":  {
body = str.encode(json.dumps(data))
headers = {'Content-Type':'application/json', 'Authorization':('Bearer '+ key)}
req = urllib.request.Request(endpoint, body, headers)

    response = urllib.request.urlopen(req)
    result =
    json_result = json.loads(result)
    y = json_result["Results"]["WebServiceOutput0"][0]["Predicted CO2 Emissions"]
    print('Predicted CO2 Emissions: {:.2f}'.format(y))

except urllib.error.HTTPError as error:
    print("The request failed with status code: " + str(error.code))

    # Print the headers to help debug the error
    print(json.loads("utf8", 'ignore')))
  1. Run the code and verify that the Predicted CO2 emissions value is returned.
    Test the predictive service

    You can further explore your service by submitting information about other cars.


  1. In the Azure portal, select Resource groups on the right menu and then select the resource group that you have created.
  2. Click Delete resource group.

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top