ML.NET Getting Started Guide

Jane Doe Avatar

Jane Doe

Senior Software Engineer specializing in AI and .NET development.

Introduction to ML.NET

ML.NET is a free, cross-platform, open-source machine learning framework built for .NET developers. It allows you to build custom machine learning models using C# and F# without needing prior ML expertise. This guide will walk you through the essential steps to get started with ML.NET.

Prerequisites

  • Visual Studio 2022 or later with the ".NET desktop development" workload installed.
  • .NET 6 SDK or later.
  • Basic understanding of C# programming.

Setting Up Your Environment

First, ensure you have the necessary tools installed. You can download the latest .NET SDK from the official .NET website.

Creating Your First ML.NET Project

Let's create a simple console application:

dotnet new console -o MyMLApp
cd MyMLApp

Next, add the ML.NET NuGet package to your project:

dotnet add package Microsoft.ML

Understanding the ML.NET Workflow

The typical ML.NET workflow involves several key steps:

  1. Data Loading: Load your dataset into an IDataView.
  2. Data Preprocessing: Clean and transform your data.
  3. Model Training: Choose an algorithm and train your model.
  4. Model Evaluation: Assess the performance of your trained model.
  5. Model Prediction: Use the trained model to make predictions on new data.

Example: Sentiment Analysis

We'll create a simple sentiment analysis model to predict whether a piece of text is positive or negative.

1. Define Data Structures

Create classes to represent your input data and prediction output:

public class SentimentData
{
    [LoadColumn(0)]
    public string SentimentText;

    [LoadColumn(1), ColumnName("Label")]
    public bool Sentiment;
}

public class SentimentPrediction
{
    [ColumnName("PredictedLabel")]
    public bool Prediction;

    public float Score;
    public float Probability;
}

2. Load Data and Define Pipeline

In your Program.cs file:

using Microsoft.ML;
using Microsoft.ML.Data;

// ... (SentimentData and SentimentPrediction classes from above)

// Create MLContext
var mlContext = new MLContext();

// Load your dataset (replace with your actual data path)
// For demonstration, we'll use a small in-memory dataset.
var samples = new[] {
    new SentimentData { SentimentText = "This is a great product!", Sentiment = true },
    new SentimentData { SentimentText = "I am very disappointed.", Sentiment = false },
    new SentimentData { SentimentText = "It works as expected.", Sentiment = true },
    new SentimentData { SentimentText = "Terrible experience.", Sentiment = false }
};

IDataView trainingData = mlContext.Data.LoadFromEnumerable(samples);

// Define the training pipeline
var pipeline = mlContext.Transforms.Text.FeaturizeText(outputColumnName: "Features", inputColumnName: nameof(SentimentData.SentimentText))
    .Append(mlContext.BinaryClassification.Trainers.SdcaLogisticRegression(labelColumnName: "Label", featureColumnName: "Features"));

// Train the model
Console.WriteLine("Training the model...");
var model = pipeline.Fit(trainingData);
Console.WriteLine("Model trained successfully!");

3. Make Predictions

Now, let's use the trained model to predict sentiment:

// Create a prediction engine
var predictionEngine = mlContext.Model.CreatePredictionEngine(model);

// Make a prediction
var sampleStatement = new SentimentData { SentimentText = "I love this new feature!" };
var prediction = predictionEngine.Predict(sampleStatement);

Console.WriteLine($"\nInput: \"{sampleStatement.SentimentText}\"");
Console.WriteLine($"Prediction: {(prediction.Prediction ? "Positive" : "Negative")}");
Console.WriteLine($"Confidence: {prediction.Probability:P2}");

Further Steps

  • Explore different ML.NET trainers and transforms.
  • Learn about data augmentation and feature engineering.
  • Integrate ML.NET models into your ASP.NET Core, WPF, or UWP applications.
  • Discover AutoML for automated model building.
ML.NET .NET Core Machine Learning AI Tutorial Getting Started