Developers Perspective On Azure Machine Learning Studio

Overview

This article is meant to provide a brief background on what Azure Machine Learning Studio is but more importantly from a developers perspective. In this article I am going to show how developers can tap into Azure Machine Learning Studio (AMLS) from within our applications. In short, take advantage of all of the strides being made in machine learning and apply those techniques directly within our applications. An important disclaimer: this is in now way shape or form about data science. That is an entire discipline in itself. This is more about how we can act on data science.

A term that you hear a lot is about the democratization of machine learning. AMLS is one example of that. Microsoft Azure ML Studio offers access to many sophisticated ML models through a simple graphical UI (A loose analogy that I have in my head is SSIS). It doesn’t require a PhD in statistics to leverage some very sophisticated ML algorithms. Granted there needs to be an understanding of what algorithms to use and when to use them but we are not writing the code for say a linear regression analysis.

There is no doubt with the advances in technology, specifically the cloud, the coalescing of disciplines and access to into those disciplines is making the types of things we can do as developers not only easier but brings in a new level of sophistication that was previously hard to achieve. Take for example distributed and scaling our applications worldwide. What once was extremely difficult is now almost as easy as pushing a button. Azure Machine Learning Studio (AMLS) is no different.

What is Azure Machine Learning Studio

Microsoft Azure Machine Learning Studio (AMLS)is a collaborative, drag-and-drop cloud based tool you can use to build, test, and deploy predictive analytics solutions on data. One of the aspects that becomes directly useful for our applications is the fact that AMLS can publish ML models as web services that we then can consume from within our custom apps or BI tools such as Excel and Power Bi or any custom application.

The focus on this article is not on Machine Learning Studio directly but rather how we can leverage the output of AMLS. Before we get into leveraging the output, let me just mention a few key things about AMLS.

How Do I Get Started

Microsoft offers a free MLS workspace. A great place as a developer to put on out data scientist hat and try stuff out.

Sign up for a free account here.

I highly recommend signing up for a free work space and tinkering. Its a brave new world!

What kind of things can I do in Azure Machine Learning Studio

Through AMLS you get a visual work space where you can build, test and iterate on machine learning models. You can drag-and-drop datasets and incorporate proven, out of the box analysis modules (think known algorithms such as liner regressions) onto an interactive canvas. Connect data and algorithms through a workflow to form machine learning experiments. You use Azure Machine Learning Studio to run your experiments, see results, evaluate results and iterate. Once you are satisfied with your ML experiment, you can publish your experiment to a web service which in turn makes it useful to consumers like applications. The following diagram depicts the type of work flow from withing AMLS.


What kind of business problems can Azure Machine Learning Studio help solve?

In this day an age of Big Data, how can we leverage this data to help make important business decisions. Having the data coupled with running machine learning techniques and algorithms on that data can lead to answer important business answers. Here are the types of questions and answers that Azure Machine Learning Studio can help answer.

  1. I want to predict something. Examples includes things like wanting to predict the demand for a product. Predict future sales growth. Predict when equipment will need servicing. These examples all fall into the category of predicting things through machine learning via regression algorithms.
  2. I want to find unusual things. Examples include predicting a credit default, identifying credit card fraud, detecting equipment failures. These examples all fall into the category machine learning via anomaly detection algorithms.
  3. I want to find structure. Examples include segmentation of customers, segmentation of voters, predicting customers likes, predicting which products are typically purchased together and by whom. These all fall into the category of machine learning via clustering algorithms
  4. I want to predict between two things. Examples include is this tweet positive? Is this email positive? Which coupons result in the most sales? These all fall into the category of two class classification algorithms.
  5. I want to predict between many things? Examples include what is the mood of this tweet, what service will a customer likely choose, which sales promotions draw the most customers. These too fall into the category of machine learning through classification algorithms

This are just a few examples of the types of questions we can answer by leveraging data, Azure Machine Learning Studio and machine learning techniques.

What are the tools that Azure Machine Learning Studio Provides

To develop a predictive analysis model, you typically use data from one or more sources, transform and analyze that data through various data manipulation and statistical functions and algorithms, and generate a set of results. Developing a model like this is an iterative process. As you modify the various functions and their parameters, your results converge until you are satisfied that you have a trained, effective model. AMLS gives use the tools we need to complete the machine learning workflow. The major component with AMLS the help organize our machine learning experience are

  • PROJECTS — This helps us organize all the experiments, datasets, notebooks, and other resources representing a single machine learning project. Think of this similar to a project or solution in Visual Studio.
  • EXPERIMENTS —This is where we run the actual experiments like predicting sales figures within AMLS. Within our experiments will will incorporate items such as:

Datsets— Data upload or extracted in our experiments and incorporated into our ML models,

Modules — a set of code that can run independently and perform a machine learning task, given the required inputs. A module might contain a particular algorithm, or perform a task that is important in machine learning, such as missing value replacement, or statistical analysis. This includes known algorithms and programming languages such as R and Python.

More details can be found here.

Data flows. This is a visual representation of how our experiments flow. Think of this similar to an SSIS package where we have data flow through an ETL process. Similarly there is a machine learning process.

  • WEB SERVICES — From a developer perspective this has the most use for us. This is where we deploy our experiments to web services so they can be consumed by our applications.
  • NOTEBOOKS — Jupyter notebooks that you have created that can be incorporated into our experiments
  • DATASETS — Datasets used in our experiments that you have been uploaded into AMLS.
  • TRAINED MODELS — Models that you have trained in experiments and saved in Studio
  • SETTINGS — A collection of settings that you can use to configure your account and resources.

“This is all great but show me how all this really makes a difference to me as a developer and the applications I build….”

Don’t Bore Us Get to the Chorus…

Completely agree with this sentiment. All of the above information is really just to provide a background and provide a brief understanding of what AMLS is. We have to start somewhere right…

Lets talk about a specific example of how this all can be incorporated into an application.

Lets imagine for a moment we work for a bank and we are taking applications for a credit card. Potential customers go to some application page and we gather important information about our potential customer during the application process. Important and relevant information includes items such as age, education level, current salary, monthly rent / mortgage, marital status, number of dependents, monthly bill amounts. All great information and we can leverage this information to get our customers aligned with the right credit card. Maybe more importantly we can predict how good a customer they may be by predicting their credit risk.

What if we could know right there, at the time of filling out an application things like

A) Is this applicant a high credit risk

B) This applicant is likely a low credit risk so let’s pre-appove them

C) We are not sure if this is a real customer or not and they may be trying to game the application process.

These are the exact type of questions and answers that AMLS can help us with. We can build models that we can expose to our applications that can answer these questions. Imagine in real time, if you could ask a web service at the end of an application process if this user is a low credit risk? Knowing that answer would most likely inform the application process. For example, if we predict its a low credit risk then pre-approve the card. If we predict the user is a high credit risk, funnel the application through a human validation process. If we detect that we have seen too many similar applications in a short time frame, flag the application as potential fraud. These are examples of the exact type of things we can now do in our applications leveraging machine learning.

Machine Learning In Action

Azure Machine Learning Studio has a great example of how easy it is to create an experiment in which we can predict credit risk using existing data . I don’t want to go into the specifics here as Azure already has a really useful tutorial . I highly recommend signing up for a free AMLS work space and walking through this tutorial. This will give you a feel for

  • Creating a Machine Learning Studio work space.
  • Upload and leveraging existing data in an machine learning experiment
  • Training your machine learning experiment

The more important thing I want to cover here is that after the experiment has been created we can publish that experiment to a web service that we can then leverage directly from our applications. This gives us the ability to predict right there on the spot if our credit card applicant is a credit risk.

Publish Machine Learning To Web Services

So now we are speaking the language of developers. In the ever prevalent microservice, api ecosystem, we add web services into that eco system that use machine learning. Let’s drill a bit further into this.

After walking through the credit risk machine learning experiment, we come to a spot where we want to expose this to our applications. In our specific case, the application process for a credit card.

The credit experiment should look something like the following in AMLS prior to publishing to a web service


Inside of AMLS, the next step is set this experiment as a predictive web service . We do that by selecting the Setup Web Service menu item


Once the web service has been published, you will see something like the following within AMLS.


The key here is that we can see we have 2 new components within AMLS, Web service input and output respectively. I imagine you are starting to see how our experiment is starting to take shape in the sense that we can send input through it and get output.

Our last step is to deploy this credit risk predictive model as a web service. To do that select the deploy web service menu item. That should lead you to a screen that looks like this


From here we can test our web service by entering in the required input data. Doing so will give us a predicted credit risk response by running the variable input through our ML model.

Other important information from with AMLS is the API Key. This gives us the information we need in order to connect and consume this predictive web service from say some C# web application. In our case, the credit card application web site. Once the web service is published, from within AMLS we will now see an entry that details our new service.


We can click on each web service to get all kinds of good information about it. For example, the expected inputs, the expected outputs, the key to access the service published within Azure.

All told the workflow we went through within AMLS looks something like the following:


As developers, we are probably going to be more focused on the consumption side of the predictive web service. All the work being done within the develop iterate and test is more than likely in the realm of data scientists. However, there is no reason that with a good understanding of ALMS coupled with an understanding of how, when and what machine learning modules/algorithms to use that we can’t do this ourselves. This goes back to my earlier point about how disciplines are coalescing.

Consume a Predictive Web Service

This last section details how we can code our c# applications to consume web services published through AMLS. The key here is that this predictive credit risk web service is no different that any other REST Api service we are all familiar with. In our example of the credit card application process, during the save of an application we can create an API request to the credit risk web service, get a response and act accordingly. In this specific example we can get the probability of a credit risk based on the input parameters.

The following is a sample C# request to the service.

using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Net.Http.Formatting;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;

namespace CallRequestResponseService
{
class Program
{
static void Main(string[] args)
{
InvokeRequestResponseService().Wait();
}

static async Task InvokeRequestResponseService()
{
using (var client = new HttpClient())
{
/*
Here we are setting up all the required input parameters for the predictive webservice
*/
var scoreRequest = new
{
Inputs = new Dictionary<string, List<Dictionary<string, string>>> () {
{
"input1",
// Replace columns labels with those used in your dataset
new List<Dictionary<string, string>>(){new Dictionary<string, string>(){
{
"column1", "value1"
},
{
"column2", "value2"
},
{
"column3", "value3"
}
}
}
},
},
GlobalParameters = new Dictionary<string, string>() {}
};

// Replace these values with your API key and URI found on https://services.azureml.net/
const string apiKey = "<your-api-key>";
const string apiUri = "<your-api-uri>";

client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue( "Bearer", apiKey);
client.BaseAddress = new Uri(apiUri);

// WARNING: The 'await' statement below can result in a deadlock
// if you are calling this code from the UI thread of an ASP.Net application.
// One way to address this would be to call ConfigureAwait(false)
// so that the execution does not attempt to resume on the original context.
// For instance, replace code such as:
// result = await DoSomeTask()
// with the following:
// result = await DoSomeTask().ConfigureAwait(false)

HttpResponseMessage response = await client.PostAsJsonAsync("", scoreRequest);

if (response.IsSuccessStatusCode)
{
string result = await response.Content.ReadAsStringAsync();
Console.WriteLine("Result: {0}", result);
}
else
{
Console.WriteLine(string.Format("The request failed with status code: {0}", response.StatusCode));

// Print the headers - they include the request ID and the timestamp,
// which are useful for debugging the failure
Console.WriteLine(response.Headers.ToString());

string responseContent = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseContent);
}
}
}
}
}

And a JavaScript example for the client side version of interacting with our predictive web service

let req = require("request");

const uri = "<your-api-uri>";
const apiKey = "<your-api-key>";

let data = {
"Inputs": {
"input1":
[
{
'column1': "value1",
'column2': "value2",
'column3': "value3"
}
],
},
"GlobalParameters": {}
}

const options = {
uri: uri,
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer " + apiKey,
},
body: JSON.stringify(data)
}

req(options, (err, res, body) => {
if (!err && res.statusCode == 200) {
console.log(body);
} else {
console.log("The request failed with status code: " + res.statusCode);
}
});

For our particular example, the key bits of information that we are looking for, namely credit risk probability, will be on the response from our API request to the web service. In this case the field called scored probability. That will represent the probability of a credit risk based on the input parameters and scored by our predictive credit risk web service. Reading that value and acting appropriately is an example of how we can use machine learning within an application.

Overall Azure Machine Learning Studio seems really powerful. The interface seems to pull in concepts similar to other tools such as SSIS, Azure Data Factory, and even Visual Studio. I am sure pure data scientist may say that AMLS does not have all the features required that they would typically use in their day to day jobs. That may be a fair point. Since I am not a data scientist I can’t fully comment on that sentiment. That being said though, features are being added frequently and with time I bet this will be a full fledged suite of machine learning technologies. Recent interrogations with R, Python, Jupyter notebooks indicate that catering to the data scientist crowd is at the forefront. Similar to Visual Studio, AMLS will evolve and mature with time. Already it seems extremely robust to me. I invite you to learn more for yourself.

Leave a Reply