|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using Microsoft.ML.Data; |
| 4 | + |
| 5 | +namespace Microsoft.ML.Samples.Dynamic |
| 6 | +{ |
| 7 | + public static class MapValue |
| 8 | + { |
| 9 | + class DataPoint |
| 10 | + { |
| 11 | + public string Timeframe { get; set; } |
| 12 | + public int Score { get; set; } |
| 13 | + } |
| 14 | + |
| 15 | + class TransformedData : DataPoint |
| 16 | + { |
| 17 | + public string TimeframeCategory { get; set; } |
| 18 | + public string ScoreCategory { get; set; } |
| 19 | + |
| 20 | + public uint Label { get; set; } |
| 21 | + } |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | + /// This example demonstrates the use of the ValueMappingEstimator by mapping strings to other string values, or floats to strings. |
| 26 | + /// This is useful to map types to a grouping. |
| 27 | + /// It is possible to have multiple values map to the same category. |
| 28 | + public static void Example() |
| 29 | + { |
| 30 | + // Create a new ML context, for ML.NET operations. It can be used for exception tracking and logging, |
| 31 | + // as well as the source of randomness. |
| 32 | + var mlContext = new MLContext(); |
| 33 | + |
| 34 | + // Get a small dataset as an IEnumerable. |
| 35 | + var rawData = new[] { |
| 36 | + new DataPoint() { Timeframe = "0-4yrs" , Score = 1 }, |
| 37 | + new DataPoint() { Timeframe = "6-11yrs" , Score = 2 }, |
| 38 | + new DataPoint() { Timeframe = "12-25yrs" , Score = 3 }, |
| 39 | + new DataPoint() { Timeframe = "0-5yrs" , Score = 4 }, |
| 40 | + new DataPoint() { Timeframe = "12-25yrs" , Score = 5 }, |
| 41 | + new DataPoint() { Timeframe = "25+yrs" , Score = 5 }, |
| 42 | + }; |
| 43 | + |
| 44 | + var data = mlContext.Data.LoadFromEnumerable(rawData); |
| 45 | + |
| 46 | + // Construct the mapping to other strings for the Timeframe column. |
| 47 | + var timeframeMap = new Dictionary<string, string>(); |
| 48 | + timeframeMap["0-4yrs"] = "Short"; |
| 49 | + timeframeMap["0-5yrs"] = "Short"; |
| 50 | + timeframeMap["6-11yrs"] = "Medium"; |
| 51 | + timeframeMap["12-25yrs"] = "Long"; |
| 52 | + timeframeMap["25+yrs"] = "Long"; |
| 53 | + |
| 54 | + // Construct the mapping of strings to keys(uints) for the Timeframe column. |
| 55 | + var timeframeKeyMap = new Dictionary<string, uint>(); |
| 56 | + timeframeKeyMap["0-4yrs"] = 1; |
| 57 | + timeframeKeyMap["0-5yrs"] = 1; |
| 58 | + timeframeKeyMap["6-11yrs"] = 2; |
| 59 | + timeframeKeyMap["12-25yrs"] = 3; |
| 60 | + timeframeKeyMap["25+yrs"] = 3; |
| 61 | + |
| 62 | + // Construct the mapping of ints to strings for the Score column. |
| 63 | + var scoreMap = new Dictionary<int, string>(); |
| 64 | + scoreMap[1] = "Low"; |
| 65 | + scoreMap[2] = "Low"; |
| 66 | + scoreMap[3] = "Average"; |
| 67 | + scoreMap[4] = "High"; |
| 68 | + scoreMap[5] = "High"; |
| 69 | + |
| 70 | + // Constructs the ML.net pipeline |
| 71 | + var pipeline = mlContext.Transforms.Conversion.MapValue("TimeframeCategory", timeframeMap, "Timeframe") |
| 72 | + .Append(mlContext.Transforms.Conversion.MapValue("ScoreCategory", scoreMap, "Score")) |
| 73 | + // on the MapValue below, the treatValuesAsKeyType is set to true. The type of the Label column will be a key type, |
| 74 | + // and it can be used as input for trainers performing multiclass classification. |
| 75 | + .Append(mlContext.Transforms.Conversion.MapValue("Label", timeframeKeyMap, "Timeframe", treatValuesAsKeyType: true)); |
| 76 | + |
| 77 | + // Fits the pipeline to the data. |
| 78 | + IDataView transformedData = pipeline.Fit(data).Transform(data); |
| 79 | + |
| 80 | + // Getting the resulting data as an IEnumerable. |
| 81 | + // This will contain the newly created columns. |
| 82 | + IEnumerable<TransformedData> features = mlContext.Data.CreateEnumerable<TransformedData>(transformedData, reuseRowObject: false); |
| 83 | + |
| 84 | + Console.WriteLine($" Timeframe TimeframeCategory Label Score ScoreCategory"); |
| 85 | + foreach (var featureRow in features) |
| 86 | + { |
| 87 | + Console.WriteLine($"{featureRow.Timeframe}\t\t{featureRow.TimeframeCategory}\t\t\t{featureRow.Label}\t\t{featureRow.Score}\t{featureRow.ScoreCategory}"); |
| 88 | + } |
| 89 | + |
| 90 | + // TransformedData obtained post-transformation. |
| 91 | + // |
| 92 | + // Timeframe TimeframeCategory Label Score ScoreCategory |
| 93 | + // 0 - 4yrs Short 1 1 Low |
| 94 | + // 6 - 11yrs Medium 2 2 Low |
| 95 | + // 12 - 25yrs Long 3 3 Average |
| 96 | + // 0 - 5yrs Short 1 4 High |
| 97 | + // 12 - 25yrs Long 3 5 High |
| 98 | + // 25 + yrs Long 3 5 High |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments