C# for Machine Learning: Capabilities, Limitations, and Code Examples

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

Introduction: C# in the World of Machine Learning

The world of machine learning (ML) is strongly associated with Python: TensorFlow, PyTorch, scikit-learn — these are the main tools of a data scientist. However, the .NET ecosystem and the C# language offer an alternative path. Microsoft's ML.NET platform allows you to integrate ML directly into C# business applications without rewriting code in Python. But how practical is this? In this article, we will examine the real capabilities, limitations, and write working C# code for machine learning.

On the website pythonlib.ru, we often talk about Python, but today we will look at the task from a different perspective. C# is a strictly typed, high-performance language, and its use for ML can be justified in corporate environments where there is already legacy .NET code.



Key Capabilities of ML.NET: What Can C# Do?

ML.NET is a cross-platform, open-source library. It supports classic algorithms: regression, classification, clustering, as well as deep learning through integration with TensorFlow and ONNX.



1. Text Classification (Sentiment Analysis)

The most popular example is sentiment analysis. ML.NET allows you to train a model in C# without calling external scripts.

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

// 1. Define data schemapublic class SentimentData{ public string Text { get; set; } public bool Label { get; set; }}

public class SentimentPrediction{ [ColumnName("PredictedLabel")] public bool Prediction { get; set; } public float Probability { get; set; }}

// 2. Load data and train the modelvar context = new MLContext();IDataView data = context.Data.LoadFromTextFile<SentimentData>("data.tsv", separatorChar: '\\t');

var pipeline = context.Transforms.Text.FeaturizeText("Features", nameof(SentimentData.Text)) .Append(context.BinaryClassification.Trainers.SdcaLogisticRegression());

var model = pipeline.Fit(data);

// 3. Make a predictionvar predictionEngine = context.Model.CreatePredictionEngine<SentimentData, SentimentPrediction>(model);var result = predictionEngine.Predict(new SentimentData { Text = "C# is great for ML!" });Console.WriteLine($"Sentiment: {(result.Prediction ? "Positive" : "Negative")}");

This code is completely self-contained. You don't need Python, you don't need REST requests to external services. Everything works within a single .NET process.



2. Integration with TensorFlow and ONNX

If you need a neural network (for example, for image recognition), ML.NET can load a pre-trained TensorFlow or ONNX model and use it for inference.

var pipeline = context.Transforms.LoadImages("Image", folder, "ImagePath")    .Append(context.Transforms.ResizeImages("Image", 224, 224))    .Append(context.Transforms.ExtractPixels("Input", "Image"))    .Append(modelTensorFlow);

var prediction = pipeline.Fit(data).CreatePredictionEngine<ImageData, ImagePrediction>(data);var result = prediction.Predict(new ImageData { ImagePath = "cat.jpg" });

An important nuance: training a complex neural network in C# is currently impossible — you need to use Python for that. But loading and using a ready-made model is a 100% working scenario.



Limitations of C# for ML: Why Is Python Still Better?

Despite all the advantages, C# loses to Python in several key aspects.



1. Community and Library Size

The number of NuGet packages for ML is hundreds of times smaller than on PyPI. If you need something non-standard (for example, a new algorithm from a 2024 paper), it is most likely only available in Python. ML.NET is updated once a quarter, whereas scikit-learn and PyTorch are updated almost continuously.



2. Model Training

ML.NET handles tabular data tasks (regression, classification) well. But for

Blogs

Book Recommendations