CategoricalCatalog.OneHotEncoding 方法

定义

重载

OneHotEncoding(TransformsCatalog+CategoricalTransforms, InputOutputColumnPair[], OneHotEncodingEstimator+OutputKind, Int32, ValueToKeyMappingEstimator+KeyOrdinality, IDataView)

Source:
CategoricalCatalog.cs
Source:
CategoricalCatalog.cs
Source:
CategoricalCatalog.cs

创建一个 OneHotEncodingEstimator,它将指定 columns 一个或多个输入文本列转换为一个热编码矢量的任意多列。

public static Microsoft.ML.Transforms.OneHotEncodingEstimator OneHotEncoding(this Microsoft.ML.TransformsCatalog.CategoricalTransforms catalog, Microsoft.ML.InputOutputColumnPair[] columns, Microsoft.ML.Transforms.OneHotEncodingEstimator.OutputKind outputKind = Microsoft.ML.Transforms.OneHotEncodingEstimator+OutputKind.Indicator, int maximumNumberOfKeys = 1000000, Microsoft.ML.Transforms.ValueToKeyMappingEstimator.KeyOrdinality keyOrdinality = Microsoft.ML.Transforms.ValueToKeyMappingEstimator+KeyOrdinality.ByOccurrence, Microsoft.ML.IDataView keyData = default);
static member OneHotEncoding : Microsoft.ML.TransformsCatalog.CategoricalTransforms * Microsoft.ML.InputOutputColumnPair[] * Microsoft.ML.Transforms.OneHotEncodingEstimator.OutputKind * int * Microsoft.ML.Transforms.ValueToKeyMappingEstimator.KeyOrdinality * Microsoft.ML.IDataView -> Microsoft.ML.Transforms.OneHotEncodingEstimator
<Extension()>
Public Function OneHotEncoding (catalog As TransformsCatalog.CategoricalTransforms, columns As InputOutputColumnPair(), Optional outputKind As OneHotEncodingEstimator.OutputKind = Microsoft.ML.Transforms.OneHotEncodingEstimator+OutputKind.Indicator, Optional maximumNumberOfKeys As Integer = 1000000, Optional keyOrdinality As ValueToKeyMappingEstimator.KeyOrdinality = Microsoft.ML.Transforms.ValueToKeyMappingEstimator+KeyOrdinality.ByOccurrence, Optional keyData As IDataView = Nothing) As OneHotEncodingEstimator

参数

catalog
TransformsCatalog.CategoricalTransforms

转换目录。

columns
InputOutputColumnPair[]

输入和输出列对。 输出列的数据类型将是 if BagoutputKind 的向量SingleIndicator并且BinaryKey如果是outputKind,则输出列的数据类型将是在标量输入列的情况下为键,或者在矢量输入列的情况下是键的向量。

outputKind
OneHotEncodingEstimator.OutputKind

输出类型:包(多集矢量)、Ind(指示器向量)、键(索引)或二进制编码指示器向量。

maximumNumberOfKeys
Int32

自动训练时要保留每个列的最大术语数。

keyOrdinality
ValueToKeyMappingEstimator.KeyOrdinality

向量化时应如何对项进行排序。 如果选择 ByOccurrence ,它们将按遇到的顺序排列。 如果 ByValue项目根据其默认比较进行排序,例如,文本排序将区分大小写(例如,“A”,然后“Z”,然后为“a”。

keyData
IDataView

指定编码的排序。 如果指定,这应该是单个列数据视图,并且键值将从该列获取。 如果未指定,将在拟合时根据输入数据确定排序。

返回

示例

using System;
using Microsoft.ML;

namespace Samples.Dynamic.Transforms.Categorical
{
    public static class OneHotEncodingMultiColumn
    {
        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", 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 encoding two columns
            // 'Education' and 'ZipCode'.
            var multiColumnKeyPipeline =
                mlContext.Transforms.Categorical.OneHotEncoding(
                    new[]
                    {
                        new InputOutputColumnPair("Education"),
                        new InputOutputColumnPair("ZipCode")
                    });

            // Fit and Transform data.
            IDataView transformedData =
                multiColumnKeyPipeline.Fit(data).Transform(data);

            var convertedData =
                mlContext.Data.CreateEnumerable<TransformedData>(transformedData,
                    true);

            Console.WriteLine(
                "One Hot Encoding of two columns 'Education' and 'ZipCode'.");

            // One Hot 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));

            // 1 0 0                   1 0
            // 1 0 0                   0 1
            // 0 1 0                   1 0
            // 0 1 0                   0 1
            // 0 0 1                   1 0
        }

        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; }
        }
    }
}

注解

如果将多个列传递到估算器,所有列都将在一次传递数据中进行处理。 因此,使用多个列指定一个估算器比指定多个估算器(每个估算器具有单个列)更有效。

适用于

OneHotEncoding(TransformsCatalog+CategoricalTransforms, String, String, OneHotEncodingEstimator+OutputKind, Int32, ValueToKeyMappingEstimator+KeyOrdinality, IDataView)

Source:
CategoricalCatalog.cs
Source:
CategoricalCatalog.cs
Source:
CategoricalCatalog.cs

创建一个 OneHotEncodingEstimator,它将指定的 inputColumnName 输入列转换为名为 outputColumnName一个热编码矢量的列。

public static Microsoft.ML.Transforms.OneHotEncodingEstimator OneHotEncoding(this Microsoft.ML.TransformsCatalog.CategoricalTransforms catalog, string outputColumnName, string inputColumnName = default, Microsoft.ML.Transforms.OneHotEncodingEstimator.OutputKind outputKind = Microsoft.ML.Transforms.OneHotEncodingEstimator+OutputKind.Indicator, int maximumNumberOfKeys = 1000000, Microsoft.ML.Transforms.ValueToKeyMappingEstimator.KeyOrdinality keyOrdinality = Microsoft.ML.Transforms.ValueToKeyMappingEstimator+KeyOrdinality.ByOccurrence, Microsoft.ML.IDataView keyData = default);
static member OneHotEncoding : Microsoft.ML.TransformsCatalog.CategoricalTransforms * string * string * Microsoft.ML.Transforms.OneHotEncodingEstimator.OutputKind * int * Microsoft.ML.Transforms.ValueToKeyMappingEstimator.KeyOrdinality * Microsoft.ML.IDataView -> Microsoft.ML.Transforms.OneHotEncodingEstimator
<Extension()>
Public Function OneHotEncoding (catalog As TransformsCatalog.CategoricalTransforms, outputColumnName As String, Optional inputColumnName As String = Nothing, Optional outputKind As OneHotEncodingEstimator.OutputKind = Microsoft.ML.Transforms.OneHotEncodingEstimator+OutputKind.Indicator, Optional maximumNumberOfKeys As Integer = 1000000, Optional keyOrdinality As ValueToKeyMappingEstimator.KeyOrdinality = Microsoft.ML.Transforms.ValueToKeyMappingEstimator+KeyOrdinality.ByOccurrence, Optional keyData As IDataView = Nothing) As OneHotEncodingEstimator

参数

catalog
TransformsCatalog.CategoricalTransforms

转换目录。

outputColumnName
String

由转换 inputColumnName生成的列的名称。 此列的数据类型将是 if BagoutputKind 的向量SingleIndicator并且BinaryKey如果是outputKind,则对于标量输入列或矢量输入列,此列的数据类型将是一个键。如果为矢量输入列,则为键的键。

inputColumnName
String

要转换为单热向量的列的名称。 如果设置为 nulloutputColumnName 则该值将用作源。 此列的数据类型可以是数值、文本、布尔 DateTime 值或 DateTimeOffset

outputKind
OneHotEncodingEstimator.OutputKind

输出类型:包(多集向量)、指示器(指示器向量)、键(索引)或二进制编码指示器向量。

maximumNumberOfKeys
Int32

自动训练时要保留每个列的最大术语数。

keyOrdinality
ValueToKeyMappingEstimator.KeyOrdinality

向量化时应如何对项进行排序。 如果选择 ByOccurrence ,它们将按遇到的顺序排列。 如果 ByValue项目根据其默认比较进行排序,例如,文本排序将区分大小写(例如,“A”,然后“Z”,然后为“a”。

keyData
IDataView

指定编码的排序。 如果指定,这应该是单个列数据视图,并且键值将从该列获取。 如果未指定,将在拟合时根据输入数据确定排序。

返回

示例

using System;
using Microsoft.ML;
using Microsoft.ML.Data;
using Microsoft.ML.Transforms;

namespace Samples.Dynamic.Transforms.Categorical
{
    public static class OneHotEncoding
    {
        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 IDataView.
            IDataView data = mlContext.Data.LoadFromEnumerable(samples);

            // A pipeline for one hot encoding the Education column.
            var pipeline = mlContext.Transforms.Categorical.OneHotEncoding(
                "EducationOneHotEncoded", "Education");

            // Fit and transform the data.
            IDataView oneHotEncodedData = pipeline.Fit(data).Transform(data);

            PrintDataColumn(oneHotEncodedData, "EducationOneHotEncoded");

            // We have 3 slots because there are three categories in the
            // 'Education' column.

            // 1 0 0
            // 1 0 0
            // 0 1 0
            // 0 1 0
            // 0 0 1

            // A pipeline for one hot encoding the Education column (using keying).
            var keyPipeline = mlContext.Transforms.Categorical.OneHotEncoding(
                "EducationOneHotEncoded", "Education",
                OneHotEncodingEstimator.OutputKind.Key);

            // Fit and Transform data.
            oneHotEncodedData = keyPipeline.Fit(data).Transform(data);

            var keyEncodedColumn =
                oneHotEncodedData.GetColumn<uint>("EducationOneHotEncoded");

            Console.WriteLine(
                "One Hot Encoding of single column 'Education', with key type " +
                "output.");

            // One Hot Encoding of single column 'Education', with key type output.

            foreach (uint element in keyEncodedColumn)
                Console.WriteLine(element);

            // 1
            // 1
            // 2
            // 2
            // 3
        }

        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; }
        }
    }
}

适用于