ML.NET & Model Builder and (Bonus Track) ASP.NET App to consume the model. Enjoy it! 🤟
Tags: machinelearning, mldotnet, dotnet
Published: 2021-12-23 16:36:45


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mhjes98e43nplgiprx0a.png)

Create a App Console and add ML.NET from NugetPackage. I installed 1.7.0-preview.final (I love use and test previews versions 😁)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x365bdjg2rh2b992zdxz.png)

Create ML.NET context. MlContext is the staring point for all ML.NET operations. The MLContext is used for all aspects of creating and consuming an ML.NET model.

**Load Data:** Machine learning uses known data (for example, training data) to find patterns in order to make predictions on new, unknown data. The inputs for machine learning are called Features, which are the attributes used to make predictions. The output of machine learning is called the Label, which is the actual prediction.
Data in ML.NET is represented as an IDataView, which is a flexible, efficient way of describing tabular data (for example, rows and columns). IDataView objects can contain numbers, text, booleans, vectors, and more. You can load data from files or from real-time streaming sources to an IDataView. LoadFromTextFile allows you to load data from TXT, CSV, TSV, and other file formats.

_

## Use the "DataSTEMSalary.csv" file for load data
_

Tip: LoadFromEnumerable enables loading from in-memory collections, JSON/XML, relational and non-relational databases (for example, SQL, CosmosDB, MongoDB), and many other data sources. Link for more information: [https://docs.microsoft.com/en-us/dotnet/machine-learning/how-to-guides/load-data-ml-net?WT.mc_id=dotnet-35129-website](https://docs.microsoft.com/en-us/dotnet/machine-learning/how-to-guides/load-data-ml-net?WT.mc_id=dotnet-35129-website)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d11c4mampdrl930vl5t1.png)

**Transform data:** In most cases, the data that you have available isn't suitable to be used directly to train a machine learning model. The raw data needs to be pre-processed using data transformations.

**Choose algorithm:** When using machine learning and ML.NET, you must choose a machine learning task that goes along with your scenario. ML.NET offers over 30 algorithms (or trainers) for a variety of ML tasks:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kwyizr4bbww4rg7300i1.png)

**I used LbfgsPoissonRegression.**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k2qwojc0eertnbh6qovi.png)

**Train model**: The data transformations and algorithms you have specified are not executed until you call the Fit() method (because of ML.NET's lazy loading approach). This is when model training happens.
An estimator takes in data, learns from the data, and creates a transformer. In the case of model training, the training data is the input, and the trained model is the output; the trained model is thus a transformer that turns the input Features from new data into output predictions.
Link for more information aboul model training: [https://docs.microsoft.com/en-us/dotnet/machine-learning/how-to-guides/train-machine-learning-model-ml-net?WT.mc_id=dotnet-35129-website](https://docs.microsoft.com/en-us/dotnet/machine-learning/how-to-guides/train-machine-learning-model-ml-net?WT.mc_id=dotnet-35129-website)

**Evaluate model:** ML.NET offers evaluators that assess the performance of your model on a variety of metrics. Link evaluation metrics: [https://docs.microsoft.com/en-us/dotnet/machine-learning/resources/metrics?WT.mc_id=dotnet-35129-website](https://docs.microsoft.com/en-us/dotnet/machine-learning/resources/metrics?WT.mc_id=dotnet-35129-website)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bx1xh4rbvte2k7lzfnum.png)

---
**

## Use the same example with ML.NET Model Builder 
**💪
ML.NET Model Builder is an intuitive graphical Visual Studio extension to build, train, and deploy custom machine learning models. Model Builder uses automated machine learning (AutoML) to explore different machine learning algorithms and settings to help you find the one that best suits your scenario.

How to install ML.NET Model Builder Link: [https://docs.microsoft.com/en-us/dotnet/machine-learning/how-to-guides/install-model-builder?tabs=visual-studio-2022](https://docs.microsoft.com/en-us/dotnet/machine-learning/how-to-guides/install-model-builder?tabs=visual-studio-2022)

Create a App Console and add ML.NET from NugetPackage. I installed 1.7.0-preview.final.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/41bnyibjd32lhnh2sm9y.png)

**Select Value prediction Scenario.**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vsozgi6x7xcjhw2olag7.png)

**Select Trainning Environment.**


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wxx07xf6rstnk9s00or7.png)

**Once you have chosen your scenario, Model Builder asks you to provide a dataset.**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zhfibw6594t5wb4xpay7.png)
**Click the Advanced data options link and check the categorical columns.**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8t5idckrjsb5plq2ssh3.png)

**Train: The machine learning task used to train the annual salary prediction model in this tutorial is regression**


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mhv8hi579wrszf7fsd1c.png)

**During the model training process, Model Builder trains separate models using different regression algorithms and settings to find the best-performing model for your dataset.**


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y0hm96ptjyy5znlh2i5p.png)

**Evaluate: The result of the training step will be one model which had the best performance. The evaluation step of the Model Builder tool, in the Best model section, will contain the algorithm used by the best performing model in the Model entry along with metrics for that model in RSquared.**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hopq4katku6c80ta7loz.png)

---
_

## Bonus Track!!! I share a web application in NET Core 3.1 using these models to predict the annual salary. 😊
_



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c8r11vw3qtx78do5fg00.png)

**I was training with a little more time with model builder and then exported the model to use it in my ASP.NET application.**

**Keep creating and training models with ML.NET.
Happy Coding !!!**


**DataSet:** [https://www.kaggle.com/jackogozaly/data-science-and-stem-salaries](https://www.kaggle.com/jackogozaly/data-science-and-stem-salaries)

**Source code:** [https://github.com/ppiova/STEMsalaries.ML.NET](https://github.com/ppiova/STEMsalaries.ML.NET)

**ML.NET Documentation**

- [https://docs.microsoft.com/en-us/dotnet/machine-learning/](https://docs.microsoft.com/en-us/dotnet/machine-learning/)

- [https://dotnet.microsoft.com/learn/ml-dotnet/what-is-mldotnet](https://dotnet.microsoft.com/learn/ml-dotnet/what-is-mldotnet)

- [https://dotnet.microsoft.com/apps/machinelearning-ai/ml-dotnet/model-builder](https://dotnet.microsoft.com/apps/machinelearning-ai/ml-dotnet/model-builder)