How to Apply Platt Calibration to bestRun.Model in AutoML

Andreas ss 726 Reputation points
2023-11-10T20:05:06.8466667+00:00

I am using AutoML in ML.net.

  • trainData contains the Training Data
  • testData contains the TestData Data
  1. The code works well where I "CreateBinaryClassificationExperiment" for 20 seconds.
  2. After the 20 seconds, I return: var bestModel = bestRun.Model;

What I try to understand how to do now, is how to apply a:

Platt Calibration

I have scratched my head all day but are simply not sure what code that is needed after

var bestModel = bestRun.Model;

And how to return the Platt Calibration into:

(I would do the same for Isotonic and Naive Calibrations which would follow the same steps as I understand)

var plattCalibratedPredictions

Any help would be greatful!

Code:

   void run_CreateBinaryClassificationExperiment(IDataView trainData, IDataView testData, MLContext mlContext)
    {
        //Use "AutoML" to find the "Best Model"
        BinaryClassificationMetrics bestValidationMetrics = null; string bestTrainerName = null;
        var experiment = mlContext.Auto().CreateBinaryClassificationExperiment(new BinaryExperimentSettings
        {
            MaxExperimentTimeInSeconds = 7,
            CacheBeforeTrainer = CacheBeforeTrainer.On,
            CacheDirectoryName = "C:/Aintelligence/temp/cache",
            MaximumMemoryUsageInMegaByte = 8192, // Set the maximum memory usage (adjust as needed)
            OptimizingMetric = BinaryClassificationMetric.PositivePrecision
        });
        var progressHandler = new Progress<RunDetail<BinaryClassificationMetrics>>(ph =>
        {
            if (ph.ValidationMetrics != null)
            {
                Invoke((MethodInvoker)delegate
                {
                    // Check if the current run has better metrics than the best so far
                    listBox1.Items.Add($"Current trainer - {ph.TrainerName}, {ph.ValidationMetrics.PositivePrecision}");
                    if (bestValidationMetrics == null || ph.ValidationMetrics.PositivePrecision > bestValidationMetrics.PositivePrecision)
                    {
                        bestValidationMetrics = ph.ValidationMetrics; //Save the best Model, so far!
                        bestTrainerName = ph.TrainerName;
                    }
                });
            }
        });
        var results = experiment.Execute(trainData, testData, labelColumnName: "Label", progressHandler: progressHandler);
        var bestRun = results.BestRun;
        var metrics = bestRun.ValidationMetrics;
        Invoke((MethodInvoker)delegate { richTextBox1.Text = $"Best model {bestTrainerName}, {bestValidationMetrics?.PositivePrecision ?? 0.0}"; });
        //Use Platt to calibrate
        var plattCalibrator = mlContext.BinaryClassification.Calibrators.Platt();
        // Access the best model
        var bestModel = bestRun.Model;
        //What code do we have to put here to calibrate the "bestModel" with "Platt Calibration" on the "testData"
        //So the "Platt Calibration" is returned into: "plattCalibratedPredictions" 
        var plattCalibratedPredictions;
        //The "Evaluate" method is suitable for evaluating predictions that have been calibrated.
        var _metrics = mlContext.BinaryClassification.Evaluate(plattCalibratedPredictions, labelColumnName: "Label"); 
    }
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,008 questions
.NET Machine learning
.NET Machine learning
.NET: Microsoft Technologies based on the .NET software framework.Machine learning: A type of artificial intelligence focused on enabling computers to use observed data to evolve new behaviors that have not been explicitly programmed.
159 questions
{count} vote

2 answers

Sort by: Most helpful
  1. 2023-11-10T21:03:39.1966667+00:00

    void run_CreateBinaryClassificationExperiment(IDataView trainData, IDataView testData, MLContext mlContext)


  2. 2023-12-20T01:18:41.1566667+00:00

    var plattCalibratedPredictionsvoid run_CreateBinaryClassificationExperiment(IDataView trainData, IDataView testData, MLContext mlContext)

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.