#MLNET – How to use the AutoML API in a Console App

Hi !

In my last posts I was testing AutoML using the Model Builder inside Visual Studio and also the CLI commands. There is also an API to use this in a .Net app, and the usage is very simple.

It all start, of course, adding the [Microsoft.ML.AutoML] nuget package

I read the documentation in [How to use the ML.NET automated machine learning API], and I created the following sample using the same data as in my previous posts.

private const uint ExperimentTime = 180;
static void Main(string[] args)
{
var mlContext = new MLContext();
Train(mlContext);
Console.WriteLine("Process complete! Press any key to close the app.");
Console.ReadKey();
}
public static void Train(MLContext mlContext)
{
try
{
// STEP 1: Load the data
var trainData = mlContext.Data.LoadFromTextFile(path: "AgeRangeData03_AgeGenderLabelEncodedMoreData.csv",
columns: new[]
{
new TextLoader.Column("Age", DataKind.Single, 0),
new TextLoader.Column("Gender", DataKind.Single, 1)
,
new TextLoader.Column("Label", DataKind.Single, 2)
},
hasHeader: true,
separatorChar: ','
);
var progressHandler = new MulticlassExperimentProgressHandler();
ConsoleHelper.ConsoleWriteHeader("=============== Running AutoML experiment ===============");
Console.WriteLine($"Running AutoML multiclass classification experiment for {ExperimentTime} seconds…");
ExperimentResult<MulticlassClassificationMetrics> experimentResult = mlContext.Auto()
.CreateMulticlassClassificationExperiment(ExperimentTime)
.Execute(trainData, "Label", progressHandler: progressHandler);
// Print top models found by AutoML
Console.WriteLine();
PrintTopModels(experimentResult);
Console.WriteLine();
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}

The final result displays the results for each one of the tests and showcase the top 3 ranked models. This time LightGBM Trainer is one more time the best trainer to choose.

There is a full set of samples in the Machine Learning .Net Samples repository. I’ve reused some classes from the Common folder.

The complete source code is available https://github.com/elbruno/Blog/tree/master/20190516%20MLNET%20AutoML%20API

Happy Coding!

Greetings @ Toronto

El Bruno

References

Advertisement

Leave a comment

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 )

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.

%d bloggers like this: