Share via


DataOperationsCatalog.CrossValidationSplit 메서드

정의

데이터 세트를 학습 집합 및 테스트 집합의 교차 유효성 검사 접기로 분할합니다. 제공된 경우를 samplingKeyColumnName 존중합니다.

public System.Collections.Generic.IReadOnlyList<Microsoft.ML.DataOperationsCatalog.TrainTestData> CrossValidationSplit (Microsoft.ML.IDataView data, int numberOfFolds = 5, string samplingKeyColumnName = default, int? seed = default);
member this.CrossValidationSplit : Microsoft.ML.IDataView * int * string * Nullable<int> -> System.Collections.Generic.IReadOnlyList<Microsoft.ML.DataOperationsCatalog.TrainTestData>
Public Function CrossValidationSplit (data As IDataView, Optional numberOfFolds As Integer = 5, Optional samplingKeyColumnName As String = Nothing, Optional seed As Nullable(Of Integer) = Nothing) As IReadOnlyList(Of DataOperationsCatalog.TrainTestData)

매개 변수

data
IDataView

분할할 데이터 세트입니다.

numberOfFolds
Int32

교차 유효성 검사 접기 수입니다.

samplingKeyColumnName
String

행 그룹화에 사용할 열의 이름입니다. 두 예제가 동일한 값을 samplingKeyColumnName공유하는 경우 동일한 하위 집합(학습 또는 테스트)에 표시되도록 보장됩니다. 이는 기차에서 테스트 세트로 레이블이 누출되지 않도록 하는 데 사용할 수 있습니다. 순위 실험을 samplingKeyColumnName 수행할 때 GroupId 열이어야 합니다. 행 그룹화가 수행되지 않는 경우 null

seed
Nullable<Int32>

교차 유효성 검사 폴드에 대한 행을 선택하는 데 사용되는 난수 생성기의 초기값입니다.

반환

예제

using System;
using System.Collections.Generic;
using Microsoft.ML;

namespace Samples.Dynamic
{
    /// <summary>
    /// Sample class showing how to use CrossValidationSplit.
    /// </summary>
    public static class CrossValidationSplit
    {
        public static void Example()
        {
            // Creating the ML.Net IHostEnvironment object, needed for the pipeline.
            var mlContext = new MLContext();

            // Generate some data points.
            var examples = GenerateRandomDataPoints(10);

            // Convert the examples list to an IDataView object, which is consumable
            // by ML.NET API.
            var dataview = mlContext.Data.LoadFromEnumerable(examples);

            // Cross validation splits your data randomly into set of "folds", and
            // creates groups of Train and Test sets, where for each group, one fold
            // is the Test and the rest of the folds the Train. So below, we specify
            // Group column as the column containing the sampling keys. If we pass
            // that column to cross validation it would be used to break data into
            // certain chunks.
            var folds = mlContext.Data
                .CrossValidationSplit(dataview, numberOfFolds: 3,
                samplingKeyColumnName: "Group");

            var trainSet = mlContext.Data
                .CreateEnumerable<DataPoint>(folds[0].TrainSet,
                reuseRowObject: false);

            var testSet = mlContext.Data
                .CreateEnumerable<DataPoint>(folds[0].TestSet,
                reuseRowObject: false);

            PrintPreviewRows(trainSet, testSet);

            // The data in the Train split.
            // [Group, 1], [Features, 0.8173254]
            // [Group, 2], [Features, 0.7680227]
            // [Group, 1], [Features, 0.2060332]
            // [Group, 2], [Features, 0.5588848]
            // [Group, 1], [Features, 0.4421779]
            // [Group, 2], [Features, 0.9775497]
            // 
            // The data in the Test split.
            // [Group, 0], [Features, 0.7262433]
            // [Group, 0], [Features, 0.5581612]
            // [Group, 0], [Features, 0.9060271]
            // [Group, 0], [Features, 0.2737045]

            trainSet = mlContext.Data
                .CreateEnumerable<DataPoint>(folds[1].TrainSet,
                reuseRowObject: false);

            testSet = mlContext.Data
                .CreateEnumerable<DataPoint>(folds[1].TestSet,
                reuseRowObject: false);

            PrintPreviewRows(trainSet, testSet);
            // The data in the Train split.
            // [Group, 0], [Features, 0.7262433]
            // [Group, 2], [Features, 0.7680227]
            // [Group, 0], [Features, 0.5581612]
            // [Group, 2], [Features, 0.5588848]
            // [Group, 0], [Features, 0.9060271]
            // [Group, 2], [Features, 0.9775497]
            // [Group, 0], [Features, 0.2737045]
            // 
            // The data in the Test split.
            // [Group, 1], [Features, 0.8173254]
            // [Group, 1], [Features, 0.2060332]
            // [Group, 1], [Features, 0.4421779]

            trainSet = mlContext.Data
                .CreateEnumerable<DataPoint>(folds[2].TrainSet,
                reuseRowObject: false);

            testSet = mlContext.Data
                .CreateEnumerable<DataPoint>(folds[2].TestSet,
                reuseRowObject: false);

            PrintPreviewRows(trainSet, testSet);
            // The data in the Train split.
            // [Group, 0], [Features, 0.7262433]
            // [Group, 1], [Features, 0.8173254]
            // [Group, 0], [Features, 0.5581612]
            // [Group, 1], [Features, 0.2060332]
            // [Group, 0], [Features, 0.9060271]
            // [Group, 1], [Features, 0.4421779]
            // [Group, 0], [Features, 0.2737045]
            // 
            // The data in the Test split.
            // [Group, 2], [Features, 0.7680227]
            // [Group, 2], [Features, 0.5588848]
            // [Group, 2], [Features, 0.9775497]

            // Example of a split without specifying a sampling key column.
            folds = mlContext.Data.CrossValidationSplit(dataview, numberOfFolds: 3);
            trainSet = mlContext.Data
                .CreateEnumerable<DataPoint>(folds[0].TrainSet,
                reuseRowObject: false);

            testSet = mlContext.Data
                .CreateEnumerable<DataPoint>(folds[0].TestSet,
                reuseRowObject: false);

            PrintPreviewRows(trainSet, testSet);
            // The data in the Train split.
            // [Group, 0], [Features, 0.7262433]
            // [Group, 1], [Features, 0.8173254]
            // [Group, 2], [Features, 0.7680227]
            // [Group, 0], [Features, 0.5581612]
            // [Group, 1], [Features, 0.2060332]
            // [Group, 1], [Features, 0.4421779]
            // [Group, 2], [Features, 0.9775497]
            // [Group, 0], [Features, 0.2737045]
            // 
            // The data in the Test split.
            // [Group, 2], [Features, 0.5588848]
            // [Group, 0], [Features, 0.9060271]

            trainSet = mlContext.Data
                .CreateEnumerable<DataPoint>(folds[1].TrainSet,
                reuseRowObject: false);

            testSet = mlContext.Data
                .CreateEnumerable<DataPoint>(folds[1].TestSet,
                reuseRowObject: false);

            PrintPreviewRows(trainSet, testSet);
            // The data in the Train split.
            // [Group, 2], [Features, 0.7680227]
            // [Group, 0], [Features, 0.5581612]
            // [Group, 1], [Features, 0.2060332]
            // [Group, 2], [Features, 0.5588848]
            // [Group, 0], [Features, 0.9060271]
            // [Group, 1], [Features, 0.4421779]
            // 
            // The data in the Test split.
            // [Group, 0], [Features, 0.7262433]
            // [Group, 1], [Features, 0.8173254]
            // [Group, 2], [Features, 0.9775497]
            // [Group, 0], [Features, 0.2737045]

            trainSet = mlContext.Data
                .CreateEnumerable<DataPoint>(folds[2].TrainSet,
                reuseRowObject: false);

            testSet = mlContext.Data.CreateEnumerable<DataPoint>(folds[2].TestSet,
                reuseRowObject: false);

            PrintPreviewRows(trainSet, testSet);
            // The data in the Train split.
            // [Group, 0], [Features, 0.7262433]
            // [Group, 1], [Features, 0.8173254]
            // [Group, 2], [Features, 0.5588848]
            // [Group, 0], [Features, 0.9060271]
            // [Group, 2], [Features, 0.9775497]
            // [Group, 0], [Features, 0.2737045]
            // 
            // The data in the Test split.
            // [Group, 2], [Features, 0.7680227]
            // [Group, 0], [Features, 0.5581612]
            // [Group, 1], [Features, 0.2060332]
            // [Group, 1], [Features, 0.4421779]
        }

        private static IEnumerable<DataPoint> GenerateRandomDataPoints(int count,
            int seed = 0)

        {
            var random = new Random(seed);
            for (int i = 0; i < count; i++)
            {
                yield return new DataPoint
                {
                    Group = i % 3,

                    // Create random features that are correlated with label.
                    Features = (float)random.NextDouble()
                };
            }
        }

        // Example with features and group column. A data set is a collection of
        // such examples.
        private class DataPoint
        {
            public float Group { get; set; }

            public float Features { get; set; }
        }

        // print helper
        private static void PrintPreviewRows(IEnumerable<DataPoint> trainSet,
            IEnumerable<DataPoint> testSet)

        {

            Console.WriteLine($"The data in the Train split.");
            foreach (var row in trainSet)
                Console.WriteLine($"{row.Group}, {row.Features}");

            Console.WriteLine($"\nThe data in the Test split.");
            foreach (var row in testSet)
                Console.WriteLine($"{row.Group}, {row.Features}");
        }
    }
}

적용 대상