Azure ML Tutorial 1
Azure ML Tutorial 1
Azure ML Tutorial 1
Contents
Introduction .................................................................................................................................................. 1
Create your workspace ................................................................................................................................. 2
Create your first Machine Learning Experiment ........................................................................................... 6
Importing data .......................................................................................................................................... 7
Simple Pre-Processing ............................................................................................................................. 12
Actual Machine Learning ........................................................................................................................ 16
Results ..................................................................................................................................................... 20
Publishing your experiment ........................................................................................................................ 21
Creating a Windows 10 application (UWP)................................................................................................. 24
Wrap up ...................................................................................................................................................... 32
Introduction
This tutorial aims to introduce you to AzureML and show you how you can easily create your own
experiment and publish it into a service. We will do this through an example. We are going to use a
dataset containing vote information from US elections at 1984 and make a simple Windows 10
Application “predicting” whether you would be a democrat or a republican at that time.
This tutorial is meant for people that now about Machine Learning and would like to see how this
platform works and what it has to offer but it is also meant for beginners that would like to get started.
However, the purpose of the tutorial is not to teach data analysis but to provide an overview of the tools
and the platform offered by Microsoft Azure.
Links:
Github: https://github.com/georschi/Azure-Machine-Learning-and-UWP-application
You can visit https://studio.azureml.net and sign up for free with a Microsoft Account, without a need of
having an Azure Subscription.
Importing data
A great benefit of AzureML is the ease at which we can bring data into our experiment. You can either
have your own datasets saved online or import them from anywhere in the internet (you can connect it
to your SQL database if you would like to). We will import our data from a webpage.
Now it’s time for our first RUN! Notice the bar at the bottom of your screen and press RUN.
After a while the experiment will have run and now by clicking this little circle you can visualize the
results
You can see that we have not names for our columns and there are some missing values. We will deal
with this right now.
column_name
Class
handicapped-infants
water-project-cost-sharing
adoption-of-the-budget-resolution
physician-fee-freeze
el-salvador-aid
religious-groups-in-schools
anti-satellite-test-ban
aid-to-nicaraguan-contras
mx-missile
immigration
synfuels-corporation-cutback
education-spending
superfund-right-to-sue
crime
duty-free-exports
export-administration-act-south-africa
Drag and connect the modules as shown in the picture. This way you determine what the input to your
script will be.
The variables dataset1 and dataset2 contain the datasets we gave as input.
The lines 5&6 change the name of the columns and then we delete all those lines that contain a missing
character. NOTE that you could do something else with the missing values, but it’s not the purpose of
this tutorial to deal with these issues.
By running again the experiment and visualizing the result you can see that our dataset is now ready for
machine learning.
Let’s do it!
Up to this point we have trained our model. But we need to see how good it is. So we have 2 more
modules to add.
For our example, we see some great results. One could argue that these results are not representative
since we omitted all those rows with missing values that could substantially contribute to the
performance of our model. This might be the case but again it is out of the scope of this tutorial.
Our model has been trained and therefor now it’s time to publish it and see how to integrate it within an
app.
Note your API Key since we are going to need it. By pressing Test a window pops up and asks us to enter
values for our input and by clicking the ok button we get a result.
Follow my lead!
Below these definitions we will add the following code that in the one row puts the questions and at the
other it give the user the freedom to choose between yes and no.
Now, open the MainPage.xaml.cs and right before the constructor of the MainPage declare a variable
private string[] answers = new string[6];
That page explains exactly what happens with the http calls and gives you hints and help to implement
them in any platform, so if interested, give it some time. For our project we will take advantage of some
C# code that has been regenerated for us. Simply scroll down a bit and you’ll find it.
From that code, copy the function static async Task InvokeRequestResponseService()
which contains the basic structure to call the service. After you copy-paste it there will be a few error
indications but don’t worry! That’s only because this C# code was generated for a console application
and not a UWP. Make a few changes to your code so that it looks like the one below (or you directly that
instead)
private async Task InvokeRequestResponseService()
{
using (var client = new HttpClient())
{
var scoreRequest = new
{
Inputs = new Dictionary<string, StringTable>() {
if (response.IsSuccessStatusCode)
{
string json = await response.Content.ReadAsStringAsync();
dynamic result = JsonConvert.DeserializeObject<dynamic>(json);
var whatAreYou = result.Results.output1.value.Values[0][7];
var thatMuch = result.Results.output1.value.Values[0][8];
float percent = (float)thatMuch;
percent = (percent < 0.5) ? (1 - percent) * 100 : percent * 100;
percent = (int)percent;
var dialog = new MessageDialog(String.Format("You are a {0} and I'm
{1}% sure about it!", whatAreYou, percent));
await dialog.ShowAsync();
}
else
{
// custom code to deal with a problem at the call
}
}
}
In that code what you need to notice to put your own API Key and Uri. The errors that remain will go
away once you add this class in your code
public class StringTable
{
public string[] ColumnNames { get; set; }
public string[,] Values { get; set; }
}
There is one final thing to add here and we are done! This is nothing other than calling that function and
filling up the variable answers[] with the appropriate content, when the button submit is clicked.
To do so, add the following code and you’re good to go (when writing the xaml part you may have
noticed that at the button we already added the click event Click="Button_Click").
What this function does is to search all the ToggleSwitch items we have and put their respective value in
the variable answers[] at the correct position. You may need to take some time to agree with that line of
code, don’t worry!
Run the application and have fun, it should look like this:
Wrap up
If you’ve reached this point you have successfully finished this tutorial. Congratulations!
Thanks for staying with me throughout my first tutorial. Please feel free to leave any comments or
questions, See you soon!