A clean, production-style example of building a regression model in pure C# with ML.NET and the FastTree gradient-boosted-trees trainer.
It predicts house prices from a handful of property features. The data is 100% synthetic and generated at runtime — there is no proprietary, scraped, or personal data anywhere in this repository.
This repository uses sample/mock/generated data only.
It runs an end-to-end ML workflow with one command:
- Generates a deterministic synthetic housing dataset (or loads your CSV).
- Splits it into train / test sets.
- Trains a FastTree regression model.
- Evaluates it on held-out data (R², MAE, RMSE).
- Predicts prices for a few example houses.
info: ... Training FastTree regressor (trees=200, leaves=32, learningRate=0.2).
info: ... Regression evaluation metrics
R^2 (coeff. of determination) : 0.9721
Mean Absolute Error (MAE) : 18.4195
Root Mean Squared Error (RMSE) : 24.7898
info: ... Sample predictions on hand-crafted observations:
120 m^2, 3 bd, 2 ba, age 10, 5 km out, 1 garage => predicted 277.6k
250 m^2, 5 bd, 3 ba, age 2, 2 km out, 2 garage => predicted 581.7k
ML.NET lets you train and ship machine-learning models without leaving the .NET ecosystem — no Python interop, no separate model-serving stack. The same strongly-typed, testable, dependency-injected code you already write powers the model. That means models can live inside existing ASP.NET Core services, Azure Functions, or desktop apps with first-class tooling and deployment.
The project deliberately separates concerns so each stage is independently testable and swappable (Dependency Inversion + Single Responsibility):
Program.cs thin composition root (DI + run)
└── Pipeline/
└── RegressionWorkflow orchestrates load → split → train → evaluate → predict
├── Data/ IHousingDataSource: Synthetic + CSV implementations
├── Training/ IRegressionModelTrainer: FastTree pipeline
├── Evaluation/ IModelEvaluator: metrics report
├── Prediction/ IHousePricePredictor: single-row scoring
├── Configuration/ TrainerOptions: hyper-parameters & seed
└── Models/ input/output schema + column names (no magic strings)
flowchart LR
A[IHousingDataSource] -->|IDataView| B[TrainTestSplit]
B -->|TrainSet| C[FastTree Trainer]
C -->|ITransformer| D[Model Evaluator]
B -->|TestSet| D
C -->|ITransformer| E[Price Predictor]
D --> F[RegressionMetricsReport]
Requires the .NET 8 SDK.
git clone <repo-url>
cd dotnet-mlnet-fasttree-regression
dotnet build
dotnet test # runs the unit + pipeline tests
dotnet run --project src/MlNet.FastTree.RegressionTraining happens automatically when you run the app. Tune the model by editing
TrainerOptions (number of trees, leaves, learning rate, sample size, seed).
Because the seed is fixed, every run is reproducible.
IHousePricePredictor wraps an ML.NET PredictionEngine. See
RegressionWorkflow.DemonstratePredictions for usage. For a web service, swap
the engine for PredictionEnginePool (thread-safe).
Provide a CSV whose columns match data/sample-housing.csv
(AreaSquareMeters, Bedrooms, Bathrooms, HouseAgeYears, DistanceToCityCenterKm, GarageSpaces, Price):
dotnet run --project src/MlNet.FastTree.Regression -- ./data/sample-housing.csvWhen a path is supplied, the app uses CsvHousingDataSource instead of the
synthetic generator — no code changes required.
- ML.NET training, evaluation, and prediction in idiomatic C#
- Clean architecture with interfaces, DI, and a thin entry point
- Deterministic, testable data generation
- Unit + pipeline tests, CI build, and reproducible package restore
- Synthetic data follows a known formula, so metrics are optimistic compared to messy real-world data.
- Single-model, single-run; no cross-validation or hyper-parameter search.
PredictionEngineis not thread-safe (fine for a console demo).
- Persist/load the trained model (
mlContext.Model.Save). - Add cross-validation and an AutoML comparison across trainers.
- Add feature importance (PFI) reporting.
- Expose predictions via a minimal ASP.NET Core API using
PredictionEnginePool.
dotnet mlnet machine-learning regression csharp fasttree data-science