@@ -10,67 +10,71 @@ public class ValueMappingFloatToStringExample
10
10
/// <summary>
11
11
/// Helper class for retrieving the resulting data
12
12
/// </summary>
13
- class SampleInfertDataWithInducedCategory
13
+ class SampleTemperatureDataWithCategory
14
14
{
15
- public float Age = 0 ;
16
- public float Induced = 0.0f ;
17
- public string InducedCategory = default ;
15
+ public DateTime Date = default ;
16
+ public float Temperature = 0.0f ;
17
+ public string TemperatureCategory = default ;
18
18
}
19
19
20
- /// This example demonstrates the use of floating types as the key type for ValueMappingEstimator by mapping a float-to-string value.
21
- /// The mapping looks like the following:
22
- /// 1.0 -> Cat1
23
- /// 2.0 -> Cat2
20
+ /// This example demonstrates the use of ValueMappingEstimator by mapping float-to-string values. This is useful if the key
21
+ /// data are floating point and need to be grouped into string values. In this example, the Induction value is mapped to
22
+ /// "T1", "T2", "T3", and "T4" groups.
24
23
public static void Run ( )
25
24
{
26
25
// Create a new ML context, for ML.NET operations. It can be used for exception tracking and logging,
27
26
// as well as the source of randomness.
28
27
var mlContext = new MLContext ( ) ;
29
28
30
29
// Get a small dataset as an IEnumerable.
31
- IEnumerable < SamplesUtils . DatasetUtils . SampleInfertData > data = SamplesUtils . DatasetUtils . GetInfertData ( ) ;
30
+ IEnumerable < SamplesUtils . DatasetUtils . SampleTemperatureData > data = SamplesUtils . DatasetUtils . GetSampleTemperatureData ( ) ;
32
31
IDataView trainData = mlContext . Data . ReadFromEnumerable ( data ) ;
33
32
33
+ // If the list of keys and values are known, they can be passed to the API. The ValueMappingEstimator can also get the mapping through an IDataView
34
+
34
35
// Creating a list of keys based on the induced value from the dataset
35
- // These lists are created by hand for the demonstration, but the ValueMappingEstimator does take an IEnumerable.
36
- var inducedKeys = new List < float > ( )
36
+ var temperatureKeys = new List < float > ( )
37
37
{
38
- 1.0f ,
39
- 2.0f
38
+ 39.0F ,
39
+ 67.0F ,
40
+ 75.0F ,
41
+ 82.0F ,
40
42
} ;
41
43
42
44
// Creating a list of values, these strings will map accordingly to each key.
43
- var inducedValues = new List < string > ( )
45
+ var classificationValues = new List < string > ( )
44
46
{
45
- "Cat1" ,
46
- "Cat2"
47
+ "T1" ,
48
+ "T2" ,
49
+ "T3" ,
50
+ "T4"
47
51
} ;
48
52
49
53
// Constructs the ValueMappingEstimator making the ML.net pipeline
50
- var pipeline = mlContext . Transforms . Conversion . ValueMap ( inducedKeys , inducedValues , ( "InducedCategory " , "Induced " ) ) ;
54
+ var pipeline = mlContext . Transforms . Conversion . ValueMap ( temperatureKeys , classificationValues , ( "TemperatureCategory " , "Temperature " ) ) ;
51
55
52
- // Fits the ValueMappingEstimator and transforms the data adding the InducedCategory column.
56
+ // Fits the ValueMappingEstimator and transforms the data adding the TemperatureCategory column.
53
57
IDataView transformedData = pipeline . Fit ( trainData ) . Transform ( trainData ) ;
54
58
55
- // Getting the resulting data as an IEnumerable of SampleInfertDataWithInducedCategory . This will contain the newly created column InducedCategory
56
- IEnumerable < SampleInfertDataWithInducedCategory > featureRows = mlContext . CreateEnumerable < SampleInfertDataWithInducedCategory > ( transformedData , reuseRowObject : false ) ;
59
+ // Getting the resulting data as an IEnumerable of SampleTemperatureDataWithCategory . This will contain the newly created column TemperatureCategory
60
+ IEnumerable < SampleTemperatureDataWithCategory > featureRows = mlContext . CreateEnumerable < SampleTemperatureDataWithCategory > ( transformedData , reuseRowObject : false ) ;
57
61
58
62
Console . WriteLine ( $ "Example of mapping float->string") ;
59
- Console . WriteLine ( $ "Age \t Induced \t InducedCategory ") ;
63
+ Console . WriteLine ( $ "Date \t \t Temperature \t TemperatureCategory ") ;
60
64
foreach ( var featureRow in featureRows )
61
65
{
62
- Console . WriteLine ( $ "{ featureRow . Age } \t { featureRow . Induced } \t { featureRow . InducedCategory } ") ;
66
+ Console . WriteLine ( $ "{ featureRow . Date . ToString ( "d" ) } \t { featureRow . Temperature } \t \t { featureRow . TemperatureCategory } ") ;
63
67
}
64
68
65
69
// Features column obtained post-transformation.
66
70
//
67
71
// Example of mapping float->string
68
- // Age Induced InducedCategory
69
- // 26 1 Cat1
70
- // 42 1 Cat1
71
- // 39 2 Cat2
72
- // 34 2 Cat2
73
- // 35 1 Cat1
72
+ // Date Temperature TemperatureCategory
73
+ // 1/1/2012 39 T1
74
+ // 1/2/2012 82 T4
75
+ // 1/3/2012 75 T3
76
+ // 1/4/2012 67 T2
77
+ // 1/5/2012 75 T3
74
78
}
75
79
}
76
80
}
0 commit comments