Page 54 - MSDN Magazine, November 2018
P. 54
TesT Run JAMES MCCAFFREY Introduction to the ML.NET Library
The ML.NET library is an open source collection of machine learning (ML) code that can be used directly in .NET applications. Most ML libraries, such as TensorFlow, Keras, CNTK, and PyTorch, are written in Python and call into low-level C++ routines. However, if you use a Python-based library, it’s not so easy for a .NET application to access a trained ML model. Fortunately, the ML.NET library integrates seamlessly into .NET applications.
The best way to see where this article is headed is to take a look at the demo program in Figure 1. The demo creates an ML model that predicts whether a patient will die or survive based on the patient’s age, sex, and score on a kidney medical test. Because there are only two possible outcomes, die or survive, this is a binary classification problem.
Behind the scenes, the demo program uses the ML.NET library to create and train a logistic regres- sion model. As I’m writing this article, the ML.NET library is still in preview mode, so some of the information presented here may have changed by the time you’re reading this.
Figure 1 ML.NET Demo Program in Action
The demo uses a set of training data with 30 items. After the model was trained, it was applied to the source data, and achieved 66.67 percent accuracy (20 correct and 10 wrong). The demo concludes by using the trained model to predict the outcome for a 50-year- old male with a kidney test score of 4.80—the prediction is that the patient will survive.
This article assumes you have intermediate or better program- ming skill with C#, but doesn’t assume you know anything about the ML.NET library. The complete code and data for the demo program are presented in this article and are also available in the accompanying file download.
The Demo Program
To create the demo program, I launched Visual Studio 2017. The ML.NET library will work with either the free Community Edition or any of the commercial editions of Visual Studio 2017. The ML.NET documentation doesn’t explicitly state that Visual Studio 2017 is required, but I couldn’t get the demo program to work with
Visual Studio 2015. I created a new C# console application project and named it Kidney. The ML.NET library will work with either a classic .NET or a .NET Core application type.
After the template code loaded, I right-clicked on file Program.cs in the Solution Explorer window and renamed the file to Kidney- Program.cs and I allowed Visual Studio to automatically rename class Program for me. Next, in the Solution Explorer window, I right-clicked on the Kidney project and selected the Manage NuGet Packages option. In the NuGet window, I selected the Browse tab and then entered “ML.NET” in the Search field. The ML.NET library is housed in the Microsoft.ML package. I selected that package and clicked the Install button. After a few seconds Visual Studio responded with a “successfully installed Microsoft.ML 0.3.0 to Kidney” message.
At this point I did a Build | Rebuild Solution and got a “supports only x64 architectures” error message. In the Solution Explorer window, I right-clicked on the Kidney project, and selected the Properties entry. In the Properties window, I selected the Build tab on the left, and then changed the Platform Target entry from “Any CPU” to “x64.” I also made sure I was targeting the 4.7 version of the .NET Framework. With earlier versions I got an error related to one of the math library dependencies and had to manually edit
Code download available at msdn.com/magazine/1118magcode.
48 msdn magazine

