Build A Product Recommender Using C# and ML.NET Machine Learning
In this article I’m going to build a product recommendation service using C#, ML.NET, and NET Core.
ML.NET is Microsoft’s new machine learning library. It can run linear regression, logistic classification, clustering, deep learning, and many other machine learning algorithms.
And NET Core is the Microsoft multi-platform NET Framework that runs on Windows, OS/X, and Linux. It’s the future of cross-platform NET development.
The first thing I need for my product recommendation app is a data file with hundreds of thousands of product purchases. I can use the SNAP Amazon Co-Purchasing Network which was created by crawling the Amazon website in 2003. The dataset is based on the ‘Customers Who Bought This Item Also Bought’ feature of the Amazon website and contains every product combination discovered by the crawler.
The dataset contains a list of 1,234,878 product combinations and looks like this:
It’s a very simple TSV file with only two columns:
- The ID of a purchased product
- The ID of a second product that was often bought by customers who also bought the first product
I will build a machine learning model that reads in each set of Product IDs, and then predicts popular product combinations for every product in the dataset.
Let’s get started. Here’s how to set up a new console project in NET Core:
$ dotnet new console -o Recommender2
$ cd Recommender2
Next, I need to install the ML.NET base package and the recommender extensions:
$ dotnet add package Microsoft.ML
$ dotnet add package Microsoft.ML.Recommender
Now I’m ready to add some classes. I’ll need one to hold a product combination record, and one to hold my model’s predictions.
I will modify the Program.cs file like this: