MSDN Community

Your hub for .NET development and innovation

ML.NET Overview

ML.NET is a free, cross-platform, and open-source machine learning framework for .NET developers. It allows you to build custom ML models using C# and F# and then integrate them into your existing .NET applications without requiring prior ML expertise.

What is ML.NET?

ML.NET provides a unified API that covers the entire ML lifecycle, from data loading and preparation to model training, evaluation, and deployment. It's built on top of powerful ML libraries like TensorFlow and ONNX, enabling you to leverage cutting-edge algorithms and pre-trained models.

Key Features

  • Easy Integration: Seamlessly integrate ML capabilities into your .NET applications.
  • Cross-Platform: Works on Windows, macOS, and Linux.
  • Extensible: Supports custom ML algorithms and integration with other ML frameworks.
  • Model Builder: A visual UI tool in Visual Studio to quickly train and consume ML models.
  • AutoML: Automated machine learning to find the best model for your data with minimal configuration.
  • Pre-trained Models: Utilize and adapt existing models for common tasks like image classification and sentiment analysis.

Common Use Cases

ML.NET is versatile and can be applied to a wide range of scenarios:

  • Classification: Email spam detection, customer churn prediction.
  • Regression: Sales forecasting, house price prediction.
  • Clustering: Customer segmentation, anomaly detection.
  • Recommendation Systems: Product recommendations, content suggestions.
  • Image and Text Analysis: Image recognition, sentiment analysis, object detection.

Getting Started with ML.NET

The easiest way to get started is by using the Model Builder in Visual Studio. This visual tool guides you through the process:

  1. Right-click on your .NET project in Solution Explorer and select "Add" > "Machine Learning Model...".
  2. Choose a scenario (e.g., "Value prediction", "Image classification").
  3. Select your data source.
  4. Let Model Builder train the best model.
  5. It will automatically generate code to load the model and make predictions.

For programmatic control, you can use the ML.NET SDK directly:


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

// Define input data schema
public class InputData
{
    [LoadColumn(0)]
    public float Feature1 { get; set; }

    [LoadColumn(1)]
    public float Feature2 { get; set; }

    [LoadColumn(2)]
    public float Label { get; set; }
}

// Define prediction output schema
public class OutputPrediction
{
    [ColumnName("Score")]
    public float Score { get; set; }
}

// Main ML.NET logic
var mlContext = new MLContext();

// Load data (assuming you have a file named "data.tsv")
var trainingDataView = mlContext.Data.LoadFromTextFile("data.tsv", separator: '\t', hasHeader: true);

// Define the training pipeline
var pipeline = mlContext.Transforms.Concatenate("Features", "Feature1", "Feature2")
    .Append(mlContext.Regression.Trainers.Sdca(labelColumnName: "Label", featureColumnName: "Features"));

// Train the model
var model = pipeline.Fit(trainingDataView);

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

// Make a prediction
var sampleData = new InputData { Feature1 = 1.5f, Feature2 = 2.0f };
var prediction = predictionEngine.Predict(sampleData);

Console.WriteLine($"Predicted value: {prediction.Score}");
                

Learn More

Explore the following resources to deepen your understanding of ML.NET: