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)

ソース:
TextLoaderSaverCatalog.cs
ソース:
TextLoaderSaverCatalog.cs
ソース:
TextLoaderSaverCatalog.cs

テキスト ローダー 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.Rangeで定義Columnsスロットの数null推測できます。 サンプルが ML.NET のSaveAsText(DataOperationsCatalog, IDataView, Stream, Char, Boolean, Boolean, Boolean, Boolean)と共に保存されている場合、Columnsが指定されていない場合でもローダーが読み取ることができるスキーマ情報もヘッダーに含まれます。 ファイルで定義されているスキーマを使用するために、他のすべての TextLoader.Options は既定値のままにされます。

返品

適用対象

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

ソース:
TextLoaderSaverCatalog.cs
ソース:
TextLoaderSaverCatalog.cs
ソース:
TextLoaderSaverCatalog.cs

テキスト ローダー 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

ファイルに機能名を含むヘッダーがあるかどうか。 <see paramref="dataSample"></see> が指定されている場合、trueは、<see paramref="dataSample"></see> の最初の行が機能名に使用され、Load(IMultiStreamSource)が呼び出されると、最初の行がスキップされることを示します。 <see paramref="dataSample"></see>が指定されていない場合、trueは、Load(IMultiStreamSource)が呼び出されたときにローダーが最初の行をスキップする必要があることを示すだけですが、列にはスロット名の注釈はありません。 これは、 Load(IMultiStreamSource) が呼び出されたときではなく、ローダーの作成時に出力スキーマが作成されるためです。

dataSample
IMultiStreamSource

データ サンプルのオプションの場所。 このサンプルを使用して、スロット名の注釈 (存在する場合) と、 TextLoader.Range で定義された列内のスロット数 null 最大インデックス数を推測できます。 サンプルが ML.NET のSaveAsText(DataOperationsCatalog, IDataView, Stream, Char, Boolean, Boolean, Boolean, Boolean)と共に保存されている場合、columnsnullされている場合でもローダーが読み取ることができるスキーマ情報もヘッダーに含まれます。 ファイルで定義されているスキーマを使用するために、他のすべての引数は既定値のままにされます。

allowQuoting
Boolean

入力に二重引用符で囲まれた値を含めることができます。 このパラメーターは、入力値の区切り文字を実際の区切り記号と区別するために使用されます。 trueすると、二重引用符内の区切り記号が入力値の一部として扱われます。 falseすると、引用符内の区切り記号であっても、すべての区切り記号は新しい列を区切るものとして扱われます。

trimWhitespace
Boolean

行から末尾の空白を削除します。

allowSparse
Boolean

入力にスパース表現を含めるかどうか。 たとえば、"5 2:6 4:3" を含む行は 5 つの列があることを意味し、0 以外の列はそれぞれ 6 と 3 の列 2 と 4 です。 列インデックスは 0 から始まるため、列 2 と 4 は 3 番目と 5 番目の列を表します。 列には、この方法で表されるスパース値の後に密な値が続く場合もあります。 たとえば、"1 2 5 2:6 4:3" を含む行は、値が 1 と 2 の 2 つの密な列を表し、その後に値 0、0、6、0、3 の 5 つのスパース表現列が続きます。 スパース列のインデックスは 0 から始まりますが、0 は 3 番目の列を表します。

返品

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)

ソース:
TextLoaderSaverCatalog.cs
ソース:
TextLoaderSaverCatalog.cs
ソース:
TextLoaderSaverCatalog.cs

データ モデル型からデータセット スキーマを推論して、テキスト ローダー 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)

ソース:
TextLoaderSaverCatalog.cs
ソース:
TextLoaderSaverCatalog.cs
ソース:
TextLoaderSaverCatalog.cs

データ モデル型からデータセット スキーマを推論して、テキスト ローダー 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

ファイルに機能名を含むヘッダーがあるかどうか。 <see paramref="dataSample"></see> が指定されている場合、trueは、<see paramref="dataSample"></see> の最初の行が機能名に使用され、Load(IMultiStreamSource)が呼び出されると、最初の行がスキップされることを示します。 <see paramref="dataSample"></see>が指定されていない場合、trueは、Load(IMultiStreamSource)が呼び出されたときにローダーが最初の行をスキップする必要があることを示すだけですが、列にはスロット名の注釈はありません。 これは、 Load(IMultiStreamSource) が呼び出されたときではなく、ローダーの作成時に出力スキーマが作成されるためです。

dataSample
IMultiStreamSource

データ サンプルのオプションの場所。 サンプルは、スロット名の注釈が存在する場合に推論するために使用できます。

allowQuoting
Boolean

入力に二重引用符で囲まれた値を含めることができます。 このパラメーターは、入力値の区切り文字を実際の区切り記号と区別するために使用されます。 trueすると、二重引用符内の区切り記号が入力値の一部として扱われます。 falseすると、すべての区切り記号 (これらの白い引用符も含む) は、新しい列を区切るものとして扱われます。

trimWhitespace
Boolean

行から末尾の空白を削除します。

allowSparse
Boolean

入力にスパース表現を含めるかどうか。 たとえば、"5 2:6 4:3" を含む行は 5 つの列があることを意味し、0 以外の列はそれぞれ 6 と 3 の列 2 と 4 です。 列インデックスは 0 から始まるため、列 2 と 4 は 3 番目と 5 番目の列を表します。 列には、この方法で表されるスパース値の後に密な値が続く場合もあります。 たとえば、"1 2 5 2:6 4:3" を含む行は、値が 1 と 2 の 2 つの密な列を表し、その後に値 0、0、6、0、3 の 5 つのスパース表現列が続きます。 スパース列のインデックスは 0 から始まりますが、0 は 3 番目の列を表します。

返品

適用対象