Volume 32 Number 11
Exposing Machine Learning Models from Azure Machine Learning Studio
By Frank La | November 2017
One of the key drivers of innovation in the machine learning (ML) and artificial intelligence (AI) space has been the fact that much of the results of cutting-edge research has “left the lab” and is available to the public. Services such as Cortana and Skype Translate that rely on years of work in speech recognition and linguistic analysis are in the hands of users worldwide.
In my previous column (msdn.microsoft.com/mt826348), I demonstrated how to create an ML model in Azure Machine Learning Studio. Using a sample experiment as a starting point, I created a model that predicted whether a flight would be canceled with an accuracy rate of slightly greater than 80 percent. While this model might be useful on its own, the full transformative power of AI would be realized if I made this model available to more than just the data scientists on my team. What if this predictive model were exposed to business analysts? What if it was made easy for them to consume the predictive model from within Excel? What if the model was exposed to app developers to embed into any number of applications?
In this article, I’ll demonstrate how to expose ML models created in Machine Learning Studio to non-data-scientist users.
Creating a Web Service in Machine Learning Studio
Fortunately, Machine Learning Studio makes it straightforward to expose ML models via a Web service. Browse over to Machine Learning Studio at studio.azureml.net. If you followed the steps in my previous column, please open up the Binary Classification: Flight delay prediction experiment. If not, go ahead and follow the steps I outlined last month to create the experiment.
Once loaded, there should be a button at the bottom of the Web page called Set Up Web Service. Click on it and click on the Predictive Web Service [Recommended] option in the context menu that appears (see Figure 1). If the option to create a Predictive Web Service is greyed out, then you might have to run the experiment again. Click the Run button to the immediate left of the Set Up Web Service button as shown in Figure 1.
Figure 1 The Set Up Web Service Context Menu in Azure Machine Learning Studio
Once you click the menu option, the dialog in Figure 2 will appear. Machine Learning Studio will prompt you to pick a Train Model module. Why? Two ML models are trained in this experiment: one using a Two-Class Boosted Decision Tree algorithm and the other using a Two-Class Logistic Regression algorithm. Recall that the Two-Class Logistic Regression achieved an accuracy of 81.7 percent, while the Two-Class Boosted Decision Tree achieved 80.6 percent. The model I want to expose as a Web service, therefore, is the model created with the Two-Class Logistic Regression algorithm.
Figure 2 Azure Machine Learning Studio Will Prompt You to Pick a Train Model Module
The right side of the experiment layout—after the project splits into two paths—appears as what’s shown in Figure 3. The Tune Model Hyperparameters module performs a parameter sweep on a model, in this case the Two-Class Boosted Decision Tree just above the module, to determine the best parameter settings. To get an idea of what this module does, right-click on the right-hand output node and choose Visualize.
Figure 3 The More Accurate Model Is the Two-Class Logistic Regression, Enhanced by Tune Model Hyperparameters
Once the dialog loads, it should be similar to Figure 4. Machine Learning Studio ran through a number of permutations and created 368 different decision trees using the different input data fields as decision points to find the optimal decision tree. Clicking on any node in a tree will reveal statistics about the node and its impact on creating the output model. The order in which the trees are displayed in the left-hand column indicate their rank in terms of producing an accurate model. Feel free to explore this dialog to get an appreciation of all the work that this module performed.
Figure 4 Visualizing a Tune Model Hyperparameters Module
When you’re ready to proceed, close out this dialog. Make sure that the Tune Model Hyperparameters module is selected and then click the Set Up Web Service button. In the context menu that follows, click the Predictive Web Service [Recommended] option. In a few moments, the modules in the experiment will move around and the experiment will split into two tabs: Training Experiment and Predictive Experiment. The work done so far is preserved in the Training Experiment Tab. The Predictive Experiment tab contains the same trained model and data wrangling methods with modifications needed to expose the experiment as a Web service. Click Run to execute the experiment. Once the experiment finishes running, click on the Deploy Web Service button to complete the process of exposing this data model as a Web service.
The Web service dashboard appears on screen once the Web service is deployed. The Web service dashboard provides information on how to access, configure and test the Web service. Click on the Test link highlighted in Figure 5.
Figure 5 Web Service Dashboard in Azure Machine Learning Studio
Click the button to enable Sample Data on the screen that follows. Enabling Sample Data exposes a subset of the training data in the test dialog. The goal is to make the Web service easier for users to begin using the Web service. After a moment, the text fields on the form are populated with values. Click the Test Request-Response button to test the Web service. Test results will appear in the right-hand side of the screen after a few moments.
Consuming the Trained Model in Excel
Now that the trained model has been deployed as a Web service it can be consumed by any HTTP REST-capable client, including Excel 2013 and newer. Excel is an excellent client application for any predictive model, as it’s the de facto analytics tool for business users worldwide. Furthermore, Excel integrates with numerous other analytics tools, such as Power BI.
Installing the Azure Machine Learning Add-In
To consume models from Azure Machine Learning you need to install the Azure Machine Learning add-in to Excel. Click on the Insert tab and click the Store button (see Figure 6).
Figure 6 Installing an Add-in to Excel 2016
In the following dialog click on Store and type “Azure Machine Learning” into the search box and hit Enter. As seen in Figure 7, the Azure Machine Learning add-in is the first result. Click Add to install it.
Figure 7 Searching the Store for the Azure Machine Learning Add-in
Once the add-in is installed, an Azure Machine Learning pane appears on the right-hand side of the spreadsheet. Note that there are services already added by default. Click on the Add Web Service button to add the Web service just created. The Add Service dialog in the Azure Machine Learning pane will then display two textboxes: One for the Web service URL and another for the API key to access the service. The API key is found on the Web service dashboard in Figure 5. The URL can be found on the same dashboard. In the Default Endpoint section, right-click the text link Request/Response and choose Copy Link Address to copy the URL into the clipboard. Paste that value into the URL textbox in Excel, as shown in Figure 8.
Figure 8 Adding an Azure Machine Learning Web Service to Excel
Click Add to Add the Web Service to the Current Spreadsheet.
Consuming the Web Service from Excel
Once the Web service is added you can use it. In the Azure Machine Learning pane, click Binary Classification – Flight Delay prediction from the list of available services. Click Use Sample Data and, in a moment, the spreadsheet is populated with a table of sample values. In the Input textbox, enter “Sheet1!A1:N6” to include all of the sample data (assuming that the worksheet is named Sheet1). In the Output textbox enter “O1.” Immediately below the Output text box a warning message will display about overriding existing values. Click Got It to dismiss the warning message.
Click Predict and the predicted values display in a few moments. The Add-in and link to the Web service are now part of the Excel file. You may now save and share the file with interested parties. Other users won’t need to subsequently install the Add-in or configure the Web service, as the add-in installation and configuration has been taken care of for them.
Accessing the Web Service in Other Applications
Navigate to the Web service dashboard displayed in Figure 5. Note that—as of this writing—there is a new Web service experience in preview. Click the link to view the new Web service dashboard experience. Under Basics, click the “Use endpoint” link to view a number of options on how to consume the service.
In addition to the API access keys and service endpoints, there’s sample code for accessing the service in C#, Python and R. There’s also a link to the Azure ML Request-Response Service Web App project that will instantly create an Azure Website that consumes the data, which can be found at bit.ly/2wBHE1j. For more information and for a detailed walk-through on how this template works, please refer to the documentation at bit.ly/2yt99Ye.
In this article, I demonstrated how to expose ML models created in Machine Learning Studio to users in the outside world. Starting with a predictive model created in the previous column, I demonstrated exposing that model to more than just data scientists. After all, a predictive model is only as useful as it is accessible to decision makers. Much of the rapid pace of innovation in machine learning lately can be attributed to making Azure Machine Learning technology widely available to business users and consumers.
Leveraging the cloud, these models may be deployed and scaled without any additional infrastructure burden on the organization’s existing IT Systems. In fact, little additional effort is required on the part of developers to integrate predictive models from Machine Learning Studio. Now that it’s this simple to make these models widely available for exploratory analysis in Excel and integration into other applications, the potential to add value with machine learning is limitless.
Frank La Vigne leads the Data & Analytics practice at Wintellect and co-hosts the DataDriven podcast. He blogs regularly at FranksWorld.com and you can watch him on his YouTube channel, “Frank’s World TV” (FranksWorld.TV).
Thanks to the following technical experts for reviewing this article: Andy Leonard