CategoricalCatalog.OneHotHashEncoding 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
多載
OneHotHashEncoding(TransformsCatalog+CategoricalTransforms, InputOutputColumnPair[], OneHotEncodingEstimator+OutputKind, Int32, UInt32, Boolean, Int32) |
建立 OneHotHashEncodingEstimator ,將 所 |
OneHotHashEncoding(TransformsCatalog+CategoricalTransforms, String, String, OneHotEncodingEstimator+OutputKind, Int32, UInt32, Boolean, Int32) |
建立 OneHotHashEncodingEstimator ,將 所 |
OneHotHashEncoding(TransformsCatalog+CategoricalTransforms, InputOutputColumnPair[], OneHotEncodingEstimator+OutputKind, Int32, UInt32, Boolean, Int32)
建立 OneHotHashEncodingEstimator ,將 所 columns
指定的一或多個輸入文字資料行轉換成雜湊型單熱編碼向量的數目。
public static Microsoft.ML.Transforms.OneHotHashEncodingEstimator OneHotHashEncoding (this Microsoft.ML.TransformsCatalog.CategoricalTransforms catalog, Microsoft.ML.InputOutputColumnPair[] columns, Microsoft.ML.Transforms.OneHotEncodingEstimator.OutputKind outputKind = Microsoft.ML.Transforms.OneHotEncodingEstimator+OutputKind.Indicator, int numberOfBits = 16, uint seed = 314489979, bool useOrderedHashing = true, int maximumNumberOfInverts = 0);
static member OneHotHashEncoding : Microsoft.ML.TransformsCatalog.CategoricalTransforms * Microsoft.ML.InputOutputColumnPair[] * Microsoft.ML.Transforms.OneHotEncodingEstimator.OutputKind * int * uint32 * bool * int -> Microsoft.ML.Transforms.OneHotHashEncodingEstimator
<Extension()>
Public Function OneHotHashEncoding (catalog As TransformsCatalog.CategoricalTransforms, columns As InputOutputColumnPair(), Optional outputKind As OneHotEncodingEstimator.OutputKind = Microsoft.ML.Transforms.OneHotEncodingEstimator+OutputKind.Indicator, Optional numberOfBits As Integer = 16, Optional seed As UInteger = 314489979, Optional useOrderedHashing As Boolean = true, Optional maximumNumberOfInverts As Integer = 0) As OneHotHashEncodingEstimator
參數
轉換目錄
- columns
- InputOutputColumnPair[]
輸入和輸出資料行的配對。 如果 為 、 和 ,輸出資料行的資料類型將是 的 SingleoutputKind
向量。 BinaryIndicatorBag
如果 outputKind
為 Key ,則輸出資料行的資料類型將會是純量輸入資料行的索引鍵,或向量輸入資料行的向量。
- outputKind
- OneHotEncodingEstimator.OutputKind
轉換模式。
- numberOfBits
- Int32
要雜湊到的位數。 必須介於 1 到 30 之間,包含。
- seed
- UInt32
雜湊種子。
- useOrderedHashing
- Boolean
每個字詞的位置是否應該包含在雜湊中。
- maximumNumberOfInverts
- Int32
在雜湊期間,我們會在原始值與產生的雜湊值之間串連對應。
原始值的文字表示會儲存在新資料行的中繼資料位置名稱中。 因此,雜湊可以將許多初始值對應至一個。
maximumNumberOfInverts
會指定對應至應保留之雜湊的相異輸入值數目上限。
0 不會保留任何輸入值。 -1 會保留與每個雜湊對應的所有輸入值。
傳回
範例
using System;
using Microsoft.ML;
namespace Samples.Dynamic.Transforms.Categorical
{
public static class OneHotHashEncodingMultiColumn
{
public static void Example()
{
// Create a new ML context for ML.NET operations. It can be used for
// exception tracking and logging as well as the source of randomness.
var mlContext = new MLContext();
// Get a small dataset as an IEnumerable.
var samples = new[]
{
new DataPoint {Education = "0-5yrs", ZipCode = "98005"},
new DataPoint {Education = "0-5yrs", ZipCode = "98052"},
new DataPoint {Education = "6-11yrs", ZipCode = "98005"},
new DataPoint {Education = "6-11yrs", ZipCode = "98052"},
new DataPoint {Education = "11-15yrs", ZipCode = "98005"}
};
// Convert training data to IDataView.
IDataView data = mlContext.Data.LoadFromEnumerable(samples);
// Multi column example: A pipeline for one hot has encoding two
// columns 'Education' and 'ZipCode'.
var multiColumnKeyPipeline =
mlContext.Transforms.Categorical.OneHotHashEncoding(
new[]
{
new InputOutputColumnPair("Education"),
new InputOutputColumnPair("ZipCode")
},
numberOfBits: 3);
// Fit and Transform the data.
IDataView transformedData =
multiColumnKeyPipeline.Fit(data).Transform(data);
var convertedData =
mlContext.Data.CreateEnumerable<TransformedData>(transformedData,
true);
Console.WriteLine(
"One Hot Hash Encoding of two columns 'Education' and 'ZipCode'.");
// One Hot Hash Encoding of two columns 'Education' and 'ZipCode'.
foreach (TransformedData item in convertedData)
Console.WriteLine("{0}\t\t\t{1}", string.Join(" ", item.Education),
string.Join(" ", item.ZipCode));
// We have 8 slots, because we used numberOfBits = 3.
// 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1
// 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0
// 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1
// 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0
// 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1
}
private class DataPoint
{
public string Education { get; set; }
public string ZipCode { get; set; }
}
private class TransformedData
{
public float[] Education { get; set; }
public float[] ZipCode { get; set; }
}
}
}
備註
如果將多個資料行傳遞至估算器,所有資料行都會在單一傳遞資料中處理。 因此,使用許多資料行來指定一個估算器比使用單一資料行來指定許多估算器更有效率。
適用於
OneHotHashEncoding(TransformsCatalog+CategoricalTransforms, String, String, OneHotEncodingEstimator+OutputKind, Int32, UInt32, Boolean, Int32)
建立 OneHotHashEncodingEstimator ,將 所 inputColumnName
指定的文字資料行轉換成雜湊型單熱編碼向量資料行,名為 outputColumnName
。
public static Microsoft.ML.Transforms.OneHotHashEncodingEstimator OneHotHashEncoding (this Microsoft.ML.TransformsCatalog.CategoricalTransforms catalog, string outputColumnName, string inputColumnName = default, Microsoft.ML.Transforms.OneHotEncodingEstimator.OutputKind outputKind = Microsoft.ML.Transforms.OneHotEncodingEstimator+OutputKind.Indicator, int numberOfBits = 16, uint seed = 314489979, bool useOrderedHashing = true, int maximumNumberOfInverts = 0);
static member OneHotHashEncoding : Microsoft.ML.TransformsCatalog.CategoricalTransforms * string * string * Microsoft.ML.Transforms.OneHotEncodingEstimator.OutputKind * int * uint32 * bool * int -> Microsoft.ML.Transforms.OneHotHashEncodingEstimator
<Extension()>
Public Function OneHotHashEncoding (catalog As TransformsCatalog.CategoricalTransforms, outputColumnName As String, Optional inputColumnName As String = Nothing, Optional outputKind As OneHotEncodingEstimator.OutputKind = Microsoft.ML.Transforms.OneHotEncodingEstimator+OutputKind.Indicator, Optional numberOfBits As Integer = 16, Optional seed As UInteger = 314489979, Optional useOrderedHashing As Boolean = true, Optional maximumNumberOfInverts As Integer = 0) As OneHotHashEncodingEstimator
參數
轉換目錄。
- outputColumnName
- String
轉換所產生的 inputColumnName
資料行名稱。
如果 為 、 Indicator 和 Binary ,則此資料行的資料類型將是 的 SingleBag 向量。 outputKind
如果 outputKind
為 Key ,則此資料行的資料類型將會是純量輸入資料行的索引鍵,或是向量輸入資料行的索引鍵向量。
- inputColumnName
- String
要轉換的資料行名稱。 如果設定為 null
,則會將 的值 outputColumnName
當做來源使用。
此資料行的資料類型可以是數值、文字、布林 DateTime 值或 的純量或 DateTimeOffset 向量。
- outputKind
- OneHotEncodingEstimator.OutputKind
轉換模式。
- numberOfBits
- Int32
要雜湊到的位數。 必須介於 1 到 30 之間,包含。
- seed
- UInt32
雜湊種子。
- useOrderedHashing
- Boolean
每個字詞的位置是否應該包含在雜湊中。
- maximumNumberOfInverts
- Int32
在雜湊期間,我們會在原始值與產生的雜湊值之間串連對應。
原始值的文字表示會儲存在新資料行的中繼資料位置名稱中。因此,雜湊可以將許多初始值對應至一個。
maximumNumberOfInverts
會指定對應至應保留之雜湊的相異輸入值數目上限。
0 不會保留任何輸入值。 -1 會保留與每個雜湊對應的所有輸入值。
傳回
範例
using System;
using Microsoft.ML;
using Microsoft.ML.Data;
using Microsoft.ML.Transforms;
namespace Samples.Dynamic.Transforms.Categorical
{
public static class OneHotHashEncoding
{
public static void Example()
{
// Create a new ML context for ML.NET operations. It can be used for
// exception tracking and logging as well as the source of randomness.
var mlContext = new MLContext();
// Create a small dataset as an IEnumerable.
var samples = new[]
{
new DataPoint {Education = "0-5yrs"},
new DataPoint {Education = "0-5yrs"},
new DataPoint {Education = "6-11yrs"},
new DataPoint {Education = "6-11yrs"},
new DataPoint {Education = "11-15yrs"}
};
// Convert training data to an IDataView.
IDataView data = mlContext.Data.LoadFromEnumerable(samples);
// A pipeline for one hot hash encoding the 'Education' column.
var pipeline = mlContext.Transforms.Categorical.OneHotHashEncoding(
"EducationOneHotHashEncoded", "Education", numberOfBits: 3);
// Fit and transform the data.
IDataView hashEncodedData = pipeline.Fit(data).Transform(data);
PrintDataColumn(hashEncodedData, "EducationOneHotHashEncoded");
// We have 8 slots, because we used numberOfBits = 3.
// 0 0 0 1 0 0 0 0
// 0 0 0 1 0 0 0 0
// 0 0 0 0 1 0 0 0
// 0 0 0 0 1 0 0 0
// 0 0 0 0 0 0 0 1
// A pipeline for one hot hash encoding the 'Education' column
// (using keying strategy).
var keyPipeline = mlContext.Transforms.Categorical.OneHotHashEncoding(
"EducationOneHotHashEncoded", "Education",
OneHotEncodingEstimator.OutputKind.Key, 3);
// Fit and transform the data.
IDataView hashKeyEncodedData = keyPipeline.Fit(data).Transform(data);
// Get the data of the newly created column for inspecting.
var keyEncodedColumn =
hashKeyEncodedData.GetColumn<uint>("EducationOneHotHashEncoded");
Console.WriteLine(
"One Hot Hash Encoding of single column 'Education', with key " +
"type output.");
// One Hot Hash Encoding of single column 'Education', with key type output.
foreach (uint element in keyEncodedColumn)
Console.WriteLine(element);
// 4
// 4
// 5
// 5
// 8
}
private static void PrintDataColumn(IDataView transformedData,
string columnName)
{
var countSelectColumn = transformedData.GetColumn<float[]>(
transformedData.Schema[columnName]);
foreach (var row in countSelectColumn)
{
for (var i = 0; i < row.Length; i++)
Console.Write($"{row[i]}\t");
Console.WriteLine();
}
}
private class DataPoint
{
public string Education { get; set; }
}
}
}