BinaryLoaderSaverCatalog.SaveAsBinary 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将该文件 IDataView 保存到二进制流中。
public static void SaveAsBinary (this Microsoft.ML.DataOperationsCatalog catalog, Microsoft.ML.IDataView data, System.IO.Stream stream, bool keepHidden = false);
static member SaveAsBinary : Microsoft.ML.DataOperationsCatalog * Microsoft.ML.IDataView * System.IO.Stream * bool -> unit
<Extension()>
Public Sub SaveAsBinary (catalog As DataOperationsCatalog, data As IDataView, stream As Stream, Optional keepHidden As Boolean = false)
参数
- catalog
- DataOperationsCatalog
目录。
- data
- IDataView
要保存的数据视图。
- stream
- Stream
要写入的流。
- keepHidden
- Boolean
是否在数据集中保留隐藏列。
示例
using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.ML;
namespace Samples.Dynamic
{
public static class SaveAndLoadFromBinary
{
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. Setting the seed to a fixed number
// in this example to make outputs deterministic.
var mlContext = new MLContext(seed: 0);
// Create a list of training data points.
var dataPoints = new List<DataPoint>()
{
new DataPoint(){ Label = 0, Features = 4},
new DataPoint(){ Label = 0, Features = 5},
new DataPoint(){ Label = 0, Features = 6},
new DataPoint(){ Label = 1, Features = 8},
new DataPoint(){ Label = 1, Features = 9},
};
// Convert the list of data points to an IDataView object, which is
// consumable by ML.NET API.
IDataView data = mlContext.Data.LoadFromEnumerable(dataPoints);
// Create a FileStream object and write the IDataView to it as a binary
// IDV file.
using (FileStream stream = new FileStream("data.idv", FileMode.Create))
mlContext.Data.SaveAsBinary(data, stream);
// Create an IDataView object by loading the binary IDV file.
IDataView loadedData = mlContext.Data.LoadFromBinary("data.idv");
// Inspect the data that is loaded from the previously saved binary file
var loadedDataEnumerable = mlContext.Data
.CreateEnumerable<DataPoint>(loadedData, reuseRowObject: false);
foreach (DataPoint row in loadedDataEnumerable)
Console.WriteLine($"{row.Label}, {row.Features}");
// Preview of the loaded data.
// 0, 4
// 0, 5
// 0, 6
// 1, 8
// 1, 9
}
// Example with label and feature values. A data set is a collection of such
// examples.
private class DataPoint
{
public float Label { get; set; }
public float Features { get; set; }
}
}
}