共用方式為


CategoricalCatalog.OneHotEncoding 方法

定義

多載

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

建立 , OneHotEncodingEstimator 將 中指定的 columns 一或多個輸入文字資料行轉換成一個經常性編碼向量的多個資料行。

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

建立 OneHotEncodingEstimator ,將 所 inputColumnName 指定的輸入資料行轉換成名為 outputColumnName 之一熱編碼向量的資料行。

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

建立 , 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[]

輸入和輸出資料行的配對。 如果 為 、 和 ,輸出資料行的資料類型將是 的 SingleoutputKind 向量。 BinaryIndicatorBag 如果 outputKindKey ,則輸出資料行的資料類型將會是純量輸入資料行的索引鍵,或向量輸入資料行的向量。

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)

建立 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 資料行名稱。 如果 為 、 IndicatorBinary ,則此資料行的資料類型將是 的 SingleBag 向量。 outputKind 如果 outputKindKey ,則此資料行的資料類型將會是純量輸入資料行的索引鍵,或是向量輸入資料行的索引鍵向量。

inputColumnName
String

要轉換成一熱向量的資料行名稱。 如果設定為 null ,則會將 的值 outputColumnName 當做來源使用。 此資料行的資料類型可以是數值、文字、布林值或 的純量或 DateTimeOffset 向量。 DateTime

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

適用於