Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Aded catch in R^2 calculation for case with few samples #5319

Merged
merged 4 commits into from
Jul 26, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/Microsoft.ML.Data/Evaluators/RegressionEvaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,15 @@ public override double RSquared
{
get
{
return SumWeights > 0 ? 1 - TotalL2Loss / (TotalLabelSquaredW - TotalLabelW * TotalLabelW / SumWeights) : 0;
// RSquared value cannot be well-defined with less than two samples.
// Return NaN instead of -Infinity.
if (SumWeights > 0)
{
if ((TotalLabelSquaredW - TotalLabelW * TotalLabelW / SumWeights) == 0)
return double.NaN;
return 1 - TotalL2Loss / (TotalLabelSquaredW - TotalLabelW * TotalLabelW / SumWeights);
}
return 0;
}
}

Expand Down
23 changes: 23 additions & 0 deletions test/Microsoft.ML.Functional.Tests/Validation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Microsoft.ML.Trainers.LightGbm;
using Xunit;
using Xunit.Abstractions;
using static Microsoft.ML.TrainCatalogBase;
mstfbl marked this conversation as resolved.
Show resolved Hide resolved

namespace Microsoft.ML.Functional.Tests
{
Expand Down Expand Up @@ -138,5 +139,27 @@ public void TrainWithValidationSet()
Common.AssertMetrics(trainMetrics);
Common.AssertMetrics(validMetrics);
}

/// <summary>
/// Test cross validation R^2 metric to return NaN when given fewer data
/// than needed to infer metric calculation. R^2 is NaN when given folds
/// with less than 2 rows of training data.
/// </summary>
[Fact]
public void TestCrossValidationResultsWithNotEnoughData()
{
var mlContext = new MLContext(1);
// Get data and set up sample regression pipeline.
var data = mlContext.Data.LoadFromTextFile<Iris>(TestCommon.GetDataPath(DataDir, TestDatasets.iris.trainFilename), hasHeader: true);
var dataFirstTenRows = mlContext.Data.TakeRows(data, 10);
var pipeline = mlContext.Transforms.Concatenate("Features", Iris.Features)
.Append(mlContext.Regression.Trainers.OnlineGradientDescent());

// Check that NaN is returned with fold given less than 2 rows of training data.
// With dataset of 10 rows, number of folds will be 6.
var cvResults = mlContext.Regression.CrossValidate(dataFirstTenRows, pipeline, numberOfFolds: 6);
mstfbl marked this conversation as resolved.
Show resolved Hide resolved
foreach (CrossValidationResult<RegressionMetrics> result in cvResults)
Assert.Equal(double.NaN, result.Metrics.RSquared);
}
}
}