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

I1

Happy coding!

Greetings @ Burlington

El Bruno

References

 

21 responses to “#MLNET – Hello World in ML.Net, Machine Learning for .Net !”

  1. […] Hello World in ML.Net, Machine Learning for .Net ! […]

    Like

  2. […] Hello World in ML.Net, Machine Learning for .Net ! […]

    Like

  3. […] Hello World in ML.Net, Machine Learning for .Net ! […]

    Like

  4. […] Hello World in ML.Net, Machine Learning for .Net ! […]

    Like

  5. […] Hello World in ML.Net, Machine Learning for .Net ! […]

    Like

  6. […] Hello World in ML.Net, Machine Learning for .Net ! […]

    Like

  7. […] Hello World in ML.Net, Machine Learning for .Net ! […]

    Like

  8. […] Hello World in ML.Net, Machine Learning for .Net ! […]

    Like

  9. […] Hello World in ML.Net, Machine Learning for .Net ! […]

    Like

  10. […] Hello World in ML.Net, Machine Learning for .Net ! […]

    Like

Leave a reply to #MLNET – API improvements in the new 0.6.0 version – El Bruno Cancel reply

Discover more from El Bruno

Subscribe now to keep reading and get access to the full archive.

Continue reading