Machine Learning in C# (.NET): Overview of Libraries for Developers
Although Python remains the king of machine learning, the .NET ecosystem offers mature and powerful tools for integrating ML models into enterprise applications, web services, and desktop solutions. In this article, we will cover three key libraries for C#: ML.NET, Accord.NET, and TensorFlow.NET. You will learn which one to choose for your task and see code examples.
1. ML.NET — Native Library from Microsoft
ML.NET is a cross-platform open-source library developed specifically for .NET. It allows you to create, train, and deploy machine learning models without switching to Python. It supports classification, regression, clustering, ranking, anomaly detection, and even recommendation systems.
Key Features:
- Built-in data transformers (normalization, one-hot encoding, feature extraction from text and images).
- AutoML — automatic algorithm and hyperparameter selection.
- Export models to ONNX for cross-platform use.
- GPU support via TensorFlow and ONNX Runtime.
Example: Binary Text Sentiment Classification
using Microsoft.ML;using Microsoft.ML.Data;
public class SentimentData{ public string Text { get; set; } public bool Label { get; set; }}
public class SentimentPrediction{ public bool PredictedLabel { get; set; } public float Probability { get; set; }}
var context = new MLContext();var data = context.Data.LoadFromTextFile<SentimentData>("data.csv", separatorChar: ',');
var pipeline = context.Transforms.Text.FeaturizeText("Features", nameof(SentimentData.Text)) .Append(context.BinaryClassification.Trainers.SdcaLogisticRegression());
var model = pipeline.Fit(data);var engine = context.Model.CreatePredictionEngine<SentimentData, SentimentPrediction>(model);
var prediction = engine.Predict(new SentimentData { Text = "This product is amazing!" });Console.WriteLine($"Prediction: {prediction.PredictedLabel}, Probability: {prediction.Probability}");When to use: if your application is already on .NET and you need seamless ML integration without external dependencies.
2. Accord.NET — Classic Library for Scientific Computing
Accord.NET is a set of frameworks for signal processing, statistics, and machine learning. It includes over 50 algorithms: from linear regression to deep neural networks. The library was actively used before the advent of ML.NET and is still in demand for tasks requiring fine-tuning.
Key Features:
- Flexible models: SVM, Random Forest, K-Means, Hidden Markov Models.
- Built-in image and audio processing methods (filters, edge detectors, FFT).
- Support for kernel methods and probabilistic models.
- Integration with Math.NET Numerics for advanced mathematical operations.
Example: Training SVM for Iris Classification
using Accord.MachineLearning.VectorMachines;using Accord.MachineLearning.VectorMachines.Learning;using Accord.Statistics.Kernels;
// Loading data (classic Iris dataset)double[][] inputs = // ... feature matrix (4 columns)int[] outputs = // ... class labels (0,1,2)
var teacher = new MulticlassSupportVectorLearning<Gaussian>(){ Learner = (param) => new SequentialMinimalOptimization<Gaussian>() { Complexity = 100, Tolerance = 1e-5 }};
var machine = teacher.Learn(inputs, outputs);int[] predicted = machine.Decide(inputs);
double error = new ZeroOneLoss(outputs).Loss(predicted);Console.WriteLine($"Error: {error:P}");When to
Also in library
JSON Processing in Kotlin: Libraries and Code Examples
Working with Markdown in Go (Golang): Libraries, Parsing, and Conversion
Working with Markdown in C# (.NET): Libraries and Examples