Machine Learning in C# (.NET): Overview of Key Libraries
Although Python is traditionally considered the primary language for machine learning (ML), the C# (.NET) ecosystem offers powerful and mature solutions for integrating ML models into enterprise applications, web services, and desktop programs. In this article, we will review the leading libraries available for C# developers, their capabilities, usage examples, and application scenarios.
1. ML.NET — Official Library from Microsoft
ML.NET is a cross-platform, open-source, and free machine learning library developed by Microsoft. It allows you to create, train, and use ML models directly in .NET applications without needing to switch to Python or other languages.
Key Features:
- Support for classic algorithms: regression, classification, clustering, ranking.
- Deep learning through integration with TensorFlow and ONNX.
- Automated machine learning (AutoML) for optimal model selection.
- Built-in data processing pipelines (transformations, normalization).
- Integration with Visual Studio (Model Builder extension).
Example: Binary Classification (Sentiment Analysis)
using Microsoft.ML;using Microsoft.ML.Data;
public class SentimentData{ public string SentimentText { get; set; } public bool Label { get; set; }}
public class SentimentPrediction{ public bool PredictedLabel { get; set; } public float Probability { get; set; }}
var context = new MLContext();// Load dataIDataView data = context.Data.LoadFromTextFile<SentimentData>("data.tsv", separatorChar: '\\t');// Pipeline: text featurization + trainingvar pipeline = context.Transforms.Text.FeaturizeText("Features", nameof(SentimentData.SentimentText)) .Append(context.BinaryClassification.Trainers.SdcaLogisticRegression());// Trainvar model = pipeline.Fit(data);// Usevar engine = context.Model.CreatePredictionEngine<SentimentData, SentimentPrediction>(model);var prediction = engine.Predict(new SentimentData { SentimentText = "This is great!" });Console.WriteLine($"Predicted: {prediction.PredictedLabel}, Probability: {prediction.Probability}");2. Accord.NET — Scientific Computing Framework
Accord.NET is a powerful library that provides not only ML algorithms but also tools for signal processing, statistics, computer vision, and neural networks. It is considered one of the most comprehensive solutions for .NET.
Key Features:
- Wide selection of algorithms: SVM, Random Forest, K-Means, Naive Bayes.
- Deep neural networks (Accord.Neuro).
- Image and video processing (Accord.Imaging).
- Statistical tests and distributions.
Example: Training an SVM Classifier
using Accord.MachineLearning.VectorMachines;using Accord.MachineLearning.VectorMachines.Learning;using Accord.Statistics.Kernels;
// Training data (features: two numbers)double[][] inputs = { new double[] { 0, 0 }, new double[] { 1, 1 }, new double[] { 1, 0 }, new double[] { 0, 1 }};int[] outputs = { 0, 1, 1, 0 };
// Create and train SVM with Gaussian kernelvar teacher = new SequentialMinimalOptimization<Gaussian>(){ Complexity = 100};var svm = teacher.Learn(inputs, outputs);
// Predictionint prediction = svm.Decide(new double[] { 0.8, 0.2 });Console.WriteLine($"Predicted class: {prediction}");3. TensorFlow.NET — .NET Bindings for TensorFlow
TensorFlow.NET (SciSharp) is the official low-level binding for TensorFlow in .NET. The library allows you to perform tensor operations, build and train neural networks, and load