docs icon indicating copy to clipboard operation
docs copied to clipboard

MLNET: Cross validation example does not finish processing

Open kicaj29 opened this issue 2 years ago • 0 comments

Type of issue

Code doesn't work

Description

Hi

I run example from this page https://learn.microsoft.com/en-us/dotnet/machine-learning/how-to-guides/train-machine-learning-model-cross-validation-ml-net using the following code but cross validation process never ends. I think the example should be updated or it should be explained that it can behave like this.

In file data.csv I have the same data like on the page with link above.

Console.WriteLine("Cross validation starting...");

MLContext mlContext = new MLContext(seed: 12345);
IDataView data = mlContext.Data.LoadFromTextFile<HousingData>("data.csv", separatorChar: ',', hasHeader: true);

// Define data prep estimator
IEstimator<ITransformer> dataPrepEstimator =
    mlContext.Transforms.Concatenate("Features", new string[] { "Size", "HistoricalPrices" })
        .Append(mlContext.Transforms.NormalizeMinMax("Features"));

// Create data prep transformer
ITransformer dataPrepTransformer = dataPrepEstimator.Fit(data);

// Transform data
IDataView transformedData = dataPrepTransformer.Transform(data);

// Define StochasticDualCoordinateAscent algorithm estimator
IEstimator<ITransformer> sdcaEstimator = mlContext.Regression.Trainers.Sdca();

// Apply 5-fold cross validation
Stopwatch sw = Stopwatch.StartNew();

var cvResults = mlContext.Regression.CrossValidate(transformedData, sdcaEstimator, numberOfFolds: 5);

Console.WriteLine(sw.ToString()); // ----------This line is never executed when I run it.---------------

// Select all models
ITransformer[] models =
    cvResults
        .OrderByDescending(fold => fold.Metrics.RSquared)
        .Select(fold => fold.Model)
        .ToArray();

// Get Top Model
ITransformer topModel = models[0];

Console.WriteLine("Finished");

Page URL

https://learn.microsoft.com/en-us/dotnet/machine-learning/how-to-guides/train-machine-learning-model-cross-validation-ml-net

Content source URL

https://github.com/dotnet/docs/blob/main/docs/machine-learning/how-to-guides/train-machine-learning-model-cross-validation-ml-net.md

Document Version Independent Id

3c593c29-8e2f-9c88-26f6-c54a7345b6c7

Article author

@luisquintanilla

Metadata

  • ID: df25ca43-b9af-7d37-1b6d-690183ab421d
  • Product: dotnet-ml

kicaj29 avatar Dec 27 '23 12:12 kicaj29