Share via


TextLoaderSaverCatalog.CreateTextLoader 方法

定義

多載

CreateTextLoader(DataOperationsCatalog, TextLoader+Options, IMultiStreamSource)

建立文字載入器 TextLoader

CreateTextLoader(DataOperationsCatalog, TextLoader+Column[], Char, Boolean, IMultiStreamSource, Boolean, Boolean, Boolean)

建立文字載入器 TextLoader

CreateTextLoader<TInput>(DataOperationsCatalog, TextLoader+Options, IMultiStreamSource)

從資料模型類型推斷資料集架構,以建立文字載入器 TextLoader

CreateTextLoader<TInput>(DataOperationsCatalog, Char, Boolean, IMultiStreamSource, Boolean, Boolean, Boolean)

從資料模型類型推斷資料集架構,以建立文字載入器 TextLoader

CreateTextLoader(DataOperationsCatalog, TextLoader+Options, IMultiStreamSource)

建立文字載入器 TextLoader

public static Microsoft.ML.Data.TextLoader CreateTextLoader (this Microsoft.ML.DataOperationsCatalog catalog, Microsoft.ML.Data.TextLoader.Options options, Microsoft.ML.Data.IMultiStreamSource dataSample = default);
static member CreateTextLoader : Microsoft.ML.DataOperationsCatalog * Microsoft.ML.Data.TextLoader.Options * Microsoft.ML.Data.IMultiStreamSource -> Microsoft.ML.Data.TextLoader
<Extension()>
Public Function CreateTextLoader (catalog As DataOperationsCatalog, options As TextLoader.Options, Optional dataSample As IMultiStreamSource = Nothing) As TextLoader

參數

options
TextLoader.Options

定義載入作業的設定。

dataSample
IMultiStreamSource

資料範例的選擇性位置。 如果存在,則此範例可用來推斷位置名稱批註,以及使用 TextLoader.Rangenull 最大索引定義之 中 Columns 的位置數目。 如果範例已與 ML.NET SaveAsText(DataOperationsCatalog, IDataView, Stream, Char, Boolean, Boolean, Boolean, Boolean) 一起儲存,它也會在標頭中包含載入器可以讀取的架構資訊,即使未指定也一樣 Columns 。 為了使用檔案中定義的架構,所有其他 TextLoader.Options 都保留其預設值。

傳回

適用於

CreateTextLoader(DataOperationsCatalog, TextLoader+Column[], Char, Boolean, IMultiStreamSource, Boolean, Boolean, Boolean)

建立文字載入器 TextLoader

public static Microsoft.ML.Data.TextLoader CreateTextLoader (this Microsoft.ML.DataOperationsCatalog catalog, Microsoft.ML.Data.TextLoader.Column[] columns, char separatorChar = '\t', bool hasHeader = false, Microsoft.ML.Data.IMultiStreamSource dataSample = default, bool allowQuoting = false, bool trimWhitespace = false, bool allowSparse = false);
static member CreateTextLoader : Microsoft.ML.DataOperationsCatalog * Microsoft.ML.Data.TextLoader.Column[] * char * bool * Microsoft.ML.Data.IMultiStreamSource * bool * bool * bool -> Microsoft.ML.Data.TextLoader
<Extension()>
Public Function CreateTextLoader (catalog As DataOperationsCatalog, columns As TextLoader.Column(), Optional separatorChar As Char = '\t', Optional hasHeader As Boolean = false, Optional dataSample As IMultiStreamSource = Nothing, Optional allowQuoting As Boolean = false, Optional trimWhitespace As Boolean = false, Optional allowSparse As Boolean = false) As TextLoader

參數

columns
TextLoader.Column[]

定義架構的資料 TextLoader.Column 行陣列。

separatorChar
Char

用來作為資料列中資料點之間分隔符號的字元。 根據預設,索引標籤字元會當做分隔符號使用。

hasHeader
Boolean

檔案是否有具有功能名稱的標頭。 提供 時, true 表示 中的 第一行將用於功能名稱,而呼叫 時 Load(IMultiStreamSource) ,將會略過第一行。 如果沒有提供, true 只要指出載入器應該在呼叫 時 Load(IMultiStreamSource) 略過第一行,但資料行不會有位置名稱批註。 這是因為會在建立載入器時建立輸出架構,而不是呼叫 時 Load(IMultiStreamSource)

dataSample
IMultiStreamSource

資料範例的選擇性位置。 如果存在,則此範例可用來推斷位置名稱批註,以及以 null 最大索引定義 TextLoader.Range 之資料行中的位置數目。 如果範例已與 ML.NET SaveAsText(DataOperationsCatalog, IDataView, Stream, Char, Boolean, Boolean, Boolean, Boolean) 一起儲存,它也會在標頭中包含載入器可以讀取的架構資訊,即使 為 null 也一樣 columns 。 為了使用檔案中定義的架構,所有其他引數都會保留其預設值。

allowQuoting
Boolean

輸入是否可能包含雙引號值。 此參數用來區分輸入值中的分隔符號與實際分隔符號。 當 為 時 true ,雙引號內的分隔符號會視為輸入值的一部分。 當 為 時 false ,即使是引號內的分隔符號,也會被視為分隔新資料行。

trimWhitespace
Boolean

從行中移除尾端空白字元。

allowSparse
Boolean

輸入是否可能包含疏鬆標記法。 例如,包含 「5 2:6 4:3」 的資料清單示有 5 個數據行,而唯一的非零資料行是 2 和 4,分別具有 6 和 3 的值。 資料行索引是以零起始,因此資料行 2 和 4 代表第三欄和第 5 個數據行。 資料行可能也有密集值,後面接著以這種方式表示的疏鬆值。 例如,包含 「1 2 5 2:6 4:3」 的資料列代表兩個具有 1 和 2 值的密集資料行,後面接著 5 個疏鬆表示值為 0、0、6、0 和 3 的資料行。 疏鬆資料行的索引從 0 開始,即使 0 代表第三個數據行也一樣。

傳回

範例

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

namespace Samples.Dynamic.DataOperations
{
    public static class LoadingText
    {
        // This examples shows all the ways to load data with TextLoader.
        public static void Example()
        {
            // Create 5 data files to illustrate different loading methods.
            var dataFiles = new List<string>();
            var random = new Random(1);
            var dataDirectoryName = "DataDir";
            Directory.CreateDirectory(dataDirectoryName);
            for (int i = 0; i < 5; i++)
            {
                var fileName = Path.Combine(dataDirectoryName, $"Data_{i}.csv");
                dataFiles.Add(fileName);
                using (var fs = File.CreateText(fileName))
                {
                    // Write without header with 10 random columns, forcing
                    // approximately 80% of values to be 0.
                    for (int line = 0; line < 10; line++)
                    {
                        var sb = new StringBuilder();
                        for (int pos = 0; pos < 10; pos++)
                        {
                            var value = random.NextDouble();
                            sb.Append((value < 0.8 ? 0 : value).ToString() + '\t');
                        }
                        fs.WriteLine(sb.ToString(0, sb.Length - 1));
                    }
                }
            }

            // Create a TextLoader.
            var mlContext = new MLContext();
            var loader = mlContext.Data.CreateTextLoader(
                columns: new[]
                {
                    new TextLoader.Column("Features", DataKind.Single, 0, 9)
                },
                hasHeader: false
            );

            // Load a single file from path.
            var singleFileData = loader.Load(dataFiles[0]);
            PrintRowCount(singleFileData);

            // Expected Output:
            //   10


            // Load all 5 files from path.
            var multipleFilesData = loader.Load(dataFiles.ToArray());
            PrintRowCount(multipleFilesData);

            // Expected Output:
            //   50


            // Load all files using path wildcard.
            var multipleFilesWildcardData =
                loader.Load(Path.Combine(dataDirectoryName, "Data_*.csv"));
            PrintRowCount(multipleFilesWildcardData);

            // Expected Output:
            //   50


            // Create a TextLoader with user defined type.
            var loaderWithCustomType =
                mlContext.Data.CreateTextLoader<Data>(hasHeader: false);

            // Load a single file from path.
            var singleFileCustomTypeData = loaderWithCustomType.Load(dataFiles[0]);
            PrintRowCount(singleFileCustomTypeData);

            // Expected Output:
            //   10


            // Create a TextLoader with unknown column length to illustrate
            // how a data sample may be used to infer column size.
            var dataSample = new MultiFileSource(dataFiles[0]);
            var loaderWithUnknownLength = mlContext.Data.CreateTextLoader(
                columns: new[]
                {
                    new TextLoader.Column("Features",
                                          DataKind.Single,
                                          new[] { new TextLoader.Range(0, null) })
                },
                dataSample: dataSample
            );

            var dataWithInferredLength = loaderWithUnknownLength.Load(dataFiles[0]);
            var featuresColumn = dataWithInferredLength.Schema.GetColumnOrNull("Features");
            if (featuresColumn.HasValue)
                Console.WriteLine(featuresColumn.Value.ToString());

            // Expected Output:
            //   Features: Vector<Single, 10>
            //
            // ML.NET infers the correct length of 10 for the Features column,
            // which is of type Vector<Single>.

            PrintRowCount(dataWithInferredLength);

            // Expected Output:
            //   10


            // Save the data with 10 rows to a text file to illustrate the use of
            // sparse format.
            var sparseDataFileName = Path.Combine(dataDirectoryName, "saved_data.tsv");
            using (FileStream stream = new FileStream(sparseDataFileName, FileMode.Create))
                mlContext.Data.SaveAsText(singleFileData, stream);

            // Since there are many zeroes in the data, it will be saved in a sparse
            // representation to save disk space. The data may be forced to be saved
            // in a dense representation by setting forceDense to true. The sparse
            // data will look like the following:
            //
            //   10 7:0.943862259
            //   10 3:0.989767134
            //   10 0:0.949778438   8:0.823028445   9:0.886469543
            //
            // The sparse representation of the first row indicates that there are
            // 10 columns, the column 7 (8-th column) has value 0.943862259, and other
            // omitted columns have value 0.

            // Create a TextLoader that allows sparse input.
            var sparseLoader = mlContext.Data.CreateTextLoader(
                columns: new[]
                {
                    new TextLoader.Column("Features", DataKind.Single, 0, 9)
                },
                allowSparse: true
            );

            // Load the saved sparse data.
            var sparseData = sparseLoader.Load(sparseDataFileName);
            PrintRowCount(sparseData);

            // Expected Output:
            //   10


            // Create a TextLoader without any column schema using TextLoader.Options.
            // Since the sparse data file was saved with ML.NET, it has the schema
            // enoded in its header that the loader can understand:
            //
            // #@ TextLoader{
            // #@   sep=tab
            // #@   col=Features:R4:0-9
            // #@ }
            //
            // The schema syntax is unimportant since it is only used internally. In
            // short, it tells the loader that the values are separated by tabs, and
            // that columns 0-9 in the text file are to be read into one column named
            // "Features" of type Single (internal type R4).

            var options = new TextLoader.Options()
            {
                AllowSparse = true,
            };
            var dataSampleWithSchema = new MultiFileSource(sparseDataFileName);
            var sparseLoaderWithSchema =
                mlContext.Data.CreateTextLoader(options, dataSample: dataSampleWithSchema);

            // Load the saved sparse data.
            var sparseDataWithSchema = sparseLoaderWithSchema.Load(sparseDataFileName);
            PrintRowCount(sparseDataWithSchema);

            // Expected Output:
            //   10
        }

        private static void PrintRowCount(IDataView idv)
        {
            // IDataView is lazy so we need to iterate through it
            // to get the number of rows.
            long rowCount = 0;
            using (var cursor = idv.GetRowCursor(idv.Schema))
                while (cursor.MoveNext())
                    rowCount++;

            Console.WriteLine(rowCount);
        }

        private class Data
        {
            [LoadColumn(0, 9)]
            public float[] Features { get; set; }
        }
    }
}

適用於

CreateTextLoader<TInput>(DataOperationsCatalog, TextLoader+Options, IMultiStreamSource)

從資料模型類型推斷資料集架構,以建立文字載入器 TextLoader

public static Microsoft.ML.Data.TextLoader CreateTextLoader<TInput> (this Microsoft.ML.DataOperationsCatalog catalog, Microsoft.ML.Data.TextLoader.Options options, Microsoft.ML.Data.IMultiStreamSource dataSample = default);
static member CreateTextLoader : Microsoft.ML.DataOperationsCatalog * Microsoft.ML.Data.TextLoader.Options * Microsoft.ML.Data.IMultiStreamSource -> Microsoft.ML.Data.TextLoader
<Extension()>
Public Function CreateTextLoader(Of TInput) (catalog As DataOperationsCatalog, options As TextLoader.Options, Optional dataSample As IMultiStreamSource = Nothing) As TextLoader

類型參數

TInput

參數

options
TextLoader.Options

定義載入作業的設定。 定義載入作業的設定。 不需要指定 Columns 欄位,因為此方法會推斷資料行。

dataSample
IMultiStreamSource

資料範例的選擇性位置。 此範例可用來推斷資料行的相關資訊,例如位置名稱。

傳回

適用於

CreateTextLoader<TInput>(DataOperationsCatalog, Char, Boolean, IMultiStreamSource, Boolean, Boolean, Boolean)

從資料模型類型推斷資料集架構,以建立文字載入器 TextLoader

public static Microsoft.ML.Data.TextLoader CreateTextLoader<TInput> (this Microsoft.ML.DataOperationsCatalog catalog, char separatorChar = '\t', bool hasHeader = false, Microsoft.ML.Data.IMultiStreamSource dataSample = default, bool allowQuoting = false, bool trimWhitespace = false, bool allowSparse = false);
static member CreateTextLoader : Microsoft.ML.DataOperationsCatalog * char * bool * Microsoft.ML.Data.IMultiStreamSource * bool * bool * bool -> Microsoft.ML.Data.TextLoader
<Extension()>
Public Function CreateTextLoader(Of TInput) (catalog As DataOperationsCatalog, Optional separatorChar As Char = '\t', Optional hasHeader As Boolean = false, Optional dataSample As IMultiStreamSource = Nothing, Optional allowQuoting As Boolean = false, Optional trimWhitespace As Boolean = false, Optional allowSparse As Boolean = false) As TextLoader

類型參數

TInput

定義要載入之資料的架構。 使用以 (裝飾的公用欄位或屬性 LoadColumnAttribute ,以及可能) 的其他屬性,在載入資料的架構中指定資料行名稱和其資料類型。

參數

separatorChar
Char

資料行分隔符號。 預設值為 '\t'

hasHeader
Boolean

檔案是否有具有功能名稱的標頭。 提供 時, true 表示 中的 第一行將用於功能名稱,而呼叫 時 Load(IMultiStreamSource) ,將會略過第一行。 如果沒有提供, true 只要指出載入器應該在呼叫 時 Load(IMultiStreamSource) 略過第一行,但資料行不會有位置名稱批註。 這是因為會在建立載入器時建立輸出架構,而不是呼叫 時 Load(IMultiStreamSource)

dataSample
IMultiStreamSource

資料範例的選擇性位置。 如果存在,此範例可用來推斷位置名稱批註。

allowQuoting
Boolean

輸入是否可能包含雙引號值。 此參數用來區分輸入值中的分隔符號與實際分隔符號。 當 為 時 true ,雙引號內的分隔符號會視為輸入值的一部分。 當 為 時 false ,所有分隔符號,甚至是那些引號,都會被視為分隔新資料行。

trimWhitespace
Boolean

從行中移除尾端空白字元。

allowSparse
Boolean

輸入是否可能包含疏鬆標記法。 例如,包含 「5 2:6 4:3」 的資料清單示有 5 個數據行,而唯一的非零資料行是 2 和 4,分別具有 6 和 3 的值。 資料行索引是以零起始,因此資料行 2 和 4 代表第三欄和第 5 個數據行。 資料行可能也有密集值,後面接著以這種方式表示的疏鬆值。 例如,包含 「1 2 5 2:6 4:3」 的資料列代表兩個具有 1 和 2 值的密集資料行,後面接著 5 個疏鬆表示值為 0、0、6、0 和 3 的資料行。 疏鬆資料行的索引從 0 開始,即使 0 代表第三個數據行也一樣。

傳回

適用於