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.
* * * * *
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:
Now let’s create project for our ML.NET by typing “dotnet new console -o MLtest” in command line:
As you can see above basically 2 files have been created and “obj” directory.
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”:
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”:
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:
Once clicked on project file the previously installed C# extension wants to restore dependencies so I let it do it:
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:
You can also run the program from terminal window, by typing “dotnet run”:
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!