In this post, I will work on the machine learning predictive model and how to explore the types of client and server applications that you can use to consume Azure Machine Learning web service.
I will walk through building various client and server applications meant to consume Azure Machine Learning predictive models exposed as web services.
This example contains a very simple and basic client application and explores a full-blown ASP.NET web API web service implementation that implements the REST protocol and provides Azure Machine Learning predictions using JSON for the inputs and the outputs.
Let’s begin by looking at the default sample code the call an Azure Machine Learning web services that we have exposed.
You can reach a code sample page by navigation to your Azure Machine Learning workspace from the global menu.
- Go to Azure Machine Learning and select your workspace.
- Choose the right implementation version for the Azure Machine Learning Web Service.
- Select the API help page for the request/Response option.
- After that, we will be directed to a web page where we can easily view all the technical details to invoke the Azure Machine Learning web service over the HTTP POST.
The help page for the Azure Machine Learning API web service provides specific implementation details about the following aspects of making an Azure Machine Learning web service via an HTTP POST request,
- OData End Point Address
- Method Request URI Format
- POST Request Headers
- Sample REQUEST body
- RESPONSE – Status Codes
- RESPONSE – Headers
- RESPONSE – Body
- RESPONSE – Sample Reply
Let’s start with creating a new clients application to call our new Azure Machine Learning web service from C#.
Create a Console Application.
Invoke the NuGet Package Manager console.
Install the Microsoft.AspNet.WebApi.Client NuGet packages and their dependencies:
Install-Package Microsoft.AspNet.WebApi.Client
Implementation
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Net.Http;
- using System.Net.Http.Headers;
-
- First create a DataTable class
- public class DataTable
- {
- public string[] ColumnsNames { get; set; }
- public string[,] Values { get; set; }
- }
This class contains two string arrays used for invoking the web service.
- The ColumnNames string array contains the column names for the input parameters.
- The Values string array will contain the individual values for each corresponding parameter passed into the web service call.
Then, create in the program class.
- class Program
- {
- static void Main(string[] args)
- {
- invokeRequestResponseService().Wait();
- }
-
- }
Then, implement the invokeRequestResponseService method.
- static async Task invokeRequestResponseService()
- {
- using (var client =new HttpClient())
- {
- var score = new
- {
- Inputs = new Dictionary<string, DataTable>()
- {
- {
- "Input1",
- new DataTable()
- {
- ColumnsNames=new string[] {"Age", "education", "origin"},
- Values= new string[,] { {"0","28", "0","value", "0","1" }}
- }
- },
- },
- GlobalParameters=new Dictionary<string, string>() { }
- };
- const string apiKey = "abc123";
- client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("scheme", apiKey);
- client.BaseAddress = new Uri("YOUR URI");
- HttpResponseMessage resp = await client.PostAsJsonAsync("", score);
- if (resp.IsSuccessStatusCode)
- {
- string result = await resp.Content.ReadAsStringAsync();
- }
- else
- {
- Console.WriteLine("Error {0}", resp.StatusCode);
- }
-
- }
- }
You can find your API key for your web service by navigating to the web service tab of your Azure Machine Learning Studio workspace.
The result of the execution.
In this example, the response back from our call to the Azure Machine Learning web service indicates that this input candidate likely makes less than $50,000 a year. The corresponding confidence level is 0.0010072038276121.