How To Test Azure Machine Learning Using ASP.NET

In this tutorial, I will explain how we test a new Azure Machine Learning, by building an ASPX webpage. This webpage is based on the sample C# code provided with Azure Machine Learning Web Service.

Let’s start by creating an ASP.NET web application project.

Azure Machine Learning

After that, choose an Empty web project template.

Azure Machine Learning

Now, your project is created.

Next, right-click the project and select from the shortcut menu, add and select a new element.

Azure Machine Learning

Next, select a Web Form and name the page « Default.aspx ».

Azure Machine Learning

Let’s create a layout with input controls (text box, drop-down list…), this provides the user a list of options for each parameter required for the inputs to the Azure Machine Learning web service.

  1. <body>  
  2.     <form id="form1" runat="server">  
  3.         <div>  
  4.             <label>Age</label>  
  5.             <asp:TextBox runat="server" ID="age"></asp:TextBox><br />  
  6.   
  7.             <label>Classroom</label>  
  8.             <asp:TextBox runat="server" ID="Classroom"></asp:TextBox><br />  
  9.   
  10.             <label>Sex</label>  
  11.             <asp:DropDownList ID="sex" runat="server" AppendDataBoundItems="true" DataTextField="sex_field" DataValueField="id">  
  12.                 <asp:ListItem Text="M" Value="M" />  
  13.                 <asp:ListItem Text="F" Value="F" />  
  14.   
  15.             </asp:DropDownList></br>  
  16.   
  17.             <label>Education</label>  
  18.             <asp:DropDownList ID="Education" runat="server" AppendDataBoundItems="true" DataTextField="Company_Name" DataValueField="id">  
  19.                 <asp:ListItem Text="Doctorate" Value="Doctorate" />  
  20.                 <asp:ListItem Text="Masters" Value="Masters" />  
  21.                 <asp:ListItem Text="Engineer" Value="Engineer" />  
  22.                 <asp:ListItem Text="Bachelors" Value="Bachelors" />  
  23.                 <asp:ListItem Text="College" Value="College" />  
  24.             </asp:DropDownList></br>  
  25.              <label>Capital-gain</label>  
  26.             <asp:TextBox runat="server" ID="capital"></asp:TextBox><br />  
  27.             <asp:Button Text="Make Prediction" />  
  28.   
  29.         </div>  
  30.     </form>  
  31. </body>  

Then, run the project and you must have t.g like this

Azure Machine Learning

Next, let’s do the validation of the inputs, but before that, we should add a JavaScript file.

Azure Machine Learning

 Then, add this code.

  1. function validationInput() {  
  2.     var age = document.getElementById("age").value;  
  3.     if ((age < 0) || (age > 110)) {  
  4.         alert("Age In [0..110]");  
  5.         return false;  
  6.     }  
  7.     var classRoom = document.getElementById("Classroom").value;  
  8.     if ((classRoom < 0) || (classRoom > 10)) {  
  9.         alert("Classroom  In [0..10]");  
  10.         return false;  
  11.     }  
  12.     var capital = document.getElementById("capital").value;  
  13.     if ((capital < 0) || (capital > 99999)) {  
  14.         alert("classRoom  In [0..99999]");  
  15.         return false;  
  16.     }  
  17.   
  18. }  

Let’s program the prediction command button on the ASP.NET  web form page, we simply populate the values string array with the various input values from webpage as inputs,

But first, we should install.

Install-Package Machine.Learning.Client.API.Web.Service.Azure -Version 1.0.0

Azure Machine Learning

Azure Machine Learning

 

In the code behind of Default.aspx.cs, take the code of the Class AzureMLClient or instantiate the class AzureMLClient and make some modification to pass the data into the value inputs.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Net.Http;  
  5. using System.Net.Http.Headers;  
  6. using System.Threading.Tasks;  
  7. using System.Web;  
  8. using System.Web.UI;  
  9. using System.Web.UI.WebControls;  
  10.   
  11. namespace MLASP.NETWebPage  
  12. {  
  13.     public partial class Default : System.Web.UI.Page  
  14.     {  
  15.         protected void Page_Load(object sender, EventArgs e)  
  16.         {  
  17.   
  18.         }  
  19.   
  20.         protected void makePrediction(object sender, EventArgs e)  
  21.         {  
  22.             string age = this.age.Text;  
  23.             string classRoom = this.Classroom.Text;  
  24.             string sex = this.sex.SelectedItem.ToString();  
  25.             string education=this.education.SelectedItem.ToString();  
  26.             string capital = this.capital.Text;  
  27.             List<string> val = new List<string>();  
  28.             val.Add(age);  
  29.             val.Add(classRoom);  
  30.             val.Add(sex);  
  31.             val.Add(education);  
  32.             val.Add(capital);  
  33.             var score = new  
  34.             {  
  35.                 Inputs = new Dictionary<string, DataTable>()  
  36.                     {  
  37.                         {  
  38.                         "Input1"// The input name  
  39.                         new DataTable()  
  40.                         {  
  41.                             ColumnsNames=new string[] {"age""classRoom","sex","education","capital" }, // Your Columns name eg. ColumnsNames=new string[] {"Age", "education", "sex"},  
  42.                             Values= new string[,] { { age, classRoom, sex , education , capital } } // The value eg. Values= new string[,] { {"0","28", "0","value", "0","1" }}   
  43.                         }  
  44.                     },  
  45.                     },  
  46.                 GlobalParameters = new Dictionary<string, string>() { }  
  47.             };  
  48.             invokeRequestResponseService(score).Wait();  
  49.   
  50.   
  51.         }  
  52.         public class DataTable  
  53.         {  
  54.             public string[] ColumnsNames { get; set; }  
  55.             public string[,] Values { get; set; }  
  56.         }  
  57.   
  58.         static async Task invokeRequestResponseService(object score)  
  59.         {  
  60.             using (var client = new HttpClient())  
  61.             {  
  62.   
  63.                 const string apiKey = "Your key";  
  64.                 client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("your scheme name", apiKey);  
  65.                 client.BaseAddress = new Uri("YOUR URI");  
  66.                 HttpResponseMessage resp = await client.PostAsJsonAsync("", score);  
  67.                 if (resp.IsSuccessStatusCode)  
  68.                 {  
  69.                     string result = await resp.Content.ReadAsStringAsync();  
  70.                 }  
  71.                 else  
  72.                 {  
  73.                     //Error  
  74.                 }  
  75.   
  76.             }  
  77.         }  
  78.     }  
  79. }  
Azure Machine Learning

We can then invoke our call to the Azure Machine Learning web service.

You can see how this type of simple ASP.NET webpage application can make it far easier to interact with our Azure Machine Learning web service to perform basic testing operations via the use of basic UI controls like list boxes and client-side validation. This approach also gets around the current support restriction for CORS capabilities in Azure Machine Learning web services as the ASP.NET implementation is all done on the server.

The other advantage of this approach is that it allows you to quickly create and deploy an ASP.Net web application to the cloud to help test your Azure Machine Learning prediction model over the Internet. This means your teams can help evaluate your predictive models even faster.