Introduction: Can C# Compete with Python in ML?
The world of machine learning (ML) has long been associated exclusively with Python. A vast number of libraries (scikit-learn, PyTorch, TensorFlow), an active community, and an abundance of educational materials have made Python the undisputed leader. However, developers working with C# and the .NET platform often wonder: can I use a familiar tool for ML tasks? The answer is yes, you can, but with certain caveats.
In this article, we will look at what tools the .NET ecosystem provides for machine learning, what their strengths are, and where C# still lags behind Python. The article will be useful both for .NET developers wanting to enter the world of ML and for data scientists considering alternatives.
ML.NET: Native Library from Microsoft
The main tool for machine learning in C# is ML.NET. It is a cross-platform, open-source library developed by Microsoft specifically for the .NET ecosystem. It allows you to embed ML models directly into C# applications without needing to switch to Python.
Key Features of ML.NET
- Training and Inference: ML.NET supports both training models from scratch and loading pre-trained models.
- Common Tasks: Classification, regression, clustering, ranking, anomaly detection, text sentiment analysis.
- AutoML: A built-in mechanism for automatically selecting the best algorithm and hyperparameters (Automated Machine Learning).
- ONNX Integration: Support for the Open Neural Network Exchange (ONNX) format for importing models from PyTorch or TensorFlow.
Example: Price Prediction (Regression) with ML.NET
Let's look at a simple example of training a model to predict housing prices based on area. First, define the data class:
public class HousingData{ public float Size { get; set; } public float Price { get; set; }}
public class PricePrediction{ [ColumnName("Score")] public float Price { get; set; }}Now let's write the training code:
using Microsoft.ML;
var context = new MLContext();var data = context.Data.LoadFromTextFile<HousingData>("housing.csv", separatorChar: ',');
var pipeline = context.Transforms.Concatenate("Features", new[] { "Size" }) .Append(context.Regression.Trainers.Sdca(labelColumnName: "Price"));
var model = pipeline.Fit(data);
var predictionEngine = context.Model.CreatePredictionEngine<HousingData, PricePrediction>(model);var prediction = predictionEngine.Predict(new HousingData { Size = 75f });
Console.WriteLine($"Predicted price: {prediction.Price:F2}");As you can see, the API is intuitive for a .NET developer. However, it is important to understand: ML.NET does not support deep learning (neural networks) as flexibly as Keras or PyTorch.
TensorFlow.NET and the SciSharp Stack
For deep learning tasks in C#, there is the TensorFlow.NET (TF.NET) project. This is a .NET binding to the original TensorFlow, allowing you to create, train, and run neural networks in C#. Together with the SciSharp libraries (Keras.NET, NumSharp, Pandas.NET), it forms an analogue of the Python stack for .NET.
Example: Simple Neural Network with TensorFlow.NET
using Tensorflow;using static Tensorflow.Binding;using NumSharp;
// Create a simple Sequential modelvar model = keras.Sequential();model.add(keras.layers.Dense(128, activation: "relu", input_shape: new Shape(784)));model.add(keras.layers.Dense(10, activation: "softmax"));
// Compilationmodel.compile(optimizer: "adam", loss: "categorical_crossentropy", metrics: new[] { "accuracy" });
// Load MNIST data (via NumSharp)var (x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data();x