DataOperationsCatalog.LoadFromEnumerable 方法

定義

多載

LoadFromEnumerable<TRow>(IEnumerable<TRow>, SchemaDefinition)

在使用者定義型別專案的可列舉上建立新的 IDataView 。 使用者會維護 的 data 擁有權,而產生的資料檢視永遠不會改變 的內容 data 。 由於 IDataView 假設為不可變,因此使用者預期支援會傳回相同結果之 的 data 多個列舉,除非使用者知道資料只會進行資料指標處理一次。

串流資料檢視的一個典型用法可能是:建立資料檢視,視需要延遲載入資料,然後將預先定型的轉換套用至該檢視,然後針對轉換結果進行資料指標。

LoadFromEnumerable<TRow>(IEnumerable<TRow>, DataViewSchema)

使用提供的 DataViewSchema ,在可列舉的使用者定義型別專案上建立新的 IDataView ,其中可能包含架構的詳細資訊,而不是類型可以擷取。

LoadFromEnumerable<TRow>(IEnumerable<TRow>, SchemaDefinition)

在使用者定義型別專案的可列舉上建立新的 IDataView 。 使用者會維護 的 data 擁有權,而產生的資料檢視永遠不會改變 的內容 data 。 由於 IDataView 假設為不可變,因此使用者預期支援會傳回相同結果之 的 data 多個列舉,除非使用者知道資料只會進行資料指標處理一次。

串流資料檢視的一個典型用法可能是:建立資料檢視,視需要延遲載入資料,然後將預先定型的轉換套用至該檢視,然後針對轉換結果進行資料指標。

public Microsoft.ML.IDataView LoadFromEnumerable<TRow> (System.Collections.Generic.IEnumerable<TRow> data, Microsoft.ML.Data.SchemaDefinition schemaDefinition = default) where TRow : class;
member this.LoadFromEnumerable : seq<'Row (requires 'Row : null)> * Microsoft.ML.Data.SchemaDefinition -> Microsoft.ML.IDataView (requires 'Row : null)
Public Function LoadFromEnumerable(Of TRow As Class) (data As IEnumerable(Of TRow), Optional schemaDefinition As SchemaDefinition = Nothing) As IDataView

類型參數

TRow

使用者定義專案類型。

參數

data
IEnumerable<TRow>

包含要轉換成 之型 TRow 別的 IDataView 可列舉資料。

schemaDefinition
SchemaDefinition

要建立之資料檢視的選擇性架構定義。 如果 null 為 ,則會從 TRow 推斷架構定義。

傳回

建構的 IDataView

範例

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

namespace Samples.Dynamic
{
    public static class LoadFromEnumerable
    {
        // Creating IDataView from IEnumerable, and setting the size of the vector
        // at runtime. When the data model is defined through types, setting the
        // size of the vector is done through the VectorType annotation. When the
        // size of the data is not known at compile time, the Schema can be directly
        // modified at runtime and the size of the vector set there. This is
        // important, because most of the ML.NET trainers require the Features
        // vector to be of known size. 
        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<DataPointVector> enumerableKnownSize = new DataPointVector[]
            {
               new DataPointVector{ Features = new float[]{ 1.2f, 3.4f, 4.5f, 3.2f,
                   7,5f } },

               new DataPointVector{ Features = new float[]{ 4.2f, 3.4f, 14.65f,
                   3.2f, 3,5f } },

               new DataPointVector{ Features = new float[]{ 1.6f, 3.5f, 4.5f, 6.2f,
                   3,5f } },

            };

            // Load dataset into an IDataView. 
            IDataView data = mlContext.Data.LoadFromEnumerable(enumerableKnownSize);
            var featureColumn = data.Schema["Features"].Type as VectorDataViewType;
            // Inspecting the schema
            Console.WriteLine($"Is the size of the Features column known: " +
                $"{featureColumn.IsKnownSize}.\nSize: {featureColumn.Size}");

            // Preview
            //
            // Is the size of the Features column known? True.
            // Size: 5.

            // If the size of the vector is unknown at compile time, it can be set 
            // at runtime.
            IEnumerable<DataPoint> enumerableUnknownSize = new DataPoint[]
            {
               new DataPoint{ Features = new float[]{ 1.2f, 3.4f, 4.5f } },
               new DataPoint{ Features = new float[]{ 4.2f, 3.4f, 1.6f } },
               new DataPoint{ Features = new float[]{ 1.6f, 3.5f, 4.5f } },
            };

            // The feature dimension (typically this will be the Count of the array 
            // of the features vector known at runtime).
            int featureDimension = 3;
            var definedSchema = SchemaDefinition.Create(typeof(DataPoint));
            featureColumn = definedSchema["Features"]
                .ColumnType as VectorDataViewType;

            Console.WriteLine($"Is the size of the Features column known: " +
                $"{featureColumn.IsKnownSize}.\nSize: {featureColumn.Size}");

            // Preview
            //
            // Is the size of the Features column known? False.
            // Size: 0.

            // Set the column type to be a known-size vector.
            var vectorItemType = ((VectorDataViewType)definedSchema[0].ColumnType)
                .ItemType;
            definedSchema[0].ColumnType = new VectorDataViewType(vectorItemType,
                featureDimension);

            // Read the data into an IDataView with the modified schema supplied in
            IDataView data2 = mlContext.Data
                .LoadFromEnumerable(enumerableUnknownSize, definedSchema);

            featureColumn = data2.Schema["Features"].Type as VectorDataViewType;
            // Inspecting the schema
            Console.WriteLine($"Is the size of the Features column known: " +
                $"{featureColumn.IsKnownSize}.\nSize: {featureColumn.Size}");

            // Preview
            //
            // Is the size of the Features column known? True. 
            // Size: 3.
        }
    }

    public class DataPoint
    {
        public float[] Features { get; set; }
    }

    public class DataPointVector
    {
        [VectorType(5)]
        public float[] Features { get; set; }
    }
}

適用於

LoadFromEnumerable<TRow>(IEnumerable<TRow>, DataViewSchema)

使用提供的 DataViewSchema ,在可列舉的使用者定義型別專案上建立新的 IDataView ,其中可能包含架構的詳細資訊,而不是類型可以擷取。

public Microsoft.ML.IDataView LoadFromEnumerable<TRow> (System.Collections.Generic.IEnumerable<TRow> data, Microsoft.ML.DataViewSchema schema) where TRow : class;
member this.LoadFromEnumerable : seq<'Row (requires 'Row : null)> * Microsoft.ML.DataViewSchema -> Microsoft.ML.IDataView (requires 'Row : null)
Public Function LoadFromEnumerable(Of TRow As Class) (data As IEnumerable(Of TRow), schema As DataViewSchema) As IDataView

類型參數

TRow

使用者定義專案類型。

參數

data
IEnumerable<TRow>

包含要轉換成 之型 TRow 別的 IDataView 可列舉資料。

schema
DataViewSchema

傳回 IDataView 之 的架構。

傳回

IDataView具有指定 schema 之 的 。

備註

使用者會維護 的 data 擁有權,而產生的資料檢視永遠不會改變 的內容 data 。 由於 IDataView 假設為不可變,因此使用者預期支援會傳回相同結果之 的 data 多個列舉,除非使用者知道資料只會進行資料指標處理一次。 串流資料檢視的一個典型用法可能是:建立資料檢視,視需要延遲載入資料,然後將預先定型的轉換套用至該檢視,然後針對轉換結果進行資料指標。 其中一個實用的用法是透過 DataViewSchema.Annotations 提供功能資料行名稱。

適用於