Skip to content

Commit edbb1f0

Browse files
committed
Addressing PR comments
1 parent a51e688 commit edbb1f0

File tree

6 files changed

+104
-119
lines changed

6 files changed

+104
-119
lines changed

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

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,6 @@ namespace Microsoft.ML.Samples.Dynamic
55
{
66
public static class ConvertType
77
{
8-
private sealed class InputData
9-
{
10-
public bool Survived;
11-
}
12-
13-
private sealed class TransformedData
14-
{
15-
public bool Survived { get; set; }
16-
17-
public Int32 SurvivedInt32 { get; set; }
18-
}
19-
208
public static void Example()
219
{
2210
var mlContext = new MLContext(seed: 1);
@@ -51,5 +39,13 @@ public static void Example()
5139
// A: False Aconv:0
5240
// A: False Aconv:0
5341
}
42+
private class InputData
43+
{
44+
public bool Survived;
45+
}
46+
private sealed class TransformedData : InputData
47+
{
48+
public Int32 SurvivedInt32 { get; set; }
49+
}
5450
}
5551
}

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

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,15 @@
44
namespace Microsoft.ML.Samples.Dynamic
55
{
66
// This example illustrates how to convert multiple columns of different types to one type, in this case System.Single.
7-
// This is often a useful data transformation before concatenting the features together and passing them to a particular estimator.
7+
// This is often a useful data transformation before concatenating the features together and passing them to a particular estimator.
88
public static class ConvertTypeMultiColumn
9-
{
10-
// The initial data type
11-
private class InputData
12-
{
13-
public bool Feature1;
14-
public string Feature2;
15-
public DateTime Feature3;
16-
public double Feature4;
17-
}
18-
19-
// The resulting data type after the transformation
20-
private sealed class TransformedData : InputData
21-
{
22-
public float Converted1 { get; set; }
23-
public float Converted2 { get; set; }
24-
25-
public float Converted3 { get; set; }
26-
public float Converted4 { get; set; }
27-
}
28-
9+
{
2910
public static void Example()
3011
{
12+
// Create a new ML context, for ML.NET operations. It can be used for exception tracking and logging,
13+
// as well as the source of randomness.
3114
var mlContext = new MLContext(seed: 1);
15+
3216
var rawData = new[] {
3317
new InputData() { Feature1 = true, Feature2 = "0.4", Feature3 = DateTime.Now, Feature4 = 0.145},
3418
new InputData() { Feature1 = false, Feature2 = "0.5", Feature3 = DateTime.Today, Feature4 = 3.14},
@@ -37,6 +21,7 @@ public static void Example()
3721
new InputData() { Feature1 = true, Feature2 = "8904", Feature3 = DateTime.UtcNow, Feature4 = 8.09},
3822
};
3923

24+
// Convert the data to an IDataView.
4025
var data = mlContext.Data.LoadFromEnumerable(rawData);
4126

4227
// Construct the pipeline.
@@ -46,22 +31,24 @@ public static void Example()
4631
new InputOutputColumnPair("Converted2", "Feature2"),
4732
new InputOutputColumnPair("Converted3", "Feature3"),
4833
new InputOutputColumnPair("Converted4", "Feature4"),
34+
},
35+
DataKind.Single);
4936

50-
}, DataKind.Single);
51-
52-
// Let's train our pipeline, and then apply it to the same data.
37+
// Let's fit our pipeline to the data.
5338
var transformer = pipeline.Fit(data);
39+
// Transforming the same data. This will add the 4 columns defined in the pipeline, containing the converted
40+
// values of the initial columns.
5441
var transformedData = transformer.Transform(data);
5542

56-
// Shape the transformed data as a strongly typed IEnumerable
43+
// Shape the transformed data as a strongly typed IEnumerable.
5744
var convertedData = mlContext.Data.CreateEnumerable<TransformedData>(transformedData, true);
5845

5946
// Printing the results.
6047
Console.WriteLine("Converted1\t Converted2\t Converted3\t Converted4");
6148
foreach (var item in convertedData)
6249
Console.WriteLine($"\t{item.Converted1}\t {item.Converted2}\t\t {item.Converted3}\t {item.Converted4}");
6350

64-
// Output
51+
// Transformed data.
6552
//
6653
// Converted1 Converted2 Converted3 Converted4
6754
// 1 0.4 6.368921E+17 0.145
@@ -71,5 +58,21 @@ public static void Example()
7158
// 1 8904 6.368924E+17 8.09
7259

7360
}
61+
// The initial data type
62+
private class InputData
63+
{
64+
public bool Feature1;
65+
public string Feature2;
66+
public DateTime Feature3;
67+
public double Feature4;
68+
}
69+
// The resulting data type after the transformation
70+
private class TransformedData : InputData
71+
{
72+
public float Converted1 { get; set; }
73+
public float Converted2 { get; set; }
74+
public float Converted3 { get; set; }
75+
public float Converted4 { get; set; }
76+
}
7477
}
7578
}

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

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,13 @@
22
using System.Collections.Generic;
33
using Microsoft.ML.Data;
44

5+
56
namespace Microsoft.ML.Samples.Dynamic
67
{
78
public static class MapValue
89
{
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-
2510
/// 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.
11+
/// This is useful to map types to a category.
2812
public static void Example()
2913
{
3014
// Create a new ML context, for ML.NET operations. It can be used for exception tracking and logging,
@@ -38,7 +22,7 @@ public static void Example()
3822
new DataPoint() { Timeframe = "12-25yrs" , Score = 3 },
3923
new DataPoint() { Timeframe = "0-5yrs" , Score = 4 },
4024
new DataPoint() { Timeframe = "12-25yrs" , Score = 5 },
41-
new DataPoint() { Timeframe = "25+yrs" , Score = 5 },
25+
new DataPoint() { Timeframe = "25+yrs" , Score = 5 },
4226
};
4327

4428
var data = mlContext.Data.LoadFromEnumerable(rawData);
@@ -70,7 +54,7 @@ public static void Example()
7054
// Constructs the ML.net pipeline
7155
var pipeline = mlContext.Transforms.Conversion.MapValue("TimeframeCategory", timeframeMap, "Timeframe")
7256
.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,
57+
// on the MapValue below, the treatValuesAsKeyType is set to true. The type of the Label column will be a KeyDataViewType type,
7458
// and it can be used as input for trainers performing multiclass classification.
7559
.Append(mlContext.Transforms.Conversion.MapValue("Label", timeframeKeyMap, "Timeframe", treatValuesAsKeyType: true));
7660

@@ -83,19 +67,28 @@ public static void Example()
8367

8468
Console.WriteLine($" Timeframe TimeframeCategory Label Score ScoreCategory");
8569
foreach (var featureRow in features)
86-
{
8770
Console.WriteLine($"{featureRow.Timeframe}\t\t{featureRow.TimeframeCategory}\t\t\t{featureRow.Label}\t\t{featureRow.Score}\t{featureRow.ScoreCategory}");
88-
}
8971

9072
// TransformedData obtained post-transformation.
9173
//
9274
// 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
75+
// 0-4yrs Short 1 1 Low
76+
// 6-11yrs Medium 2 2 Low
77+
// 12-25yrs Long 3 3 Average
78+
// 0-5yrs Short 1 4 High
79+
// 12-25yrs Long 3 5 High
80+
// 25+yrs Long 3 5 High
81+
}
82+
private class DataPoint
83+
{
84+
public string Timeframe { get; set; }
85+
public int Score { get; set; }
86+
}
87+
private class TransformedData : DataPoint
88+
{
89+
public string TimeframeCategory { get; set; }
90+
public string ScoreCategory { get; set; }
91+
public uint Label { get; set; }
9992
}
10093
}
10194
}

docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/Conversion/MapValueIDVLookup.cs renamed to docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/Conversion/MapValueIdvLookup.cs

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,6 @@ namespace Microsoft.ML.Samples.Dynamic
55
{
66
public static class MapValueIdvLookup
77
{
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-
268
/// This example demonstrates the use of MapValue by mapping floats to strings, looking up the mapping in an IDataView.
279
/// This is useful to map types to a grouping.
2810
public static void Example()
@@ -47,17 +29,17 @@ public static void Example()
4729
// Create the lookup map data IEnumerable.
4830
var lookupData = new[] {
4931
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}
32+
new LookupMap { Value = 1.19f , Category = "Low" },
33+
new LookupMap { Value = 2.17f , Category = "Low" },
34+
new LookupMap { Value = 33.784f, Category = "Medium" },
35+
new LookupMap { Value = 2000f, Category = "High"}
5436

5537
};
5638

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

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

6345
// Fits the ValueMappingEstimator and transforms the data converting the Price to PriceCategory.
@@ -79,5 +61,20 @@ public static void Example()
7961
// 2.17 Low
8062
// 33.784 Medium
8163
}
64+
65+
// Type for the IDataView that will be serving as the map
66+
private class LookupMap
67+
{
68+
public float Value { get; set; }
69+
public string Category { get; set; }
70+
}
71+
private class DataPoint
72+
{
73+
public float Price { get; set; }
74+
}
75+
private class TransformedData : DataPoint
76+
{
77+
public string PriceCategory { get; set; }
78+
}
8279
}
8380
}

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

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,7 @@
33
namespace Microsoft.ML.Samples.Dynamic
44
{
55
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-
6+
{
177
/// This example demonstrates the use of MapValue by mapping strings to array values, which allows for mapping data to numeric arrays.
188
/// 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.
199
/// In this example, we are mapping the Timeframe data to arbitrary integer arrays.
@@ -30,42 +20,48 @@ public static void Example()
3020
new DataPoint() { Timeframe = "12-25yrs" },
3121
new DataPoint() { Timeframe = "0-5yrs" },
3222
new DataPoint() { Timeframe = "12-25yrs" },
33-
new DataPoint() { Timeframe = "25+yrs" },
23+
new DataPoint() { Timeframe = "25+yrs" },
3424
};
3525

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

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
28+
// Creating a list of key-value pairs to indicate the mapping between the
29+
// DataPoint values, and the arrays they should map to.
4030
var timeframeMap = new Dictionary<string, int[]>();
4131
timeframeMap["0-4yrs"] = new int[] { 0, 5, 300 };
4232
timeframeMap["0-5yrs"] = new int[] { 0, 5, 300 };
4333
timeframeMap["6-11yrs"] = new int[] { 6, 11, 300 };
4434
timeframeMap["12-25yrs"] = new int[] { 12, 50, 300 };
4535
timeframeMap["25+yrs"] = new int[] { 12, 50, 300 };
4636

47-
// Constructs the ValueMappingEstimator making the ML.net pipeline.
48-
var pipeline = mlContext.Transforms.Conversion.MapValue("Feature", timeframeMap, "Timeframe");
37+
// Constructs the ValueMappingEstimator making the ML.NET pipeline.
38+
var pipeline = mlContext.Transforms.Conversion.MapValue("Features", timeframeMap, "Timeframe");
4939

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

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

56-
Console.WriteLine($"Timeframe Feature");
46+
Console.WriteLine($"Timeframe Features");
5747
foreach (var featureRow in featuresColumn)
58-
{
59-
Console.WriteLine($"{featureRow.Timeframe}\t\t {string.Join(",", featureRow.Feature)}");
60-
}
48+
Console.WriteLine($"{featureRow.Timeframe}\t\t {string.Join(",", featureRow.Features)}");
6149

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
50+
// Timeframe Features
51+
// 0-4yrs 0, 5, 300
52+
// 6-11yrs 6, 11, 300
53+
// 12-25yrs 12, 50, 300
54+
// 0-5yrs 0, 5, 300
55+
// 12-25yrs 12, 50,300
56+
// 25+yrs 12, 50, 300
57+
}
58+
public class DataPoint
59+
{
60+
public string Timeframe { get; set; }
61+
}
62+
public class TransformedData : DataPoint
63+
{
64+
public int[] Features { get; set; }
6965
}
7066
}
7167
}

src/Microsoft.ML.Data/Transforms/ConversionsExtensionsCatalog.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ internal static TypeConvertingEstimator ConvertType(this TransformsCatalog.Conve
106106
/// <example>
107107
/// <format type="text/markdown">
108108
/// <![CDATA[
109-
/// [!code-csharp[KeyToValueMappingEstimator](~/../docs/samples/docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/Conversion/ValueMappingStringToKeyType.cs)]
109+
/// [!code-csharp[MapKeyToValue](~/../docs/samples/docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/Conversion/ValueMappingStringToKeyType.cs)]
110110
/// ]]></format>
111111
/// </example>
112112
public static KeyToValueMappingEstimator MapKeyToValue(this TransformsCatalog.ConversionTransforms catalog, string outputColumnName, string inputColumnName = null)
@@ -179,7 +179,7 @@ public static KeyToVectorMappingEstimator MapKeyToVector(this TransformsCatalog.
179179
/// <example>
180180
/// <format type="text/markdown">
181181
/// <![CDATA[
182-
/// [!code-csharp[ValueToKey](~/../docs/samples/docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/Conversion/KeyToValueToKey.cs)]
182+
/// [!code-csharp[MapKeyToValue](~/../docs/samples/docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/Conversion/KeyToValueToKey.cs)]
183183
/// ]]>
184184
/// </format>
185185
/// </example>
@@ -229,7 +229,7 @@ public static ValueToKeyMappingEstimator MapValueToKey(this TransformsCatalog.Co
229229
/// <example>
230230
/// <format type="text/markdown">
231231
/// <![CDATA[
232-
/// [!code-csharp[ValueToKey](~/../docs/samples/docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/Conversion/KeyToValueValueToKey.cs)]
232+
/// [!code-csharp[MapKeyToValue](~/../docs/samples/docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/Conversion/KeyToValueValueToKey.cs)]
233233
/// ]]>
234234
/// </format>
235235
/// </example>

0 commit comments

Comments
 (0)