@@ -8,20 +8,15 @@ namespace Microsoft.ML.Samples.Dynamic
8
8
{
9
9
public class ConcatTransformExample
10
10
{
11
- class SampleInfertDataWithFeatures
12
- {
13
- public VBuffer < float > Features { get ; set ; }
14
- }
15
-
16
11
public static void ConcatTransform ( )
17
12
{
18
13
// Create a new ML context, for ML.NET operations. It can be used for exception tracking and logging,
19
14
// as well as the source of randomness.
20
- var ml = new MLContext ( ) ;
15
+ var mlContext = new MLContext ( ) ;
21
16
22
17
// Get a small dataset as an IEnumerable and them read it as ML.NET's data type.
23
18
IEnumerable < SamplesUtils . DatasetUtils . SampleInfertData > data = SamplesUtils . DatasetUtils . GetInfertData ( ) ;
24
- var trainData = ml . Data . ReadFromEnumerable ( data ) ;
19
+ var trainData = mlContext . Data . ReadFromEnumerable ( data ) ;
25
20
26
21
// Preview of the data.
27
22
//
@@ -32,16 +27,20 @@ public static void ConcatTransform()
32
27
// 34.0 1.0 0-5yrs 2.0 4.0 2.0 4.0 ...
33
28
// 35.0 1.0 6-11yrs 1.0 3.0 32.0 5.0 ...
34
29
35
- // A pipeline for concatenating the age, parity and induced columns together in the Features column.
30
+ // A pipeline for concatenating the Age, Parity and Induced columns together into a vector that will be the Features column.
31
+ // Concatenation is necessary because learners take **feature vectors** as inputs.
32
+ // e.g. var regressionTrainer = mlContext.Regression.Trainers.FastTree(labelColumn: "Label", featureColumn: "Features");
36
33
string outputColumnName = "Features" ;
37
- var pipeline = new ColumnConcatenatingEstimator ( ml , outputColumnName , new [ ] { "Age" , "Parity" , "Induced" } ) ;
34
+ var pipeline = mlContext . Transforms . Concatenate ( outputColumnName , new [ ] { "Age" , "Parity" , "Induced" } ) ;
38
35
39
36
// The transformed data.
40
37
var transformedData = pipeline . Fit ( trainData ) . Transform ( trainData ) ;
41
38
42
- // Getting the data of the newly created column as an IEnumerable of SampleInfertDataWithFeatures.
43
- var featuresColumn = ml . CreateEnumerable < SampleInfertDataWithFeatures > ( transformedData , reuseRowObject : false ) ;
39
+ // Now let's take a look at what this concatenation did.
40
+ // We can extract the newly created column as an IEnumerable of SampleInfertDataWithFeatures, the class we define above.
41
+ var featuresColumn = mlContext . CreateEnumerable < SampleInfertDataWithFeatures > ( transformedData , reuseRowObject : false ) ;
44
42
43
+ // And we can write out a few rows
45
44
Console . WriteLine ( $ "{ outputColumnName } column obtained post-transformation.") ;
46
45
foreach ( var featureRow in featuresColumn )
47
46
{
@@ -50,6 +49,7 @@ public static void ConcatTransform()
50
49
Console . WriteLine ( "" ) ;
51
50
}
52
51
52
+ // Expected output:
53
53
// Features column obtained post-transformation.
54
54
//
55
55
// 26 6 1
@@ -58,5 +58,10 @@ public static void ConcatTransform()
58
58
// 34 4 2
59
59
// 35 3 1
60
60
}
61
+
62
+ private class SampleInfertDataWithFeatures
63
+ {
64
+ public VBuffer < float > Features { get ; set ; }
65
+ }
61
66
}
62
67
}
0 commit comments