Skip to content

Commit 599a29a

Browse files
committed
Adjust the samples about ValueMapping
1 parent 3958048 commit 599a29a

File tree

8 files changed

+258
-321
lines changed

8 files changed

+258
-321
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace Microsoft.ML.Samples.Dynamic
5+
{
6+
public static class MapValueIdvLookup
7+
{
8+
// Type for the IDataVIew that will be serving as the map
9+
private class LookupMap
10+
{
11+
public float Value { get; set; }
12+
public string Category { get; set; }
13+
14+
}
15+
16+
private class DataPoint
17+
{
18+
public float Price { get; set; }
19+
}
20+
21+
private class TransformedData : DataPoint
22+
{
23+
public string PriceCategory { get; set; }
24+
}
25+
26+
/// This example demonstrates the use of MapValue by mapping floats to strings, looking up the mapping in an IDataView.
27+
/// This is useful to map types to a grouping.
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() { Price = 3.14f },
37+
new DataPoint() { Price = 2000f },
38+
new DataPoint() { Price = 1.19f },
39+
new DataPoint() { Price = 2.17f },
40+
new DataPoint() { Price = 33.784f },
41+
42+
};
43+
44+
// Convert to IDataView
45+
var data = mlContext.Data.LoadFromEnumerable(rawData);
46+
47+
// Create the lookup map data IEnumerable.
48+
var lookupData = new[] {
49+
new LookupMap { Value = 3.14f, Category = "Low" },
50+
new LookupMap { Category = "Low" , Value = 1.19f },
51+
new LookupMap { Category = "Low" , Value = 2.17f },
52+
new LookupMap { Category = "Medium", Value = 33.784f},
53+
new LookupMap { Category = "High", Value = 2000f}
54+
55+
};
56+
57+
// Convert to IDataView
58+
var lookupIdvMap = mlContext.Data.LoadFromEnumerable(lookupData);
59+
60+
// Constructs the ValueMappingEstimator making the ML.net pipeline
61+
var pipeline = mlContext.Transforms.Conversion.MapValue("PriceCategory", lookupIdvMap, lookupIdvMap.Schema["Value"], lookupIdvMap.Schema["Category"], "Price");
62+
63+
// Fits the ValueMappingEstimator and transforms the data converting the Price to PriceCategory.
64+
IDataView transformedData = pipeline.Fit(data).Transform(data);
65+
66+
// Getting the resulting data as an IEnumerable.
67+
IEnumerable<TransformedData> features = mlContext.Data.CreateEnumerable<TransformedData>(transformedData, reuseRowObject: false);
68+
69+
Console.WriteLine($" Price PriceCategory");
70+
foreach (var featureRow in features)
71+
Console.WriteLine($"{featureRow.Price}\t\t{featureRow.PriceCategory}");
72+
73+
// TransformedData obtained post-transformation.
74+
//
75+
// Price PriceCategory
76+
// 3.14 Low
77+
// 2000 High
78+
// 1.19 Low
79+
// 2.17 Low
80+
// 33.784 Medium
81+
}
82+
}
83+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
using System;
2+
using System.Collections.Generic;
3+
namespace Microsoft.ML.Samples.Dynamic
4+
{
5+
public static class MapValueToArray
6+
{
7+
class DataPoint
8+
{
9+
public string Timeframe { get; set; }
10+
}
11+
12+
class TransformedData : DataPoint
13+
{
14+
public int[] Feature { get; set; }
15+
}
16+
17+
/// This example demonstrates the use of MapValue by mapping strings to array values, which allows for mapping data to numeric arrays.
18+
/// 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.
19+
/// In this example, we are mapping the Timeframe data to arbitrary integer arrays.
20+
public static void Example()
21+
{
22+
// Create a new ML context, for ML.NET operations. It can be used for exception tracking and logging,
23+
// as well as the source of randomness.
24+
var mlContext = new MLContext();
25+
26+
// Get a small dataset as an IEnumerable.
27+
var rawData = new[] {
28+
new DataPoint() { Timeframe = "0-4yrs" },
29+
new DataPoint() { Timeframe = "6-11yrs" },
30+
new DataPoint() { Timeframe = "12-25yrs" },
31+
new DataPoint() { Timeframe = "0-5yrs" },
32+
new DataPoint() { Timeframe = "12-25yrs" },
33+
new DataPoint() { Timeframe = "25+yrs" },
34+
};
35+
36+
var data = mlContext.Data.LoadFromEnumerable(rawData);
37+
38+
// If the list of keys and values are known, they can be passed to the API.
39+
// Creating a list of key-value pairs based on the dataset
40+
var timeframeMap = new Dictionary<string, int[]>();
41+
timeframeMap["0-4yrs"] = new int[] { 0, 5, 300 };
42+
timeframeMap["0-5yrs"] = new int[] { 0, 5, 300 };
43+
timeframeMap["6-11yrs"] = new int[] { 6, 11, 300 };
44+
timeframeMap["12-25yrs"] = new int[] { 12, 50, 300 };
45+
timeframeMap["25+yrs"] = new int[] { 12, 50, 300 };
46+
47+
// Constructs the ValueMappingEstimator making the ML.net pipeline.
48+
var pipeline = mlContext.Transforms.Conversion.MapValue("Feature", timeframeMap, "Timeframe");
49+
50+
// Fits the ValueMappingEstimator and transforms the data adding the Features column.
51+
IDataView transformedData = pipeline.Fit(data).Transform(data);
52+
53+
// Getting the resulting data as an IEnumerable.
54+
IEnumerable<TransformedData> featuresColumn = mlContext.Data.CreateEnumerable<TransformedData>(transformedData, reuseRowObject: false);
55+
56+
Console.WriteLine($"Timeframe Feature");
57+
foreach (var featureRow in featuresColumn)
58+
{
59+
Console.WriteLine($"{featureRow.Timeframe}\t\t {string.Join(",", featureRow.Feature)}");
60+
}
61+
62+
// Timeframe Feature
63+
// 0 - 4yrs 0, 5, 300
64+
// 6 - 11yrs 6, 11, 300
65+
// 12 - 25yrs 12, 50, 300
66+
// 0 - 5yrs 0, 5, 300
67+
// 12 - 25yrs 12, 50,300
68+
// 25 + yrs 12, 50, 300
69+
}
70+
}
71+
}

docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/Conversion/ValueMapping.cs

Lines changed: 0 additions & 72 deletions
This file was deleted.

docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/Conversion/ValueMappingFloatToString.cs

Lines changed: 0 additions & 64 deletions
This file was deleted.

0 commit comments

Comments
 (0)