forked from dotnet/machinelearning
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[AutoML] Cross validation fixes; validate empty training / validation…
… input data (dotnet#3794)
- Loading branch information
Showing
5 changed files
with
162 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using System.Linq; | ||
using Microsoft.ML.Data; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
namespace Microsoft.ML.AutoML.Test | ||
{ | ||
[TestClass] | ||
public class SplitUtilTests | ||
{ | ||
/// <summary> | ||
/// When there's only one row of data, assert that | ||
/// attempted cross validation throws (all splits should have empty | ||
/// train or test set). | ||
/// </summary> | ||
[TestMethod] | ||
[ExpectedException(typeof(InvalidOperationException))] | ||
public void CrossValSplitThrowsWhenNotEnoughData() | ||
{ | ||
var mlContext = new MLContext(); | ||
var dataViewBuilder = new ArrayDataViewBuilder(mlContext); | ||
dataViewBuilder.AddColumn("Number", NumberDataViewType.Single, 0f); | ||
dataViewBuilder.AddColumn("Label", NumberDataViewType.Single, 0f); | ||
var dataView = dataViewBuilder.GetDataView(); | ||
SplitUtil.CrossValSplit(mlContext, dataView, 10, null); | ||
} | ||
|
||
/// <summary> | ||
/// When there are few rows of data, assert that | ||
/// cross validation succeeds, but # of splits is less than 10 | ||
/// (splits with empty train or test sets should not be returned from this API). | ||
/// </summary> | ||
[TestMethod] | ||
public void CrossValSplitSmallDataView() | ||
{ | ||
var mlContext = new MLContext(seed: 0); | ||
var dataViewBuilder = new ArrayDataViewBuilder(mlContext); | ||
dataViewBuilder.AddColumn("Number", NumberDataViewType.Single, new float[9]); | ||
dataViewBuilder.AddColumn("Label", NumberDataViewType.Single, new float[9]); | ||
var dataView = dataViewBuilder.GetDataView(); | ||
const int requestedNumSplits = 10; | ||
var splits = SplitUtil.CrossValSplit(mlContext, dataView, requestedNumSplits, null); | ||
Assert.IsTrue(splits.trainDatasets.Any()); | ||
Assert.IsTrue(splits.trainDatasets.Count() < requestedNumSplits); | ||
Assert.AreEqual(splits.trainDatasets.Count(), splits.validationDatasets.Count()); | ||
} | ||
|
||
/// <summary> | ||
/// Assert that with many rows of data, cross validation produces the requested | ||
/// # of splits. | ||
/// </summary> | ||
[TestMethod] | ||
public void CrossValSplitLargeDataView() | ||
{ | ||
var mlContext = new MLContext(seed: 0); | ||
var dataViewBuilder = new ArrayDataViewBuilder(mlContext); | ||
dataViewBuilder.AddColumn("Number", NumberDataViewType.Single, new float[10000]); | ||
dataViewBuilder.AddColumn("Label", NumberDataViewType.Single, new float[10000]); | ||
var dataView = dataViewBuilder.GetDataView(); | ||
const int requestedNumSplits = 10; | ||
var splits = SplitUtil.CrossValSplit(mlContext, dataView, requestedNumSplits, null); | ||
Assert.IsTrue(splits.trainDatasets.Any()); | ||
Assert.AreEqual(requestedNumSplits, splits.trainDatasets.Count()); | ||
Assert.AreEqual(requestedNumSplits, splits.validationDatasets.Count()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters