Skip to content

yashshah7575/dotnet-mlnet-fasttree-regression

Repository files navigation

ML.NET FastTree Regression Starter

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.

build license

What this project does

It runs an end-to-end ML workflow with one command:

  1. Generates a deterministic synthetic housing dataset (or loads your CSV).
  2. Splits it into train / test sets.
  3. Trains a FastTree regression model.
  4. Evaluates it on held-out data (R², MAE, RMSE).
  5. Predicts prices for a few example houses.

Example output

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

Why ML.NET matters for .NET engineers

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.

Architecture

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]
Loading

How to run

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.Regression

How to train

Training 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.

How to predict

IHousePricePredictor wraps an ML.NET PredictionEngine. See RegressionWorkflow.DemonstratePredictions for usage. For a web service, swap the engine for PredictionEnginePool (thread-safe).

How to replace the sample data

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.csv

When a path is supplied, the app uses CsvHousingDataSource instead of the synthetic generator — no code changes required.

What this demonstrates

  • 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

Limitations

  • 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.
  • PredictionEngine is not thread-safe (fine for a console demo).

Future improvements

  • 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.

Suggested GitHub topics

dotnet mlnet machine-learning regression csharp fasttree data-science

License

MIT

About

Clean, SOLID ML.NET FastTree regression starter in C#/.NET 8 with synthetic data, DI, metrics and tests.

Topics

Resources

License

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages