Skip to content

Commit 0ffecfc

Browse files
author
Rogan Carr
committed
Adding a sample for SelectColumn.
1 parent cc0956b commit 0ffecfc

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Microsoft.ML.Data;
4+
5+
namespace Microsoft.ML.Samples.Dynamic
6+
{
7+
public class SelectColumns
8+
{
9+
public static void Example()
10+
{
11+
// Create a new ML context, for ML.NET operations. It can be used for exception tracking and logging,
12+
// as well as the source of randomness.
13+
var mlContext = new MLContext();
14+
15+
// Get a small dataset as an IEnumerable and them read it as ML.NET's data type.
16+
IEnumerable<SamplesUtils.DatasetUtils.SampleInfertData> data = SamplesUtils.DatasetUtils.GetInfertData();
17+
var trainData = mlContext.Data.ReadFromEnumerable(data);
18+
19+
// Preview of the data.
20+
//
21+
// Age Case Education induced parity pooled.stratum row_num ...
22+
// 26.0 1.0 0-5yrs 1.0 6.0 3.0 1.0 ...
23+
// 42.0 1.0 0-5yrs 1.0 1.0 1.0 2.0 ...
24+
// 39.0 1.0 0-5yrs 2.0 6.0 4.0 3.0 ...
25+
// 34.0 1.0 0-5yrs 2.0 4.0 2.0 4.0 ...
26+
// 35.0 1.0 6-11yrs 1.0 3.0 32.0 5.0 ...
27+
28+
// Select a subset of columns to keep, but don't drop the others.
29+
var pipeline = mlContext.Transforms.SelectColumns(new string[] { "Age", "Education" });
30+
31+
// Now we can transform the data and look at the output to confirm the behavior of CopyColumns.
32+
// Don't forget that this operation doesn't actually evaluate data until we read the data below.
33+
var transformedData = pipeline.Fit(trainData).Transform(trainData);
34+
35+
// Print the number of columns schema
36+
Console.WriteLine($"There are {transformedData.Schema.Count} columns in the dataset.");
37+
38+
// Expected output:
39+
// There are 2 columns in the dataset.
40+
41+
// We can extract the newly created column as an IEnumerable of SampleInfertDataTransformed, the class we define below.
42+
var rowEnumerable = mlContext.CreateEnumerable<SampleInfertDataTransformed>(transformedData, reuseRowObject: false);
43+
44+
// And finally, we can write out the rows of the dataset, looking at the columns of interest.
45+
Console.WriteLine($"Label and Educations columns obtained post-transformation.");
46+
foreach (var row in rowEnumerable)
47+
{
48+
Console.WriteLine($"Label: {row.Age} Education: {row.Education}");
49+
}
50+
51+
// Expected output:
52+
// Label and Education columns obtained post-transformation.
53+
// Label: 26 Education: 0 - 5yrs
54+
// Label: 42 Education: 0 - 5yrs
55+
// Label: 39 Education: 0 - 5yrs
56+
// Label: 34 Education: 0 - 5yrs
57+
// Label: 35 Education: 6 - 11yrs
58+
}
59+
60+
private class SampleInfertDataTransformed
61+
{
62+
public float Age { get; set; }
63+
public string Education { get; set; }
64+
}
65+
}
66+
}

0 commit comments

Comments
 (0)