Share via


ConversionsExtensionsCatalog.MapValue 方法

定義

多載

MapValue(TransformsCatalog+ConversionTransforms, String, IDataView, DataViewSchema+Column, DataViewSchema+Column, String)

建立 , ValueMappingEstimator 它會將實值型別轉換成索引鍵,從 指定索引鍵和 valueColumn 個別值的位置 keyColumn 載入要使用的 lookupMap 索引鍵。

MapValue<TInputType,TOutputType>(TransformsCatalog+ConversionTransforms, String, IEnumerable<KeyValuePair<TInputType,TOutputType[]>>, String)

建立 ValueMappingEstimator ,將實值型別轉換成索引鍵,載入要從 keyValuePairs 使用的索引鍵。

MapValue<TInputType,TOutputType>(TransformsCatalog+ConversionTransforms, String, IEnumerable<KeyValuePair<TInputType,TOutputType>>, String, Boolean)

建立 ValueMappingEstimator ,將實值型別轉換成索引鍵,載入要從 keyValuePairs 使用的索引鍵。

MapValue(TransformsCatalog+ConversionTransforms, String, IDataView, DataViewSchema+Column, DataViewSchema+Column, String)

建立 , ValueMappingEstimator 它會將實值型別轉換成索引鍵,從 指定索引鍵和 valueColumn 個別值的位置 keyColumn 載入要使用的 lookupMap 索引鍵。

public static Microsoft.ML.Transforms.ValueMappingEstimator MapValue (this Microsoft.ML.TransformsCatalog.ConversionTransforms catalog, string outputColumnName, Microsoft.ML.IDataView lookupMap, Microsoft.ML.DataViewSchema.Column keyColumn, Microsoft.ML.DataViewSchema.Column valueColumn, string inputColumnName = default);
static member MapValue : Microsoft.ML.TransformsCatalog.ConversionTransforms * string * Microsoft.ML.IDataView * Microsoft.ML.DataViewSchema.Column * Microsoft.ML.DataViewSchema.Column * string -> Microsoft.ML.Transforms.ValueMappingEstimator
<Extension()>
Public Function MapValue (catalog As TransformsCatalog.ConversionTransforms, outputColumnName As String, lookupMap As IDataView, keyColumn As DataViewSchema.Column, valueColumn As DataViewSchema.Column, Optional inputColumnName As String = Nothing) As ValueMappingEstimator

參數

catalog
TransformsCatalog.ConversionTransforms

轉換轉換的目錄

outputColumnName
String

轉換 inputColumnName 所產生的資料行名稱。 資料類型可以是數值、文字、布林值、 DateTimeDateTimeOffsetDataViewRowId 類型的基本類型或向量。

lookupMap
IDataView

的實例 IDataView ,其中包含 keyColumn 和 資料 valueColumn 行。

keyColumn
DataViewSchema.Column

中的 lookupMap 索引鍵資料行。

valueColumn
DataViewSchema.Column

中的 lookupMap 值資料行。

inputColumnName
String

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

傳回

範例

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

namespace Samples.Dynamic
{
    public static class MapValueIdvLookup
    {
        /// This example demonstrates the use of MapValue by mapping floats to
        /// strings, looking up the mapping in an IDataView. This is useful to map
        /// types to a grouping. 
        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() { Price = 3.14f },
                new DataPoint() { Price = 2000f },
                new DataPoint() { Price = 1.19f },
                new DataPoint() { Price = 2.17f },
                new DataPoint() { Price = 33.784f },

            };

            // Convert to IDataView
            var data = mlContext.Data.LoadFromEnumerable(rawData);

            // Create the lookup map data IEnumerable.   
            var lookupData = new[] {
                new LookupMap { Value = 3.14f, Category = "Low" },
                new LookupMap { Value = 1.19f , Category = "Low" },
                new LookupMap { Value = 2.17f , Category = "Low" },
                new LookupMap { Value = 33.784f, Category = "Medium" },
                new LookupMap { Value = 2000f, Category = "High"}

            };

            // Convert to IDataView
            var lookupIdvMap = mlContext.Data.LoadFromEnumerable(lookupData);

            // Constructs the ValueMappingEstimator making the ML.NET pipeline
            var pipeline = mlContext.Transforms.Conversion.MapValue("PriceCategory",
                lookupIdvMap, lookupIdvMap.Schema["Value"], lookupIdvMap.Schema[
                    "Category"], "Price");

            // Fits the ValueMappingEstimator and transforms the data converting the
            // Price to PriceCategory.
            IDataView transformedData = pipeline.Fit(data).Transform(data);

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

            Console.WriteLine($" Price   PriceCategory");
            foreach (var featureRow in features)
                Console.WriteLine($"{featureRow.Price}\t\t" +
                $"{featureRow.PriceCategory}");

            // TransformedData obtained post-transformation.
            //
            // Price        PriceCategory
            // 3.14            Low
            // 2000            High
            // 1.19            Low
            // 2.17            Low
            // 33.784          Medium
        }

        // Type for the IDataView that will be serving as the map
        private class LookupMap
        {
            public float Value { get; set; }
            public string Category { get; set; }
        }

        private class DataPoint
        {
            public float Price { get; set; }
        }

        private class TransformedData : DataPoint
        {
            public string PriceCategory { get; set; }
        }
    }
}

適用於

MapValue<TInputType,TOutputType>(TransformsCatalog+ConversionTransforms, String, IEnumerable<KeyValuePair<TInputType,TOutputType[]>>, String)

建立 ValueMappingEstimator ,將實值型別轉換成索引鍵,載入要從 keyValuePairs 使用的索引鍵。

public static Microsoft.ML.Transforms.ValueMappingEstimator<TInputType,TOutputType> MapValue<TInputType,TOutputType> (this Microsoft.ML.TransformsCatalog.ConversionTransforms catalog, string outputColumnName, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<TInputType,TOutputType[]>> keyValuePairs, string inputColumnName = default);
static member MapValue : Microsoft.ML.TransformsCatalog.ConversionTransforms * string * seq<System.Collections.Generic.KeyValuePair<'InputType, 'OutputType[]>> * string -> Microsoft.ML.Transforms.ValueMappingEstimator<'InputType, 'OutputType>
<Extension()>
Public Function MapValue(Of TInputType, TOutputType) (catalog As TransformsCatalog.ConversionTransforms, outputColumnName As String, keyValuePairs As IEnumerable(Of KeyValuePair(Of TInputType, TOutputType())), Optional inputColumnName As String = Nothing) As ValueMappingEstimator(Of TInputType, TOutputType)

類型參數

TInputType

索引鍵類型。

TOutputType

值型別。

參數

catalog
TransformsCatalog.ConversionTransforms

轉換轉換的目錄

outputColumnName
String

轉換 inputColumnName 所產生的資料行名稱。 資料類型可以是數值、文字、布林值、 DateTimeDateTimeOffsetDataViewRowId 型別的基本類型或向量,如 中所 TOutputType 指定。

keyValuePairs
IEnumerable<KeyValuePair<TInputType,TOutputType[]>>

指定要執行的對應。 索引鍵會對應至 中指定的 keyValuePairs 值。

inputColumnName
String

要轉換的資料行名稱。 如果設定為 null ,則會 outputColumnName 將 的值當做來源使用。 資料類型可以是數值、文字、布林值、 DateTimeDateTimeOffsetDataViewRowId 型別的基本類型或向量,如 中所 TInputType 指定。

傳回

ValueMappingEstimator<TInputType,TOutputType>

範例

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

namespace Samples.Dynamic
{
    public static class MapValueToArray
    {
        /// This example demonstrates the use of MapValue by mapping strings to
        /// array values, which allows for mapping data to numeric arrays. This
        /// functionality is useful when the generated column will serve as the
        /// Features column for a trainer. Most of the trainers take a numeric
        /// vector, as the Features column. In this example, we are mapping the
        /// Timeframe data to arbitrary integer arrays.
        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 = "0-4yrs" },
                new DataPoint() { Timeframe = "6-11yrs" },
                new DataPoint() { Timeframe = "12-25yrs" },
                new DataPoint() { Timeframe = "0-5yrs" },
                new DataPoint() { Timeframe = "12-25yrs" },
                new DataPoint() { Timeframe = "25+yrs" },
            };

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

            // Creating a list of key-value pairs to indicate the mapping between
            // the DataPoint values, and the arrays they should map to. 
            var timeframeMap = new Dictionary<string, int[]>();
            timeframeMap["0-4yrs"] = new int[] { 0, 5, 300 };
            timeframeMap["0-5yrs"] = new int[] { 0, 5, 300 };
            timeframeMap["6-11yrs"] = new int[] { 6, 11, 300 };
            timeframeMap["12-25yrs"] = new int[] { 12, 50, 300 };
            timeframeMap["25+yrs"] = new int[] { 12, 50, 300 };

            // Constructs the ValueMappingEstimator making the ML.NET pipeline.
            var pipeline = mlContext.Transforms.Conversion.MapValue("Features",
                timeframeMap, "Timeframe");

            // Fits the ValueMappingEstimator and transforms the data adding the
            // Features column.
            IDataView transformedData = pipeline.Fit(data).Transform(data);

            // Getting the resulting data as an IEnumerable.
            IEnumerable<TransformedData> featuresColumn = mlContext.Data
                .CreateEnumerable<TransformedData>(transformedData, reuseRowObject:
                false);

            Console.WriteLine($"Timeframe     Features");
            foreach (var featureRow in featuresColumn)
                Console.WriteLine($"{featureRow.Timeframe}\t\t " +
                $"{string.Join(",", featureRow.Features)}");

            // Timeframe      Features
            // 0-4yrs       0, 5, 300
            // 6-11yrs      6, 11, 300
            // 12-25yrs     12, 50, 300
            // 0-5yrs       0, 5, 300
            // 12-25yrs     12, 50,300
            // 25+yrs       12, 50, 300
        }

        public class DataPoint
        {
            public string Timeframe { get; set; }
        }

        public class TransformedData : DataPoint
        {
            public int[] Features { get; set; }
        }
    }
}

適用於

MapValue<TInputType,TOutputType>(TransformsCatalog+ConversionTransforms, String, IEnumerable<KeyValuePair<TInputType,TOutputType>>, String, Boolean)

建立 ValueMappingEstimator ,將實值型別轉換成索引鍵,載入要從 keyValuePairs 使用的索引鍵。

public static Microsoft.ML.Transforms.ValueMappingEstimator<TInputType,TOutputType> MapValue<TInputType,TOutputType> (this Microsoft.ML.TransformsCatalog.ConversionTransforms catalog, string outputColumnName, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<TInputType,TOutputType>> keyValuePairs, string inputColumnName = default, bool treatValuesAsKeyType = false);
static member MapValue : Microsoft.ML.TransformsCatalog.ConversionTransforms * string * seq<System.Collections.Generic.KeyValuePair<'InputType, 'OutputType>> * string * bool -> Microsoft.ML.Transforms.ValueMappingEstimator<'InputType, 'OutputType>
<Extension()>
Public Function MapValue(Of TInputType, TOutputType) (catalog As TransformsCatalog.ConversionTransforms, outputColumnName As String, keyValuePairs As IEnumerable(Of KeyValuePair(Of TInputType, TOutputType)), Optional inputColumnName As String = Nothing, Optional treatValuesAsKeyType As Boolean = false) As ValueMappingEstimator(Of TInputType, TOutputType)

類型參數

TInputType

索引鍵類型。

TOutputType

值型別。

參數

catalog
TransformsCatalog.ConversionTransforms

轉換轉換的目錄

outputColumnName
String

轉換 inputColumnName 所產生的資料行名稱。 輸出資料類型可以是數值、文字、布林值、 DateTimeDateTimeOffsetDataViewRowId 類型的基本類型或向量。

keyValuePairs
IEnumerable<KeyValuePair<TInputType,TOutputType>>

指定要執行的對應。 索引鍵會對應至 中指定的 keyValuePairs 值。

inputColumnName
String

要轉換的資料行名稱。 如果設定為 null ,則會 outputColumnName 將 的值當做來源使用。 輸入資料類型可以是數值、文字、布林值、 DateTimeDateTimeOffsetDataViewRowId 類型的基本類型或向量。

treatValuesAsKeyType
Boolean

是否要將值視為索引鍵。

傳回

ValueMappingEstimator<TInputType,TOutputType>

的實例 ValueMappingEstimator

範例

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

namespace Samples.Dynamic
{
    public static class MapValue
    {
        /// This example demonstrates the use of the ValueMappingEstimator by 
        /// mapping strings to other string values, or floats to strings. This is
        /// useful to map types to a category. 
        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 = "0-4yrs" , Score = 1 },
                new DataPoint() { Timeframe = "6-11yrs" , Score = 2 },
                new DataPoint() { Timeframe = "12-25yrs" , Score = 3 },
                new DataPoint() { Timeframe = "0-5yrs" , Score = 4 },
                new DataPoint() { Timeframe = "12-25yrs" , Score = 5 },
                new DataPoint() { Timeframe = "25+yrs" , Score = 5 },
            };

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

            // Construct the mapping to other strings for the Timeframe column.  
            var timeframeMap = new Dictionary<string, string>();
            timeframeMap["0-4yrs"] = "Short";
            timeframeMap["0-5yrs"] = "Short";
            timeframeMap["6-11yrs"] = "Medium";
            timeframeMap["12-25yrs"] = "Long";
            timeframeMap["25+yrs"] = "Long";

            // Construct the mapping of strings to keys(uints) for the Timeframe
            // column. 
            var timeframeKeyMap = new Dictionary<string, uint>();
            timeframeKeyMap["0-4yrs"] = 1;
            timeframeKeyMap["0-5yrs"] = 1;
            timeframeKeyMap["6-11yrs"] = 2;
            timeframeKeyMap["12-25yrs"] = 3;
            timeframeKeyMap["25+yrs"] = 3;

            // Construct the mapping of ints to strings for the Score column. 
            var scoreMap = new Dictionary<int, string>();
            scoreMap[1] = "Low";
            scoreMap[2] = "Low";
            scoreMap[3] = "Average";
            scoreMap[4] = "High";
            scoreMap[5] = "High";

            // Constructs the ML.net pipeline
            var pipeline = mlContext.Transforms.Conversion.MapValue(
                "TimeframeCategory", timeframeMap, "Timeframe").Append(mlContext.
                Transforms.Conversion.MapValue("ScoreCategory", scoreMap, "Score"))
                // on the MapValue below, the treatValuesAsKeyType is set to true.
                // The type of the Label column will be a KeyDataViewType type, 
                // and it can be used as input for trainers performing multiclass
                // classification.
                .Append(mlContext.Transforms.Conversion.MapValue("Label",
                timeframeKeyMap, "Timeframe", treatValuesAsKeyType: true));

            // 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   TimeframeCategory   Label    Score   " +
                "ScoreCategory");

            foreach (var featureRow in features)
                Console.WriteLine($"{featureRow.Timeframe}\t\t" +
                    $"{featureRow.TimeframeCategory}\t\t\t{featureRow.Label}\t\t" +
                    $"{featureRow.Score}\t{featureRow.ScoreCategory}");

            // TransformedData obtained post-transformation.
            //
            //  Timeframe   TimeframeCategory   Label    Score   ScoreCategory
            // 0-4yrs         Short              1       1       Low
            // 6-11yrs        Medium             2       2       Low
            // 12-25yrs       Long               3       3       Average
            // 0-5yrs         Short              1       4       High
            // 12-25yrs       Long               3       5       High
            // 25+yrs         Long               3       5       High
        }

        private class DataPoint
        {
            public string Timeframe { get; set; }
            public int Score { get; set; }
        }

        private class TransformedData : DataPoint
        {
            public string TimeframeCategory { get; set; }
            public string ScoreCategory { get; set; }
            public uint Label { get; set; }
        }
    }
}

適用於