Machine Learning in C# (.NET): Overview of ML.NET, Accord.NET, TensorFlow.NET Libraries

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Machine Learning in C# (.NET): Overview of Key Libraries

Although Python is traditionally considered the primary language for machine learning, the C# (.NET) ecosystem also offers powerful and mature tools for building ML models. If your infrastructure is built on the Microsoft stack, or you prefer static typing and high performance, libraries for .NET will help you implement artificial intelligence without changing your technology stack. In this article, we will look at three main libraries: ML.NET, Accord.NET, and TensorFlow.NET, and also provide practical code examples.



1. ML.NET — Official Library from Microsoft

ML.NET is a cross-platform, open-source machine learning library created specifically for .NET developers. It allows you to integrate ML into applications without needing to learn Python or R. ML.NET supports classification, regression, clustering, ranking, anomaly detection, and even deep learning through integration with ONNX and TensorFlow.



Key Features:

  • Built-in transformers for data preparation (normalization, encoding, concatenation).
  • Automatic model selection (AutoML) via AutoML.NET.
  • Model export to ONNX format for further use on other platforms.
  • Support for training on large data through IDataView.


Example: Binary Classification of Reviews (Sentiment Analysis)

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; }}

// Load data and trainvar 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);

// Predictionvar predictionEngine = context.Model.CreatePredictionEngine<SentimentData, SentimentPrediction>(model);var prediction = predictionEngine.Predict(new SentimentData { Text = "This product is amazing!" });Console.WriteLine($"Positive: {prediction.PredictedLabel}, Confidence: {prediction.Probability:P}");

This code shows how to train a model on a CSV file and make a prediction in just a few lines. ML.NET automatically handles data types and creates a pipeline.



2. Accord.NET — Classic Framework for Scientific Computing

Accord.NET is a set of libraries for signal processing, statistics, and machine learning. It includes over 40 algorithms: from SVM and neural networks to hidden Markov models. The library has been actively used in academia and industry since 2010. Unlike ML.NET, Accord.NET provides more low-level control over algorithms.



Key Features:

  • Wide selection of classifiers: KNN, NaiveBayes, RandomForest, SVM.
  • Built-in preprocessing methods: PCA, LDA, normalization.
  • Support for neural networks (Deep Learning Toolkit).
  • Integration with Math.NET for complex mathematical operations.


Example: Fisher's Iris Classification using KNN

using Accord.MachineLearning;using Accord.MachineLearning.VectorMachines;using Accord.Statistics.Analysis;

// Load data (classic Iris dataset)double[][] inputs = new double[][]{ new double[] { 5.1, 3.5, 1.4, 0.2 }, new double[] { 4.9, 3.0, 1.4, 0.2 }, // ... rest

Recommendations