다음을 통해 공유


DataOperationsCatalog.CreateEnumerable<TRow> 메서드

정의

IDataView 강력한 형식IEnumerable<T>의 .로 변환합니다.

public System.Collections.Generic.IEnumerable<TRow> CreateEnumerable<TRow> (Microsoft.ML.IDataView data, bool reuseRowObject, bool ignoreMissingColumns = false, Microsoft.ML.Data.SchemaDefinition schemaDefinition = default) where TRow : class, new();
member this.CreateEnumerable : Microsoft.ML.IDataView * bool * bool * Microsoft.ML.Data.SchemaDefinition -> seq<'Row (requires 'Row : null and 'Row : (new : unit -> 'Row))> (requires 'Row : null and 'Row : (new : unit -> 'Row))
Public Function CreateEnumerable(Of TRow As {Class, New}) (data As IDataView, reuseRowObject As Boolean, Optional ignoreMissingColumns As Boolean = false, Optional schemaDefinition As SchemaDefinition = Nothing) As IEnumerable(Of TRow)

형식 매개 변수

TRow

사용자 정의 항목 유형입니다.

매개 변수

data
IDataView

기본 데이터 뷰입니다.

reuseRowObject
Boolean

모든 행에서 동일한 개체를 반환할지 아니면 행당 새 개체를 할당할지 여부입니다.

ignoreMissingColumns
Boolean

요청된 열이 데이터 뷰에 없는 경우 대/소문자를 무시할지 여부입니다.

schemaDefinition
SchemaDefinition

선택적 사용자 제공 스키마 정의입니다. 스키마가 없는 경우 스키마는 T 정의에서 유추됩니다.

반환

IEnumerable<TRow>

IEnumerable<T> 있는 데이터를 보유하는 것입니다 data. 여러 번 열거할 수 있습니다.

예제

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

namespace Samples.Dynamic
{
    public static class DataViewEnumerable
    {
        // A simple case of creating IDataView from
        //IEnumerable.
        public static void Example()
        {
            // Create a new context for ML.NET operations. It can be used for
            // exception tracking and logging,
            // as a catalog of available operations and as the source of randomness.
            var mlContext = new MLContext();

            // Get a small dataset as an IEnumerable.
            IEnumerable<SampleTemperatureData> enumerableOfData =
                GetSampleTemperatureData(5);

            // Load dataset into an IDataView. 
            IDataView data = mlContext.Data.LoadFromEnumerable(enumerableOfData);

            // We can now examine the records in the IDataView. We first create an
            // enumerable of rows in the IDataView.
            var rowEnumerable = mlContext.Data
                .CreateEnumerable<SampleTemperatureData>(data,
                reuseRowObject: true);

            // SampleTemperatureDataWithLatitude has the definition of a Latitude
            // column of type float. We can use the parameter ignoreMissingColumns
            // to true to ignore any missing columns in the IDataView. The produced
            // enumerable will have the Latitude field set to the default for the
            // data type, in this case 0. 
            var rowEnumerableIgnoreMissing = mlContext.Data
                .CreateEnumerable<SampleTemperatureDataWithLatitude>(data,
                reuseRowObject: true, ignoreMissingColumns: true);

            Console.WriteLine($"Date\tTemperature");
            foreach (var row in rowEnumerable)
                Console.WriteLine(
                    $"{row.Date.ToString("d")}\t{row.Temperature}");

            // Expected output:
            //  Date    Temperature
            //  1/2/2012        36
            //  1/3/2012        36
            //  1/4/2012        34
            //  1/5/2012        35
            //  1/6/2012        35

            Console.WriteLine($"Date\tTemperature\tLatitude");
            foreach (var row in rowEnumerableIgnoreMissing)
                Console.WriteLine($"{row.Date.ToString("d")}\t{row.Temperature}"
                    + $"\t{row.Latitude}");

            // Expected output:
            //  Date    Temperature     Latitude
            //  1/2/2012        36      0
            //  1/3/2012        36      0
            //  1/4/2012        34      0
            //  1/5/2012        35      0
            //  1/6/2012        35      0
        }

        private class SampleTemperatureData
        {
            public DateTime Date { get; set; }
            public float Temperature { get; set; }
        }

        private class SampleTemperatureDataWithLatitude
        {
            public float Latitude { get; set; }
            public DateTime Date { get; set; }
            public float Temperature { get; set; }
        }

        /// <summary>
        /// Get a fake temperature dataset.
        /// </summary>
        /// <param name="exampleCount">The number of examples to return.</param>
        /// <returns>An enumerable of <see cref="SampleTemperatureData"/>.</returns>
        private static IEnumerable<SampleTemperatureData> GetSampleTemperatureData(
            int exampleCount)

        {
            var rng = new Random(1234321);
            var date = new DateTime(2012, 1, 1);
            float temperature = 39.0f;

            for (int i = 0; i < exampleCount; i++)
            {
                date = date.AddDays(1);
                temperature += rng.Next(-5, 5);
                yield return new SampleTemperatureData
                {
                    Date = date,
                    Temperature =
                    temperature
                };

            }
        }
    }
}

적용 대상