Azure ML Studio — Deploy your python notebook model as a Web Service

Andreas Pogiatzis
5 min readDec 13, 2018

--

Source: Unsplash

Azure ML Studio can be a powerful tool in the arsenal of a data scientistas it provides the flexibility to rapidly experiment with out-of-the-box datasets and machine learning models. And guess what… with zero code! But for me, the ultimate feature is the option to deploy your model as a web service instantly in no time.

Let’s face it, although Azure ML Studio experiments can be an awesome way to create predictive models by just dragging and dropping modules on a workspace, the de facto tool for working on data science tasks is Jupyter notebook.

Still, Microsoft has your back with the Azure Notebooks service (https://notebooks.azure.com/) which provides FREE online hosting and management of Jupyter notebooks in an intuitive way. Yet, deploying a Jupyter notebook as a web service is a completely different story. For instance, you could build a Flask application, get a web server and deploy your code. But if that sounds too much for you! Azure ML Studio provides the option of creating and hosting your Jupyter notebooks online and deploying it as a web service while you building your models in the notebook!

This can be a real lifesaver when integrating web applications with data science, during prototyping, hackathons or even when you are looking to meet your deadline for launching your new machine learning startup.

In this post, we will be creating a python notebook which will be deployed as a web service for predicting the diagnosis of breast cancer given a set of morphological features.

The development of the predictive model is not the focus of this post so I won’t be concerned about the generalisability, validation and robustness of the model. Rather, it is an example problem to demonstrate the web service deployment

Preliminary Setup

Let’s go ahead and prepare our environment by firstly creating a python notebook in Azure ML Studio.

Navigate to https://studio.azureml.net/ and login to your workspace.

Click on the “Notebooks” tab on the left pane and then click “New” at the bottom.

Then select “Python 2” and name it whatever your like. Note that to the best of my knowledge this will only work for Python 2!

Now, leave the notebook open, go back to your workspace page and click on the “Settings” tab. From there, note down your workspace id as illustrated in the screenshot.

Click on “AUTHORIZATION TOKENS” tab at the top and note down the primary token also shown in the screenshot below.

Now with these information noted down, go back to your notebook and set the following variables:

workspace_id = "<workspace-id>"
workspace_token = "<workspace-token>"

Developing the model

Great! So far so good! Now it’s time to start building the model.

First add the line below into the notebook and execute in order to update scikit-learn package

!pip install -U scikit-learn

Next we will import all the modules necessary for the model, download, read the dataset in csv format and explore some basic information about the dataset by executing the code below:

The dataset we are using is the Breast Cancer Wisconsin (Diagnostic) Data Set which includes features that are computed from a digitized image of a fine needle aspirate (FNA) of a breast mass. More details about the dataset can be found here.

Next we will apply some preprocessing to the dataset before training our model. Since the values of the diagnosis column is encoded as ‘M’ for Malignant and ‘’B’ for Benign, we will map these into numeric values so that ‘M’ becomes 1 and ‘B’ becomes 0. Additionally, will split the data so that 80% of the 569 entries are used for training and the rest 20% for testing. Lastly, to make things simpler and avoiding using corelated features we will select only a subset of the available features in the dataset.

It’s time to train the model and evaluate its accuracy. The model that will be used is a Random Forest classifier. Normally, the choice of the model must be driven by cross-validating a wide variety of distinct models but for the purpose of this post Random Forest is good enough.

Go ahead the execute the following code:

The n_estimators sets the number of trees in the forest and n_jobs is the number of jobs to run in parallel upon training and prediction. I have explicitly set the number of jobs here to -1 (-1 means use all processors available) because I have notices that the web service raises an exception when the number of jobs is not set for an unknown reason.

Lastly we need to train and get the accuracy of the model:

This will give around 90% accuracy but remember, accuracy is not the only evaluation metric! In a real case scenario a more in depth evaluation must be performed. Nevertheless, for this case we are done with building the model and now it is ready for deployment.

Deploying the Web Service

Deploying the webservice is extremely simple. In general, you can deploy any function as a web service using the Azure Machine Learning SDK. Therefore, the plan is to create a function that takes features as parameters and returns the prediction of the model that we created previously.

Paste the following code into your notebook and execute it

On line 3 we use workspace_id and workspace_token constants that we defined earlier so make sure that you have these set already. The services.types and services.returns decorators are simply defining the types of the input and output values respectively. Lastly, we store some useful urls to call the services through an HTTP request from the notebook.

Calling the service

It is finally time to test our awesome web service. The service can be called either through the SDK itself or through an HTTP request. To call the service through the SDK add the code below to your notebook:

breast_cancer_predictor.service(018.14, 75.00, 0.09968, 0.05914, 0.1619)

Calling this will return either 0 or 1 which corresponds to Benign and Malignant diagnosis respectively. Feel free to experiment with the features and explore how the model behaves.

Alternatively you can call the web service by sending an HTTP request as shown below:

In this case we are sending an HTTP request from the jupyter notebook for demonstration purposes but in the same manner, it can be called by your client code using the service_url and api_key values.

Conclussion

I hope you enjoyed the tutorial folks! As a next step I would recommend checking out the Azure Machine Learning SDK documentation here to explore more about its awesome features.

You can find the complete notebook and dataset in my repository:

--

--

Andreas Pogiatzis
Andreas Pogiatzis

Written by Andreas Pogiatzis

☰ PhD Candidate @ UoG ● Combining Cyber Security with Data Science ● Writing to Understand

Responses (2)