How to use Microsoft Machine Learning ML.NET library on Mac with Visual Studio Code

What if I told you, that you can practice machine learning with C# and Visual Studio Code, and that you can run it on all platforms – Mac, Windows, Linux, Azure cloud, AWS cloud, etc. Let’s do it on Mac!

Why on Mac not on PC? Well, this is just a fact of life that I own both PCs and Macs but it’s more pleasant to work on Macs, particularly since macOS 10.14 Mojave has introduced the dark mode. Besides that many people who work in Machine Learning and people who work at startups, just prefer to use Mac than PC. That’s why I wanted to achieve: ability to use Microsoft’s ML.NET on both PC and Mac.

Let’s don’t beat around the bush: most data scientists and machine learning engineers use Python or R and open source ML libraries. However Microsoft offers powerful ML.NET library, also open-sourced where you can write with C# not Python or R, and which can run on Mac, Linux and Windows. It can also be deployed to the cloud (Azure, AWS) so is scalable.

ML.NET was announced just this year in May 2018, and this month, in July 2018 Microsoft has announced version 0.3: .NET Blog: Announcing ML.NET 0.3. What convinced me to try ML.NET was that it has certain Machine Learning learner algorithms that I need in my current research. Furthermore ML.NET uses C# that I know better than Python and ML.NET runs with .NET technology that is native for Microsoft servers but also runs via cross-platform .NET Core on all 3 major platforms: Linux, Mac and Windows.

* * * * *

My purpose was to use freely downloadable components without the need to buy anything.  Microsoft is offering commercial “Visual Studio for Mac Professional” that makes it possible to write Mac apps with C#, but let’s just be honest about it: it’s not free and feels a bit like old Microsoft Office for Mac. There is “Visual Studio for Mac Community Edition” that is fully free, but it is big pain in the backside: you can’t develop Mac apps with it, not even console apps – it’s basically designed only to develop iOS and Android apps. Microsoft has purchased company Xamarin and rebranded their tools to Visual Studio for Mac but obviously Microsoft is too greedy to offer Mac development for free.

I wanted however to run everything with ML.NET fully for free. Here comes Visual Studio Code into play. Visual Studio Code is Microsoft’s free cross-platform text-editor with many many extensions and very popular worldwide as kind-of IDE (Integrated Development Environment), which is very powerful particularly in web development – HTML5 and JavaScript – but also used a lot by Python and many other language users.

* * * * *

Let’s start!

First let’s install Visual Studio Code from https://code.visualstudio.com/download although strictly it’s not needed to edit ML.NET programs – just because it’s such an extremely powerful code editor, and easy to use and because it offers for example this nice C# extension (not official from Microsoft but still good).

Then let’s download .NET Core SDK (software development kit) : https://www.microsoft.com/net/download/macos

The “Core” in name means it’s multiplatform. Once installed type “dotnet –info” and observe:

machine-learning-mac-01

Now let’s create project for our ML.NET  by typing “dotnet new console -o MLtest” in command line:

machine-learning-mac-02

As you can see above basically 2 files have been created and “obj” directory.

machine-learning-mac-03

As you can see above the 2 files are a C# file (source code) and a C# project file (in XML format).

What is missing from the project file is reference to ML.NET Machine Learning library, that can be added by executing command “dotnet add package Microsoft.ML”:

machine-learning-mac-04

As you can see above the reference to Microsoft.ML (ML.NET) has been added now and this library has been added.

Next, it’s time to download some dataset. Choosing proper dataset for exercises is a big problem in machine learning, but to avoid this decision I just pick this data set, that Microsoft picked as example in this article.

So in Safari I just open this data set, and I do “save page as” and I save it to the folder where is my project as “iris.csv”:

machine-learning-mac-05

Now it’s time to extend the initial Program.cs into one where machine learning stuff is done, so let’s open Visual Studio Code and therein do File/Open and open the directory where the files with solution are:

machine-learning-mac-06

Once clicked on project file the previously installed C# extension wants to restore dependencies so I let it do it:

machine-learning-mac-07

So now let’s put sample code from Microsoft’s article to Program.cs (only change the file name from “iris-data.txt” to “iris.csv”) and let’s run it in Visual Studio Code by using menu Debug / Start Debugging:

machine-learning-mac-08

You can also run the program from terminal window, by typing “dotnet run”:

machine-learning-mac-09

As you can see everything works on the Mac: both running of ML.NET training and prediction programs, as well as code editing for free with Visual Studio Code and without the need to buy commercial Visual Studio for Mac Professional.

Since ML.NET is a very powerful technology and it’s working natively in Windows but also supports many platforms, using it in machine learning might be more effective than installing Python or Scala/Spark everywhere. Well, now you can do it with Mac too!

 

 

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s