Hi!
I can not wait to start writing a little more about ML.Net. For now just a couple of code snippets to show how simple and fast it can be
Starting with a set of data with ages to classify babies and kids, in a CSV file. All based on my personal criteria
| 0 | 3 | baby | |
|---|---|---|---|
| 1 | 3 | baby | |
| 3 | 3 | baby | |
| 4 | 6 | kid | |
| 6 | 8 | kid | |
| 5 | 9 | kid | |
| 6 | 10 | kid | |
| 9 | 10 | kid |
And now a little magic with ML. A .Net Core Console application where we create a LearningPipeline and train it with the previous CSV information
| using System; | |
| using Microsoft.ML; | |
| using Microsoft.ML.Runtime.Api; | |
| using Microsoft.ML.Trainers; | |
| using Microsoft.ML.Transforms; | |
| namespace MlNetConsole01 | |
| { | |
| class Program | |
| { | |
| static void Main(string[] args) | |
| { | |
| var agesRangesCsv = "AgeRangeData.csv"; | |
| var pipeline = new LearningPipeline | |
| { | |
| new TextLoader<AgeRangeData>(agesRangesCsv, separator: ","), | |
| new Dictionarizer("Label"), | |
| new ColumnConcatenator("Features", "AgeStart", "AgeEnd"), | |
| new StochasticDualCoordinateAscentClassifier(), | |
| new PredictedLabelColumnOriginalValueConverter {PredictedLabelColumn = "PredictedLabel"} | |
| }; | |
| var model = pipeline.Train<AgeRangeData, AgeRangePrediction>(); | |
| var prediction = model.Predict(new AgeRangeData() | |
| { | |
| AgeStart = 1, | |
| AgeEnd = 2 | |
| }); | |
| Console.WriteLine($"Predicted age range is: {prediction.PredictedLabels}"); | |
| prediction = model.Predict(new AgeRangeData() | |
| { | |
| AgeStart = 7, | |
| AgeEnd = 7 | |
| }); | |
| Console.WriteLine($"Predicted age range is: {prediction.PredictedLabels}"); | |
| Console.ReadLine(); | |
| } | |
| } | |
| public class AgeRangeData | |
| { | |
| [Column(ordinal: "0")] | |
| public float AgeStart; | |
| [Column(ordinal: "1")] | |
| public float AgeEnd; | |
| [Column(ordinal: "2", name: "Label")] | |
| public string Label; | |
| } | |
| public class AgeRangePrediction | |
| { | |
| [ColumnName("PredictedLabel")] | |
| public string PredictedLabels; | |
| } | |
| } |
Below we check a couple of predictions with Age Ranges that are not part of the original CSV

Happy coding!
Greetings @ Burlington
El Bruno
References
Leave a Reply