Skip to content

Commit f5af03b

Browse files
authored
Link to the concat transform sample in the catalog (#2317)
Adding a link to the concat transform sample; adding a bit more explanation to the concat transform sample.
1 parent 0c385ee commit f5af03b

File tree

3 files changed

+24
-12
lines changed

3 files changed

+24
-12
lines changed

docs/samples/Microsoft.ML.Samples/Dynamic/ConcatTransform.cs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,15 @@ namespace Microsoft.ML.Samples.Dynamic
88
{
99
public class ConcatTransformExample
1010
{
11-
class SampleInfertDataWithFeatures
12-
{
13-
public VBuffer<float> Features { get; set; }
14-
}
15-
1611
public static void ConcatTransform()
1712
{
1813
// Create a new ML context, for ML.NET operations. It can be used for exception tracking and logging,
1914
// as well as the source of randomness.
20-
var ml = new MLContext();
15+
var mlContext = new MLContext();
2116

2217
// Get a small dataset as an IEnumerable and them read it as ML.NET's data type.
2318
IEnumerable<SamplesUtils.DatasetUtils.SampleInfertData> data = SamplesUtils.DatasetUtils.GetInfertData();
24-
var trainData = ml.Data.ReadFromEnumerable(data);
19+
var trainData = mlContext.Data.ReadFromEnumerable(data);
2520

2621
// Preview of the data.
2722
//
@@ -32,16 +27,20 @@ public static void ConcatTransform()
3227
// 34.0 1.0 0-5yrs 2.0 4.0 2.0 4.0 ...
3328
// 35.0 1.0 6-11yrs 1.0 3.0 32.0 5.0 ...
3429

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");
3633
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" });
3835

3936
// The transformed data.
4037
var transformedData = pipeline.Fit(trainData).Transform(trainData);
4138

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);
4442

43+
// And we can write out a few rows
4544
Console.WriteLine($"{outputColumnName} column obtained post-transformation.");
4645
foreach (var featureRow in featuresColumn)
4746
{
@@ -50,6 +49,7 @@ public static void ConcatTransform()
5049
Console.WriteLine("");
5150
}
5251

52+
// Expected output:
5353
// Features column obtained post-transformation.
5454
//
5555
// 26 6 1
@@ -58,5 +58,10 @@ public static void ConcatTransform()
5858
// 34 4 2
5959
// 35 3 1
6060
}
61+
62+
private class SampleInfertDataWithFeatures
63+
{
64+
public VBuffer<float> Features { get; set; }
65+
}
6166
}
6267
}

docs/samples/Microsoft.ML.Samples/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ internal static class Program
66
{
77
static void Main(string[] args)
88
{
9-
FeatureContributionCalculationTransform_RegressionExample.FeatureContributionCalculationTransform_Regression();
9+
ConcatTransformExample.ConcatTransform();
1010
}
1111
}
1212
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ public static ColumnCopyingEstimator CopyColumns(this TransformsCatalog catalog,
3636
/// <param name="catalog">The transform's catalog.</param>
3737
/// <param name="outputColumnName">Name of the column resulting from the transformation of <paramref name="inputColumnNames"/>.</param>
3838
/// <param name="inputColumnNames">Name of the columns to transform.</param>
39+
/// <example>
40+
/// <format type="text/markdown">
41+
/// <![CDATA[
42+
/// [!code-csharp[Concat](~/../docs/samples/docs/samples/Microsoft.ML.Samples/Dynamic/ConcatTransform.cs)]
43+
/// ]]>
44+
/// </format>
45+
/// </example>
3946
public static ColumnConcatenatingEstimator Concatenate(this TransformsCatalog catalog, string outputColumnName, params string[] inputColumnNames)
4047
=> new ColumnConcatenatingEstimator(CatalogUtils.GetEnvironment(catalog), outputColumnName, inputColumnNames);
4148

0 commit comments

Comments
 (0)