#MLNET – Hola Mundo con ML.Net, Machine Learning for .Net !

Buenas!

No puedo esperar a empezar a escribir un poco mas al respecto sobre ML.Net. Por ahora solo un par de code snippets para demostrar lo simple y rápido que puede ser

Comenzando con un set de datos con edades para clasificar babies y kids, en un archivo CSV. Todo basado en mi criterio personal


0 3 baby
1 3 baby
3 3 baby
4 6 kid
6 8 kid
5 9 kid
6 10 kid
9 10 kid

view raw
MLNetAgeRanges.csv
hosted with ❤ by GitHub

Y ahora un poco de magia con ML. Una aplicación de Consola en donde creamos un LEarningPipeline y lo entrenamos con la información de CSV

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;
}
}

view raw
MLNetAgeRanges.cs
hosted with ❤ by GitHub

A continuación verificamos un par de predicciones con Age Ranges que no son parte del CSV original

I1

Happy coding!

Saludos @ Burlington

El Bruno

References

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.