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.
After that, choose an Empty web project template.
Now, your project is created.
Next, right-click the project and select from the shortcut menu, add and select a new element.
Next, select a Web Form and name the page « Default.aspx ».
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.
- <body>
- <form id="form1" runat="server">
- <div>
- <label>Age</label>
- <asp:TextBox runat="server" ID="age"></asp:TextBox><br />
-
- <label>Classroom</label>
- <asp:TextBox runat="server" ID="Classroom"></asp:TextBox><br />
-
- <label>Sex</label>
- <asp:DropDownList ID="sex" runat="server" AppendDataBoundItems="true" DataTextField="sex_field" DataValueField="id">
- <asp:ListItem Text="M" Value="M" />
- <asp:ListItem Text="F" Value="F" />
-
- </asp:DropDownList></br>
-
- <label>Education</label>
- <asp:DropDownList ID="Education" runat="server" AppendDataBoundItems="true" DataTextField="Company_Name" DataValueField="id">
- <asp:ListItem Text="Doctorate" Value="Doctorate" />
- <asp:ListItem Text="Masters" Value="Masters" />
- <asp:ListItem Text="Engineer" Value="Engineer" />
- <asp:ListItem Text="Bachelors" Value="Bachelors" />
- <asp:ListItem Text="College" Value="College" />
- </asp:DropDownList></br>
- <label>Capital-gain</label>
- <asp:TextBox runat="server" ID="capital"></asp:TextBox><br />
- <asp:Button Text="Make Prediction" />
-
- </div>
- </form>
- </body>
Then, run the project and you must have t.g like this
Next, let’s do the validation of the inputs, but before that, we should add a JavaScript file.
Then, add this code.
- function validationInput() {
- var age = document.getElementById("age").value;
- if ((age < 0) || (age > 110)) {
- alert("Age In [0..110]");
- return false;
- }
- var classRoom = document.getElementById("Classroom").value;
- if ((classRoom < 0) || (classRoom > 10)) {
- alert("Classroom In [0..10]");
- return false;
- }
- var capital = document.getElementById("capital").value;
- if ((capital < 0) || (capital > 99999)) {
- alert("classRoom In [0..99999]");
- return false;
- }
-
- }
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
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.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net.Http;
- using System.Net.Http.Headers;
- using System.Threading.Tasks;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
-
- namespace MLASP.NETWebPage
- {
- public partial class Default : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
-
- }
-
- protected void makePrediction(object sender, EventArgs e)
- {
- string age = this.age.Text;
- string classRoom = this.Classroom.Text;
- string sex = this.sex.SelectedItem.ToString();
- string education=this.education.SelectedItem.ToString();
- string capital = this.capital.Text;
- List<string> val = new List<string>();
- val.Add(age);
- val.Add(classRoom);
- val.Add(sex);
- val.Add(education);
- val.Add(capital);
- var score = new
- {
- Inputs = new Dictionary<string, DataTable>()
- {
- {
- "Input1",
- new DataTable()
- {
- ColumnsNames=new string[] {"age", "classRoom","sex","education","capital" },
- Values= new string[,] { { age, classRoom, sex , education , capital } }
- }
- },
- },
- GlobalParameters = new Dictionary<string, string>() { }
- };
- invokeRequestResponseService(score).Wait();
-
-
- }
- public class DataTable
- {
- public string[] ColumnsNames { get; set; }
- public string[,] Values { get; set; }
- }
-
- static async Task invokeRequestResponseService(object score)
- {
- using (var client = new HttpClient())
- {
-
- const string apiKey = "Your key";
- client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("your scheme name", apiKey);
- client.BaseAddress = new Uri("YOUR URI");
- HttpResponseMessage resp = await client.PostAsJsonAsync("", score);
- if (resp.IsSuccessStatusCode)
- {
- string result = await resp.Content.ReadAsStringAsync();
- }
- else
- {
-
- }
-
- }
- }
- }
- }
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.