Using Visual Studio & Azure?
Take a quick, 5 minute survey to help us make Visual Studio and its Azure-related experiences better.

What is ML.NET?

ML.NET is an open source machine learning framework, created by Microsoft, for the .NET developer platform. ML.NET is cross platform and runs on macOS, Linux and Windows.

ML.NET

ML.NET is a free, open source, and cross platform machine learning framework for the .NET developer platform.

ML.NET allows you to train, build, and ship custom machine learning models using C# or F# for a variety of ML scenarios. ML.NET includes features like automated machine learning (AutoML) and tools like ML.NET CLI and ML.NET Model Builder, which make integrating machine learning into your applications even easier.

ML.NET step-by-step

ML.NET follows the same basic steps for nearly every scenario; it combines data loading, transformations, and model training to make it easy for you to create machine learning models.

Create ML.NET context

MLContext is the starting point for all ML.NET operations. The MLContext is used for all aspects of creating and consuming an ML.NET model. It is similar conceptually to DbContext in Entity Framework.

var mlContext = new MLContext();

Once you have an instance of an MLContext, you can load and transform data, choose the best algorithm for your machine learning task, train your model. Once trained, you can test your model for accuracy, save it to disk, and it to make predictions. An MLContext can be initialized from an model that was previously saved to disk.

Explore MLContext

Load data

Machine learning uses known data (e.g. training data) to find patterns in order to make predictions on new, unknown data.

The inputs for machine learning are called Features, which are the attributes used to make predictions. The output of machine learning is called the Label, which is the actual prediction.

Data in ML.NET is represented as an IDataView, which is a flexible, efficient way of describing tabular data (e.g. rows and columns). IDataView objects can contain numbers, text, booleans, vectors, and more. You can load data from files or from real-time streaming sources to an IDataView.

LoadFromTextFile allows you to load data from TXT, CSV, TSV, and other file formats.

IDataView trainingData = mlContext.Data
    .LoadFromTextFile<SentimentInput>(dataPath, separatorChar: ',', hasHeader: true);

LoadFromEnumerable enables loading from in-memory collections, JSON/XML, relational and non-relational databases (e.g. SQL, CosmosDB, MongoDB), and many other data sources.

IDataView trainingData = mlContext.Data
    .LoadFromEnumerable<SentimentInput>(inMemoryCollection);

Learn more about loading data

Transform data

In most cases, the data that you have available isn't suitable to be used directly to train a machine learning model. The raw data needs to be pre-processed using data transformations.

Transformers take data, do some work on it, and return new, transformed data.

The FeaturizeText method takes a piece of text and converts it to a series of numbers that can be used for machine learning.

There are built-in set of data transforms for replacing missing values, data conversion, featurizing text, and more.

// Convert sentiment text into numeric features
IEstimator<ITransformer> dataTransformPipeline = mlContext.Transforms.Text
    .FeaturizeText("Features", "SentimentText");

Explore data transforms

Choose algorithm

When using machine learning and ML.NET, you must choose a machine learning task that goes along with your scenario. ML.NET offers over 30 algorithms (or trainers) for a variety of ML tasks:

ML Task Algorithms
Binary classification (e.g. sentiment analysis) AveragedPerceptronTrainer, SdcaLogisticRegressionBinaryTrainer
Multi-class classification (e.g. topic categorization) LightGbmMulticlassTrainer, OneVersusAllTrainer
Regression (e.g. price prediction) LbfgsPoissonRegressionTrainer, FastTreeRegressionTrainer
Clustering (e.g. customer segmentation) KMeansTrainer
Anomaly Detection (e.g. shampoo sales spike detection) RandomizedPcaTrainer
Recommendation (e.g. movie recommender) MatrixFactorizationTrainer
Ranking (e.g. search results) LightGbmRankingTrainer, FastTreeRankingTrainer
IEstimator<ITransformer> trainer = mlContext.BinaryClassification.Trainers
    .AveragedPerceptron(labelColumnName: "Sentiment", featureColumnName: "Features"));

IEstimator<ITransformer> trainingPipeline = dataTransformPipeline.Append(trainer);

Explore algorithms

Train model

The data transformations and algorithms you have specified are not executed until you call the Fit() method (because of ML.NET's lazy loading approach). This is when model training happens.

An estimator takes in data, learns from the data, and creates a transformer. In the case of model training, the training data is the input, and the trained model is the output; the trained model is thus a transformer that turns the input Features from new data into output predictions.

ITransformer model = pipeline.Fit(trainingData);

Learn more about model training

Evaluate model

ML.NET offers evaluators that assess the performance of your model on a variety of metrics:

  • Accuracy
  • Area under the curve (AUC)
  • R-Squared
  • Root Mean Squared Error (RMSE)
// Make predictions on test data
IDataView predictions = model.Transform(testDataView);

// Evaluate model and return metrics
var metrics = mlContext.BinaryClassification
    .Evaluate(predictions, labelColumnName: "Sentiment"); 

// Print out accuracy metric
Console.WriteLine("Accuracy" + metrics.Accuracy);

Explore evaluation metrics

Deploy & consume model

You can save your trained model as a binary file that is then integrated into your .NET applications.

mlContext.Model.Save(model, trainingData, "model.zip");

Once you have saved the trained model, you can load the model in your other .NET applications.

MLContext mlContext = new MLContext();
DataViewSchema predictionPipelineSchema;
ITransformer trainedModel = mlContext.Model.Load("model.zip", out predictionPipelineSchema);

You can then used the loaded model to start making predictions. You can use the Prediction Engine, which is a convenience API used for making single predictions, or the Transform method, which is used for making batch predictions.

var predEngine = mlContext.Model.CreatePredictionEngine(model); 
SentimentInput sampleComment = new SentimentInput{ SentimentText = "This is very rude!" }; 
SentimentOutput result = predEngine.Predict(sampleComment);
Console.WriteLine(result.Prediction);

Learn how use a trained ML model

ML.NET FAQ

What is ML.NET used for?

ML.NET is a machine learning framework for .NET developers; you can use ML.NET to integrate custom machine learning models into your .NET applications. You can use ML.NET for many scenarios, such as sentiment analysis, price prediction, product recommendation, sales forecasting, image classification, object detection, and more! Check out our GitHub samples repo for more examples of what you can do with ML.NET.

What is the difference between Artificial Intelligence and Machine Learning?

Artificial Intelligence (AI) is a branch of computing that involves training computers to do things that normally require human intelligence. Machine Learning (ML) is a subset of AI which involves computers learning from and finding patterns in data in order to then be able to make predictions on new data by themselves.

Why do I need ML.NET and how is ML.NET different than Microsoft's other AI/ML offerings?

Microsoft offers many AI and ML products and services, so here is a breakdown of the differences between them:

  • ML.NET: Build custom machine learning solutions and integrate them into your .NET applications.
  • Azure Cognitive Services: A cloud services that provides pre-built AI and machine learning models to add to your applications. Includes a set of APIs to use a variety of models for natural communication methods with vision and speech.
  • Azure Machine Learning: Comprehensive environment in the cloud to host your end-to-end ML model lifecycle including model training, versioning, deployment, and release management at cloud scale.

What types of apps can I deploy ML.NET models to?

You can use ML.NET with almost any .NET app, including web apps and services, Microservices/Containers, desktop apps (WPF and WinForms), Azure Functions and any Azure server-side app type, and console apps.

What frameworks can I use with ML.NET?

ML.NET is supported on both .NET Core (version 2.0 and above) and .NET Framework (version 4.6.1 and above).

What process architectures are supported by ML.NET?

ML.NET is currently supported on x64 and x86 processes.

What OS can I use with ML.NET?

ML.NET is cross platform, so it is supported on MacOS, Linux, and Windows.

Is ML.NET free?

Yes! Like the rest of the .NET platform, ML.NET is 100% free. For more information, see .NET is free. ML.NET is licensed under the MIT license.

What is the best programming language for ML.NET?

ML.NET is currently supported for both C# and F#.

How can I learn ML.NET?

You can get started with ML.NET with this tutorial, or you can check out our ML.NET docs to learn more.

How can I install ML.NET?

You can add ML.NET to any .NET project by adding the Microsoft.ML NuGet package, or you can get started with ML.NET Model Builder (Windows) or the ML.NET CLI (Windows, MacOS, and Linux).

Ready to Get Started?

Our step-by-step tutorial will help you get ML.NET running on your computer.

Supported on Windows, Linux, and macOS

Get Started