Exploring Machine Learning In .NET Core With ML.NET

Machine learning is a rapidly growing field that enables computers to learn from data, recognize patterns, and make predictions. With the increasing demand for intelligent applications, it has become crucial for developers to explore machine learning frameworks and libraries to build smarter applications. One such framework is ML.NET, an open-source, cross-platform machine learning framework developed by Microsoft.

In this article, we will explore how to start with ML.NET in .NET Core, step-by-step.

Machine Learning In .NET Core With ML.NET

Step 1. Install Visual Studio and .NET Core

Before we get started, we need to have Visual Studio and .NET Core installed on our machine. You can download Visual Studio from the official website and .NET Core from the .NET Core website.

Step 2. Create a new .NET Core console application

Once we have Visual Studio and .NET Core installed, let's create a new .NET Core console application. To do this, open Visual Studio and follow the below steps:

  1. Click on "Create a new project"
  2. Select "Console App (.NET Core)" from the list of templates
  3. Enter a name for the project and click on "Create"

Step 3. Add the ML.NET package

Now that we have created a new console application let's add the ML.NET package to the project. To do this, follow the below steps:

  1. Right-click on the project in the Solution Explorer and select "Manage NuGet Packages"
  2. In the "Browse" tab, search for "Microsoft.ML"
  3. Select the "Microsoft.ML" package from the search results and click on "Install"

Step 4. Create a model class

Now that we have added the ML.NET package to the project, let's create a model class. A model class defines the structure of the data we want to use for machine learning. In our example, we will create a model class to predict the price of a house based on its size.

To create a model class, add a new class file to the project and name it "House.cs". In the House class, define the following properties:

public class House {
    [LoadColumn(0)]
    public float Size {
        get;
        set;
    }
    [LoadColumn(1)]
    public float Price {
        get;
        set;
    }
}

In the above code, we have defined two properties, "Size" and "Price", which correspond to the size of the house and its price, respectively. The LoadColumn attribute is used to specify the index of the column in the dataset that corresponds to the property.

Step 5. Load and prepare the data

Now that we have defined the model class, let's load and prepare the data. In our example, we will use a CSV file containing several houses' sizes and prices.

To load the data, add the following code to the Main method:

var mlContext = new MLContext();
var data = mlContext.Data.LoadFromTextFile<House>("houses.csv", separatorChar: ',');
var trainTestSplit = mlContext.Data.TrainTestSplit(data, testFraction: 0.2);
var trainData = trainTestSplit.TrainSet;
var testData = trainTestSplit.TestSet;

In the above code, we first create a new MLContext object, which is used to perform machine learning operations. We then load the data from a CSV file using the LoadFromTextFile method. We then split the data into training and test sets using the TrainTestSplit method.

Step 6. Define the pipeline

Now that we have loaded and prepared the data, let's define the pipeline. A pipeline is a series of operations performed on the data to transform it into a format that a model can use for prediction.

In our example, we will use a linear regression algorithm to create a model that can predict the price of a house based on its size. To define the pipeline, add the following code to the Main method:

var pipeline = mlContext.Transforms.Concatenate("Features", "Size")
    .Append(mlContext.Transforms.NormalizeMinMax("Features"))
    .Append(mlContext.Transforms.Conversion.ConvertType("Label", "Price", DataKind.Single))
    .Append(mlContext.Regression.Trainers.Sdca());

var model = pipeline.Fit(trainData);

In the above code, we first define a pipeline that concatenates the "Size" property to create a new "Features" column. We then normalize the "Features" column using the NormalizeMinMax method. We then convert the "Price" property to a single data type using the ConvertType method. Finally, we use the Sdca trainer to create a linear regression model.

Step 7. Evaluate the model

Now that we have created the model, let's evaluate its performance on the test set. To do this, add the following code to the Main method:

var predictions = model.Transform(testData);
var metrics = mlContext.Regression.Evaluate(predictions, "Label", "Score");
Console.WriteLine($"R^2: {metrics.RSquared}");

In the above code, we first use the Transform method to make predictions on the test set. We then use the Evaluate method to calculate the R^2 score, which measures how well the model fits the data. We then output the R^2 score to the console.

Step 8: Make predictions

Now that we have evaluated the model, let's use it to make predictions on new data. To do this, add the following code to the Main method:

var size = 2000;
var predictionEngine = mlContext.Model.CreatePredictionEngine<House, HousePricePrediction>(model);
var prediction = predictionEngine.Predict(new House { Size = size });
Console.WriteLine($"Predicted price for a {size} sq. ft. house: {prediction.Price}");

In the above code, we first define the house size we want to predict the price for. We then create a prediction engine using the CreatePredictionEngine method, which is used to make predictions on new data. We then use the Predict method to predict a new house with the specified size. Finally, we output the predicted price to the console.

Conclusion

In this article, we explored how to start with ML.NET in .NET Core, step by step. We learned how to create a model class, load and prepare the data, define the pipeline, evaluate the model, and make predictions on new data. ML.NET is a powerful machine learning framework that enables developers to build intelligent applications in .NET Core with ease. By following the steps outlined in this article, you can get started with ML.NET and build your machine learning models quickly.


Similar Articles