다음을 통해 공유


ConversionsExtensionsCatalog.MapKeyToVector 메서드

정의

오버로드

MapKeyToVector(TransformsCatalog+ConversionTransforms, InputOutputColumnPair[], Boolean)

Create a KeyToVectorMappingEstimator, which maps the value of a key into a floating point vector representing the value.

MapKeyToVector(TransformsCatalog+ConversionTransforms, String, String, Boolean)

Create a KeyToVectorMappingEstimator, which maps the value of a key into a floating point vector representing the value.

MapKeyToVector(TransformsCatalog+ConversionTransforms, InputOutputColumnPair[], Boolean)

Create a KeyToVectorMappingEstimator, which maps the value of a key into a floating point vector representing the value.

public static Microsoft.ML.Transforms.KeyToVectorMappingEstimator MapKeyToVector (this Microsoft.ML.TransformsCatalog.ConversionTransforms catalog, Microsoft.ML.InputOutputColumnPair[] columns, bool outputCountVector = false);
static member MapKeyToVector : Microsoft.ML.TransformsCatalog.ConversionTransforms * Microsoft.ML.InputOutputColumnPair[] * bool -> Microsoft.ML.Transforms.KeyToVectorMappingEstimator
<Extension()>
Public Function MapKeyToVector (catalog As TransformsCatalog.ConversionTransforms, columns As InputOutputColumnPair(), Optional outputCountVector As Boolean = false) As KeyToVectorMappingEstimator

매개 변수

catalog
TransformsCatalog.ConversionTransforms

변환 변환의 카탈로그입니다.

columns
InputOutputColumnPair[]

입력 및 출력 열입니다. 새 열의 데이터 형식은 원래 값을 나타내는 벡터 Single 입니다.

outputCountVector
Boolean

여러 표시기 벡터를 연결하지 않고 단일 개수 벡터로 결합할지 여부입니다. 이는 입력 열이 키 벡터인 경우에만 관련이 있습니다.

반환

예제

using System;
using System.Collections.Generic;
using Microsoft.ML;
using Microsoft.ML.Data;

namespace Samples.Dynamic
{
    public class MapKeyToVectorMultiColumn
    {
        /// This example demonstrates the use of MapKeyToVector by mapping keys to
        /// floats[] for multiple columns at once. Because the ML.NET KeyType maps
        /// the missing value to zero, counting starts at 1, so the uint values
        /// converted to KeyTypes will appear skewed by one.
        /// See https://github.com/dotnet/machinelearning/blob/main/docs/code/IDataViewTypeSystem.md#key-types
        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 rawData = new[] {
                new DataPoint() { Timeframe = 9, Category = 5 },
                new DataPoint() { Timeframe = 8, Category = 4 },
                new DataPoint() { Timeframe = 8, Category = 4 },
                new DataPoint() { Timeframe = 9, Category = 3 },
                new DataPoint() { Timeframe = 2, Category = 3 },
                new DataPoint() { Timeframe = 3, Category = 5 }
            };

            var data = mlContext.Data.LoadFromEnumerable(rawData);

            // Constructs the ML.net pipeline
            var pipeline = mlContext.Transforms.Conversion.MapKeyToVector(new[]{
                    new InputOutputColumnPair ("TimeframeVector", "Timeframe"),
                    new InputOutputColumnPair ("CategoryVector", "Category")
            });

            // Fits the pipeline to the data.
            IDataView transformedData = pipeline.Fit(data).Transform(data);

            // Getting the resulting data as an IEnumerable.
            // This will contain the newly created columns.
            IEnumerable<TransformedData> features = mlContext.Data.CreateEnumerable<
                TransformedData>(transformedData, reuseRowObject: false);

            Console.WriteLine($" Timeframe           TimeframeVector         " +
                $"Category    CategoryVector");

            foreach (var featureRow in features)
                Console.WriteLine(featureRow.Timeframe + "     " +
                    string.Join(',', featureRow.TimeframeVector) + "   " +
                    featureRow.Category + "      " +
                    string.Join(',', featureRow.CategoryVector));

            // TransformedData obtained post-transformation.
            //
            // Timeframe          TimeframeVector    Category    CategoryVector
            //  10              0,0,0,0,0,0,0,0,0,1       6          0,0,0,0,0
            //  9               0,0,0,0,0,0,0,0,1,0       5          0,0,0,0,1
            //  9               0,0,0,0,0,0,0,0,1,0       5          0,0,0,0,1
            //  10              0,0,0,0,0,0,0,0,0,1       4          0,0,0,1,0
            //  3               0,0,1,0,0,0,0,0,0,0       4          0,0,0,1,0
            //  4               0,0,0,1,0,0,0,0,0,0       6          0,0,0,0,0
        }

        private class DataPoint
        {
            // The maximal value used is 9; but since 0 is reserved for missing
            // value, we set the count to 10.
            [KeyType(10)]
            public uint Timeframe { get; set; }

            [KeyType(6)]
            public uint Category { get; set; }

        }

        private class TransformedData : DataPoint
        {
            public float[] TimeframeVector { get; set; }
            public float[] CategoryVector { get; set; }
        }
    }
}

설명

이 변환은 여러 키 열에서 작동할 수 있습니다.

적용 대상

MapKeyToVector(TransformsCatalog+ConversionTransforms, String, String, Boolean)

Create a KeyToVectorMappingEstimator, which maps the value of a key into a floating point vector representing the value.

public static Microsoft.ML.Transforms.KeyToVectorMappingEstimator MapKeyToVector (this Microsoft.ML.TransformsCatalog.ConversionTransforms catalog, string outputColumnName, string inputColumnName = default, bool outputCountVector = false);
static member MapKeyToVector : Microsoft.ML.TransformsCatalog.ConversionTransforms * string * string * bool -> Microsoft.ML.Transforms.KeyToVectorMappingEstimator
<Extension()>
Public Function MapKeyToVector (catalog As TransformsCatalog.ConversionTransforms, outputColumnName As String, Optional inputColumnName As String = Nothing, Optional outputCountVector As Boolean = false) As KeyToVectorMappingEstimator

매개 변수

catalog
TransformsCatalog.ConversionTransforms

변환 변환의 카탈로그입니다.

outputColumnName
String

의 변환에서 생성된 열의 inputColumnName이름입니다. 데이터 형식은 입력 값을 나타내는 벡터 Single 입니다.

inputColumnName
String

변환할 열의 이름입니다. 이 값으로 null설정하면 해당 값이 outputColumnName 원본으로 사용됩니다. 이 변환은 키를 통해 작동합니다.

outputCountVector
Boolean

여러 표시기 벡터를 연결하지 않고 단일 개수 벡터로 결합할지 여부입니다. 이는 입력 열이 키 벡터인 경우에만 관련이 있습니다.

반환

예제

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.ML;
using Microsoft.ML.Data;

namespace Samples.Dynamic
{
    class MapKeyToVector
    {
        /// This example demonstrates the use of MapKeyToVector by mapping keys to
        /// floats[]. Because the ML.NET KeyType maps the missing value to zero,
        /// counting starts at 1, so the uint values converted to KeyTypes will
        /// appear skewed by one. See https://github.com/dotnet/machinelearning/blob/main/docs/code/IDataViewTypeSystem.md#key-types
        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 rawData = new[] {
                new DataPoint() { Timeframe = 8, PartA=1, PartB=2},
                new DataPoint() { Timeframe = 7, PartA=2, PartB=1},
                new DataPoint() { Timeframe = 8, PartA=3, PartB=2},
                new DataPoint() { Timeframe = 3, PartA=3, PartB=3}
            };

            var data = mlContext.Data.LoadFromEnumerable(rawData);

            // First transform just maps key type to indicator vector. i.e. it's
            // produces vector filled with zeros with size of key cardinality and
            // set 1 to corresponding key's value index in that array. After that we
            // concatenate two columns with single int values into vector of ints.
            // Third transform will create vector of keys, where key type is shared
            // across whole vector. Forth transform output data as count vector and
            // that vector would have size equal to shared key type cardinality and
            // put key counts to corresponding indexes in array. Fifth transform
            // output indicator vector for each key and concatenate them together.
            // Result vector would be size of key cardinality multiplied by size of
            // original vector.
            var pipeline = mlContext.Transforms.Conversion.MapKeyToVector(
                "TimeframeVector", "Timeframe")
                .Append(mlContext.Transforms.Concatenate("Parts", "PartA", "PartB"))
                .Append(mlContext.Transforms.Conversion.MapValueToKey("Parts"))
                .Append(mlContext.Transforms.Conversion.MapKeyToVector(
                    "PartsCount", "Parts", outputCountVector: true))
                .Append(mlContext.Transforms.Conversion.MapKeyToVector(
                    "PartsNoCount", "Parts"));

            // Fits the pipeline to the data.
            IDataView transformedData = pipeline.Fit(data).Transform(data);

            // Getting the resulting data as an IEnumerable.
            // This will contain the newly created columns.
            IEnumerable<TransformedData> features = mlContext.Data.CreateEnumerable<
                TransformedData>(transformedData, reuseRowObject: false);

            Console.WriteLine("Timeframe  TimeframeVector    PartsCount  " +
                "PartsNoCount");

            foreach (var featureRow in features)
                Console.WriteLine(featureRow.Timeframe + "          " +
                    string.Join(',', featureRow.TimeframeVector.Select(x => x)) + "  "
                    + string.Join(',', featureRow.PartsCount.Select(x => x)) +
                    "       " + string.Join(',', featureRow.PartsNoCount.Select(
                    x => x)));

            // Expected output:
            //  Timeframe  TimeframeVector    PartsCount  PartsNoCount
            //  9          0,0,0,0,0,0,0,0,1  1,1,0       1,0,0,0,1,0
            //  8          0,0,0,0,0,0,0,1,0  1,1,0       0,1,0,1,0,0
            //  9          0,0,0,0,0,0,0,0,1  0,1,1       0,0,1,0,1,0
            //  4          0,0,0,1,0,0,0,0,0  0,0,2       0,0,1,0,0,1
        }

        private class DataPoint
        {
            [KeyType(9)]
            public uint Timeframe { get; set; }
            public int PartA { get; set; }
            public int PartB { get; set; }

        }

        private class TransformedData : DataPoint
        {
            public float[] TimeframeVector { get; set; }
            public float[] PartsCount { get; set; }
            public float[] PartsNoCount { get; set; }
        }
    }
}

적용 대상