Introduction: Why C# for Machine Learning?
The world of machine learning (ML) is traditionally associated with Python. However, the C# ecosystem and the .NET platform offer powerful alternatives, especially for developers who already work in this environment. If you are familiar with C# and want to apply your skills in ML, this article is for you.
We will review the key libraries, the strengths and weaknesses of C# in the context of ML, and provide practical code examples. You will learn when C# might be the best choice and when you should turn to Python.
Key C# Libraries for Machine Learning
The .NET ecosystem is actively evolving towards ML. Here are three main tools that every beginner should know.
1. ML.NET — Native Library from Microsoft
ML.NET is a cross-platform, open-source, and free machine learning library for .NET. It allows you to add ML to applications without leaving the C# ecosystem.
Key Features:
- Classification (e.g., detecting spam in emails)
- Regression (price prediction)
- Clustering (customer segmentation)
- Ranking and recommendations
- Anomaly detection
- Image processing (transfer learning)
Example: A simple text sentiment classification model.
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; }}
// Training codevar context = new MLContext();var data = context.Data.LoadFromTextFile<SentimentData>("data.csv", separatorChar: ',');var pipeline = context.Transforms.Text.FeaturizeText("Features", "Text") .Append(context.BinaryClassification.Trainers.SdcaLogisticRegression());var model = pipeline.Fit(data);var predictionEngine = context.Model.CreatePredictionEngine<SentimentData, SentimentPrediction>(model);var prediction = predictionEngine.Predict(new SentimentData { Text = "C# for ML is great!" });Console.WriteLine($"Sentiment: {(prediction.PredictedLabel ? "Positive" : "Negative")}");2. TensorFlow.NET — Bridge to TensorFlow
TensorFlow.NET (TF.NET) provides bindings to the popular Google TensorFlow framework. This allows you to use all the capabilities of TensorFlow (neural networks, deep learning) from C#.
Advantages:
- Access to pre-trained models (e.g., for image recognition)
- Possibility of fine-tuning
- High performance on GPU
Example: Loading a pre-trained model for image classification.
using Tensorflow;using static Tensorflow.Binding;
// Loading the Inception model (assuming model files are in the folder)var graph = new Graph().as_default();graph.Import("inception_v3.pb");
var session = new Session(graph);var tensor = ImageUtil.CreateTensorFromImageFile("cat.jpg");var output = session.run(outputs: graph.OperationByName("output"), inputs: graph.OperationByName("input"), inputTensors: new[] { tensor });Console.WriteLine($"Predicted class: {output[0].argmax()}");3. Accord.NET and SciSharp Stack
Accord.NET is an older but still useful library that provides a wide range of algorithms (SVM, neural networks, statistics). SciSharp is a community porting popular Python libraries (NumPy, Pandas, Scikit-learn) to C#.
For a beginner developer, we recommend starting with ML.NET, as it is most integrated into the .NET ecosystem and has excellent