TextLoaderSaverCatalog.CreateTextLoader 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
多載
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
參數
- catalog
- DataOperationsCatalog
- options
- TextLoader.Options
定義負載操作的設定。
- dataSample
- IMultiStreamSource
資料樣本的可選位置。 若有此範例,可用來推斷槽名註解,以及定義中TextLoader.Range以最大索引為null的槽Columns數。
若樣本已與 ML.NET SaveAsText(DataOperationsCatalog, IDataView, Stream, Char, Boolean, Boolean, Boolean, Boolean)儲存,標頭中也會包含載入器即使未指定也能讀取Columns的結構資訊。
為了使用檔案中定義的結構,其他 TextLoader.Options 所有 Sould 都保留其預設值。
傳回
適用於
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
參數
- catalog
- DataOperationsCatalog
- columns
- TextLoader.Column[]
定義結構的欄位 TextLoader.Column 陣列。
- separatorChar
- Char
字元用於資料點間的分隔符。 預設情況下,Tab 字元會被用作分隔符。
- 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.Rangenull最大索引定義欄位的插槽數量。
若樣本已與 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 欄代表第 3 和第 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
參數
- catalog
- DataOperationsCatalog
- options
- TextLoader.Options
定義負載操作的設定。 定義負載操作的設定。 無需指定欄位欄位,因為欄位會由此方法推斷。
- 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 (以及可能其他屬性)的公用欄位或屬性,在載入資料的結構中指定欄位名稱及其資料型態。
參數
- catalog
- DataOperationsCatalog
- 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 欄,唯一非零的是第 2 和第 4 欄,分別值為 6 和 3。 欄位索引為零基,因此第 2 和第 4 欄代表第 3 和第 5 欄。 欄位也可能有密集值,接著是稀疏值,並以這種方式表示。 例如,包含「1 2 5 2:6 4:3」的列代表兩個密集欄位,值分別為 1 和 2,接著是 5 個稀疏表示的欄位,分別值為 0、0、6、0 和 3。 稀疏欄的索引從 0 開始,儘管 0 代表第三欄。