🏠 VisualStudioTutor.com  ·  C# Tutorial Home  ·  C# Lesson 35 of 40
Lesson 35 of 40 AI / ML Expert ⏱ 35 min

ML.NET & AI Integration in C#

Train and use a simple regression model with ML.NET directly from C#. The lesson focuses on the core machine-learning workflow: data, pipeline, Fit, and Predict.

Part 1: What You Will Learn

Train and use a simple regression model with ML.NET directly from C#. The lesson focuses on the core machine-learning workflow: data, pipeline, Fit, and Predict.

  • Create an `MLContext`.
  • Load strongly typed training rows into an IDataView.
  • Build a feature pipeline and choose a regression trainer.
  • Train with `Fit()` and make a prediction with a prediction engine.

Project setup: Create a .NET 10 Console App and install the stable `Microsoft.ML` package. The current stable major release is ML.NET 5.x; preview packages may be newer.

Part 2: Topic-Specific Working Example

The following example is written specifically for this lesson. Create the project described above, enter the code, run it, and then change some values to observe how the feature behaves.

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

public sealed class HouseData
{
    public float Size { get; set; }
    public float Bedrooms { get; set; }

    [ColumnName("Label")]
    public float Price { get; set; }
}

public sealed class HousePrediction
{
    [ColumnName("Score")]
    public float PredictedPrice { get; set; }
}

MLContext ml = new(seed: 1);

HouseData[] rows =
[
    new() { Size = 900,  Bedrooms = 2, Price = 220000 },
    new() { Size = 1200, Bedrooms = 3, Price = 310000 },
    new() { Size = 1600, Bedrooms = 3, Price = 390000 },
    new() { Size = 2100, Bedrooms = 4, Price = 520000 }
];

IDataView trainingData = ml.Data.LoadFromEnumerable(rows);

var pipeline = ml.Transforms
    .Concatenate("Features",
        nameof(HouseData.Size),
        nameof(HouseData.Bedrooms))
    .Append(ml.Regression.Trainers.Sdca());

ITransformer model = pipeline.Fit(trainingData);

var engine =
    ml.Model.CreatePredictionEngine<HouseData, HousePrediction>(model);

HousePrediction prediction = engine.Predict(
    new HouseData { Size = 1500, Bedrooms = 3 });

Console.WriteLine(
    $"Predicted price: {prediction.PredictedPrice:C0}");

Part 3: How the Code Works

  • `MLContext` is the starting point for ML.NET operations.
  • `IDataView` is ML.NET's data abstraction for training and transformation.
  • `Concatenate` creates the Features vector expected by the regression trainer.
  • `Fit()` trains the model from the supplied rows; `Predict()` applies the trained model to new input.
  • For production systems, evaluate the model on held-out data and avoid using `PredictionEngine` concurrently across threads.

Part 4: Mini Project & Practice

Mini project: add more training rows, split the data into training and test sets, evaluate regression metrics, and predict the price of three new houses.

Tip: Type the code yourself in Visual Studio 2026, run it, then deliberately change one part at a time. The goal is to understand the feature rather than simply copy the finished example.

When you are comfortable with this lesson, continue to Lesson 36.

C# in Visual Studio 2026

📘 This lesson is part of the book C# in Visual Studio 2026 by Dr. Liew Voon Kiong.

View on Amazon Kindle Edition