Skip to content

Commit 48664fe

Browse files
committed
Reformatted Ranking samples to width 85
1 parent 6364b31 commit 48664fe

File tree

10 files changed

+213
-111
lines changed

10 files changed

+213
-111
lines changed

docs/samples/Microsoft.ML.Samples/Dynamic/Trainers/Ranking/FastTree.cs

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,22 @@ namespace Samples.Dynamic.Trainers.Ranking
88
{
99
public static class FastTree
1010
{
11-
// This example requires installation of additional NuGet package
12-
// <a href="https://www.nuget.org/packages/Microsoft.ML.FastTree/">Microsoft.ML.FastTree</a>.
11+
// This example requires installation of additional NuGet package for
12+
// Microsoft.ML.FastTree at
13+
// https://www.nuget.org/packages/Microsoft.ML.FastTree/
1314
public static void Example()
1415
{
15-
// Create a new context for ML.NET operations. It can be used for exception tracking and logging,
16-
// as a catalog of available operations and as the source of randomness.
17-
// Setting the seed to a fixed number in this example to make outputs deterministic.
16+
// Create a new context for ML.NET operations. It can be used for
17+
// exception tracking and logging, as a catalog of available operations
18+
// and as the source of randomness. Setting the seed to a fixed number
19+
// in this example to make outputs deterministic.
1820
var mlContext = new MLContext(seed: 0);
1921

2022
// Create a list of training data points.
2123
var dataPoints = GenerateRandomDataPoints(1000);
2224

23-
// Convert the list of data points to an IDataView object, which is consumable by ML.NET API.
25+
// Convert the list of data points to an IDataView object, which is
26+
// consumable by ML.NET API.
2427
var trainingData = mlContext.Data.LoadFromEnumerable(dataPoints);
2528

2629
// Define the trainer.
@@ -29,17 +32,21 @@ public static void Example()
2932
// Train the model.
3033
var model = pipeline.Fit(trainingData);
3134

32-
// Create testing data. Use different random seed to make it different from training data.
33-
var testData = mlContext.Data.LoadFromEnumerable(GenerateRandomDataPoints(500, seed:123));
35+
// Create testing data. Use different random seed to make it different
36+
// from training data.
37+
var testData = mlContext.Data.LoadFromEnumerable(
38+
GenerateRandomDataPoints(500, seed:123));
3439

3540
// Run the model on test data set.
3641
var transformedTestData = model.Transform(testData);
3742

3843
// Take the top 5 rows.
39-
var topTransformedTestData = mlContext.Data.TakeRows(transformedTestData, 5);
44+
var topTransformedTestData = mlContext.Data.TakeRows(
45+
transformedTestData, 5);
4046

4147
// Convert IDataView object to a list.
42-
var predictions = mlContext.Data.CreateEnumerable<Prediction>(topTransformedTestData, reuseRowObject: false).ToList();
48+
var predictions = mlContext.Data.CreateEnumerable<Prediction>(
49+
topTransformedTestData, reuseRowObject: false).ToList();
4350

4451
// Print 5 predictions.
4552
foreach (var p in predictions)
@@ -61,7 +68,8 @@ public static void Example()
6168
// NDCG: @1:0.99, @2:0.98, @3:0.99
6269
}
6370

64-
private static IEnumerable<DataPoint> GenerateRandomDataPoints(int count, int seed = 0, int groupSize = 10)
71+
private static IEnumerable<DataPoint> GenerateRandomDataPoints(int count,
72+
int seed = 0, int groupSize = 10)
6573
{
6674
var random = new Random(seed);
6775
float randomFloat() => (float)random.NextDouble();
@@ -73,13 +81,16 @@ private static IEnumerable<DataPoint> GenerateRandomDataPoints(int count, int se
7381
Label = (uint)label,
7482
GroupId = (uint)(i / groupSize),
7583
// Create random features that are correlated with the label.
76-
// For data points with larger labels, the feature values are slightly increased by adding a constant.
77-
Features = Enumerable.Repeat(label, 50).Select(x => randomFloat() + x * 0.1f).ToArray()
84+
// For data points with larger labels, the feature values are
85+
// slightly increased by adding a constant.
86+
Features = Enumerable.Repeat(label, 50).Select(
87+
x => randomFloat() + x * 0.1f).ToArray()
7888
};
7989
}
8090
}
8191

82-
// Example with label, groupId, and 50 feature values. A data set is a collection of such examples.
92+
// Example with label, groupId, and 50 feature values. A data set is a
93+
// collection of such examples.
8394
private class DataPoint
8495
{
8596
[KeyType(5)]
@@ -102,8 +113,12 @@ private class Prediction
102113
// Pretty-print RankerMetrics objects.
103114
public static void PrintMetrics(RankingMetrics metrics)
104115
{
105-
Console.WriteLine($"DCG: {string.Join(", ", metrics.DiscountedCumulativeGains.Select((d, i) => $"@{i + 1}:{d:F2}").ToArray())}");
106-
Console.WriteLine($"NDCG: {string.Join(", ", metrics.NormalizedDiscountedCumulativeGains.Select((d, i) => $"@{i + 1}:{d:F2}").ToArray())}");
116+
Console.WriteLine("DCG: " + string.Join(", ",
117+
metrics.DiscountedCumulativeGains.Select(
118+
(d, i) => (i + 1) + ":" + d + ":F2").ToArray()));
119+
Console.WriteLine("NDCG: " + string.Join(", ",
120+
metrics.NormalizedDiscountedCumulativeGains.Select(
121+
(d, i) => (i + 1) + ":" + d + ":F2").ToArray()));
107122
}
108123
}
109124
}

docs/samples/Microsoft.ML.Samples/Dynamic/Trainers/Ranking/FastTree.tt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ string TrainerOptions = null;
66

77
string OptionsInclude = "";
88
string Comments= @"
9-
// This example requires installation of additional NuGet package
10-
// <a href=""https://www.nuget.org/packages/Microsoft.ML.FastTree/"">Microsoft.ML.FastTree</a>.";
9+
// This example requires installation of additional NuGet package for
10+
// Microsoft.ML.FastTree at
11+
// https://www.nuget.org/packages/Microsoft.ML.FastTree/";
1112

1213
string ExpectedOutputPerInstance = @"// Expected output:
1314
// Label: 5, Score: 13.0154

docs/samples/Microsoft.ML.Samples/Dynamic/Trainers/Ranking/FastTreeWithOptions.cs

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,22 @@ namespace Samples.Dynamic.Trainers.Ranking
99
{
1010
public static class FastTreeWithOptions
1111
{
12-
// This example requires installation of additional NuGet package
13-
// <a href="https://www.nuget.org/packages/Microsoft.ML.FastTree/">Microsoft.ML.FastTree</a>.
12+
// This example requires installation of additional NuGet package for
13+
// Microsoft.ML.FastTree at
14+
// https://www.nuget.org/packages/Microsoft.ML.FastTree/
1415
public static void Example()
1516
{
16-
// Create a new context for ML.NET operations. It can be used for exception tracking and logging,
17-
// as a catalog of available operations and as the source of randomness.
18-
// Setting the seed to a fixed number in this example to make outputs deterministic.
17+
// Create a new context for ML.NET operations. It can be used for
18+
// exception tracking and logging, as a catalog of available operations
19+
// and as the source of randomness. Setting the seed to a fixed number
20+
// in this example to make outputs deterministic.
1921
var mlContext = new MLContext(seed: 0);
2022

2123
// Create a list of training data points.
2224
var dataPoints = GenerateRandomDataPoints(1000);
2325

24-
// Convert the list of data points to an IDataView object, which is consumable by ML.NET API.
26+
// Convert the list of data points to an IDataView object, which is
27+
// consumable by ML.NET API.
2528
var trainingData = mlContext.Data.LoadFromEnumerable(dataPoints);
2629

2730
// Define trainer options.
@@ -43,17 +46,21 @@ public static void Example()
4346
// Train the model.
4447
var model = pipeline.Fit(trainingData);
4548

46-
// Create testing data. Use different random seed to make it different from training data.
47-
var testData = mlContext.Data.LoadFromEnumerable(GenerateRandomDataPoints(500, seed:123));
49+
// Create testing data. Use different random seed to make it different
50+
// from training data.
51+
var testData = mlContext.Data.LoadFromEnumerable(
52+
GenerateRandomDataPoints(500, seed:123));
4853

4954
// Run the model on test data set.
5055
var transformedTestData = model.Transform(testData);
5156

5257
// Take the top 5 rows.
53-
var topTransformedTestData = mlContext.Data.TakeRows(transformedTestData, 5);
58+
var topTransformedTestData = mlContext.Data.TakeRows(
59+
transformedTestData, 5);
5460

5561
// Convert IDataView object to a list.
56-
var predictions = mlContext.Data.CreateEnumerable<Prediction>(topTransformedTestData, reuseRowObject: false).ToList();
62+
var predictions = mlContext.Data.CreateEnumerable<Prediction>(
63+
topTransformedTestData, reuseRowObject: false).ToList();
5764

5865
// Print 5 predictions.
5966
foreach (var p in predictions)
@@ -75,7 +82,8 @@ public static void Example()
7582
// NDCG: @1:0.96, @2:0.95, @3:0.97
7683
}
7784

78-
private static IEnumerable<DataPoint> GenerateRandomDataPoints(int count, int seed = 0, int groupSize = 10)
85+
private static IEnumerable<DataPoint> GenerateRandomDataPoints(int count,
86+
int seed = 0, int groupSize = 10)
7987
{
8088
var random = new Random(seed);
8189
float randomFloat() => (float)random.NextDouble();
@@ -87,13 +95,16 @@ private static IEnumerable<DataPoint> GenerateRandomDataPoints(int count, int se
8795
Label = (uint)label,
8896
GroupId = (uint)(i / groupSize),
8997
// Create random features that are correlated with the label.
90-
// For data points with larger labels, the feature values are slightly increased by adding a constant.
91-
Features = Enumerable.Repeat(label, 50).Select(x => randomFloat() + x * 0.1f).ToArray()
98+
// For data points with larger labels, the feature values are
99+
// slightly increased by adding a constant.
100+
Features = Enumerable.Repeat(label, 50).Select(
101+
x => randomFloat() + x * 0.1f).ToArray()
92102
};
93103
}
94104
}
95105

96-
// Example with label, groupId, and 50 feature values. A data set is a collection of such examples.
106+
// Example with label, groupId, and 50 feature values. A data set is a
107+
// collection of such examples.
97108
private class DataPoint
98109
{
99110
[KeyType(5)]
@@ -116,8 +127,12 @@ private class Prediction
116127
// Pretty-print RankerMetrics objects.
117128
public static void PrintMetrics(RankingMetrics metrics)
118129
{
119-
Console.WriteLine($"DCG: {string.Join(", ", metrics.DiscountedCumulativeGains.Select((d, i) => $"@{i + 1}:{d:F2}").ToArray())}");
120-
Console.WriteLine($"NDCG: {string.Join(", ", metrics.NormalizedDiscountedCumulativeGains.Select((d, i) => $"@{i + 1}:{d:F2}").ToArray())}");
130+
Console.WriteLine("DCG: " + string.Join(", ",
131+
metrics.DiscountedCumulativeGains.Select(
132+
(d, i) => (i + 1) + ":" + d + ":F2").ToArray()));
133+
Console.WriteLine("NDCG: " + string.Join(", ",
134+
metrics.NormalizedDiscountedCumulativeGains.Select(
135+
(d, i) => (i + 1) + ":" + d + ":F2").ToArray()));
121136
}
122137
}
123138
}

docs/samples/Microsoft.ML.Samples/Dynamic/Trainers/Ranking/FastTreeWithOptions.tt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ string TrainerOptions = @"FastTreeRankingTrainer.Options
1616

1717
string OptionsInclude = "using Microsoft.ML.Trainers.FastTree;";
1818
string Comments= @"
19-
// This example requires installation of additional NuGet package
20-
// <a href=""https://www.nuget.org/packages/Microsoft.ML.FastTree/"">Microsoft.ML.FastTree</a>.";
19+
// This example requires installation of additional NuGet package for
20+
// Microsoft.ML.FastTree at
21+
// https://www.nuget.org/packages/Microsoft.ML.FastTree/";
2122

2223
string ExpectedOutputPerInstance = @"// Expected output:
2324
// Label: 5, Score: 8.807633

docs/samples/Microsoft.ML.Samples/Dynamic/Trainers/Ranking/LightGbm.cs

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,22 @@ namespace Samples.Dynamic.Trainers.Ranking
88
{
99
public static class LightGbm
1010
{
11+
// This example requires installation of additional NuGet package for
12+
// Microsoft.ML.FastTree at
13+
// https://www.nuget.org/packages/Microsoft.ML.FastTree/
1114
public static void Example()
1215
{
13-
// Create a new context for ML.NET operations. It can be used for exception tracking and logging,
14-
// as a catalog of available operations and as the source of randomness.
15-
// Setting the seed to a fixed number in this example to make outputs deterministic.
16+
// Create a new context for ML.NET operations. It can be used for
17+
// exception tracking and logging, as a catalog of available operations
18+
// and as the source of randomness. Setting the seed to a fixed number
19+
// in this example to make outputs deterministic.
1620
var mlContext = new MLContext(seed: 0);
1721

1822
// Create a list of training data points.
1923
var dataPoints = GenerateRandomDataPoints(1000);
2024

21-
// Convert the list of data points to an IDataView object, which is consumable by ML.NET API.
25+
// Convert the list of data points to an IDataView object, which is
26+
// consumable by ML.NET API.
2227
var trainingData = mlContext.Data.LoadFromEnumerable(dataPoints);
2328

2429
// Define the trainer.
@@ -27,39 +32,44 @@ public static void Example()
2732
// Train the model.
2833
var model = pipeline.Fit(trainingData);
2934

30-
// Create testing data. Use different random seed to make it different from training data.
31-
var testData = mlContext.Data.LoadFromEnumerable(GenerateRandomDataPoints(500, seed:123));
35+
// Create testing data. Use different random seed to make it different
36+
// from training data.
37+
var testData = mlContext.Data.LoadFromEnumerable(
38+
GenerateRandomDataPoints(500, seed:123));
3239

3340
// Run the model on test data set.
3441
var transformedTestData = model.Transform(testData);
3542

3643
// Take the top 5 rows.
37-
var topTransformedTestData = mlContext.Data.TakeRows(transformedTestData, 5);
44+
var topTransformedTestData = mlContext.Data.TakeRows(
45+
transformedTestData, 5);
3846

3947
// Convert IDataView object to a list.
40-
var predictions = mlContext.Data.CreateEnumerable<Prediction>(topTransformedTestData, reuseRowObject: false).ToList();
48+
var predictions = mlContext.Data.CreateEnumerable<Prediction>(
49+
topTransformedTestData, reuseRowObject: false).ToList();
4150

4251
// Print 5 predictions.
4352
foreach (var p in predictions)
4453
Console.WriteLine($"Label: {p.Label}, Score: {p.Score}");
4554

4655
// Expected output:
47-
// Label: 5, Score: 2.195333
48-
// Label: 4, Score: 0.2596574
49-
// Label: 4, Score: -2.168355
50-
// Label: 1, Score: -3.074823
51-
// Label: 1, Score: -1.523607
56+
// Label: 5, Score: 2.493263
57+
// Label: 1, Score: -4.528436
58+
// Label: 3, Score: -3.002865
59+
// Label: 3, Score: -2.151812
60+
// Label: 1, Score: -4.089102
5261

5362
// Evaluate the overall metrics.
5463
var metrics = mlContext.Ranking.Evaluate(transformedTestData);
5564
PrintMetrics(metrics);
5665

5766
// Expected output:
58-
// DCG: @1:26.03, @2:37.57, @3:45.83
59-
// NDCG: @1:0.61, @2:0.57, @3:0.59
67+
// DCG: @1:41.95, @2:63.76, @3:75.97
68+
// NDCG: @1:0.99, @2:0.99, @3:0.99
6069
}
6170

62-
private static IEnumerable<DataPoint> GenerateRandomDataPoints(int count, int seed = 0, int groupSize = 10)
71+
private static IEnumerable<DataPoint> GenerateRandomDataPoints(int count,
72+
int seed = 0, int groupSize = 10)
6373
{
6474
var random = new Random(seed);
6575
float randomFloat() => (float)random.NextDouble();
@@ -71,13 +81,16 @@ private static IEnumerable<DataPoint> GenerateRandomDataPoints(int count, int se
7181
Label = (uint)label,
7282
GroupId = (uint)(i / groupSize),
7383
// Create random features that are correlated with the label.
74-
// For data points with larger labels, the feature values are slightly increased by adding a constant.
75-
Features = Enumerable.Repeat(label, 50).Select(x => randomFloat() + x * 0.1f).ToArray()
84+
// For data points with larger labels, the feature values are
85+
// slightly increased by adding a constant.
86+
Features = Enumerable.Repeat(label, 50).Select(
87+
x => randomFloat() + x * 0.1f).ToArray()
7688
};
7789
}
7890
}
7991

80-
// Example with label, groupId, and 50 feature values. A data set is a collection of such examples.
92+
// Example with label, groupId, and 50 feature values. A data set is a
93+
// collection of such examples.
8194
private class DataPoint
8295
{
8396
[KeyType(5)]
@@ -100,8 +113,12 @@ private class Prediction
100113
// Pretty-print RankerMetrics objects.
101114
public static void PrintMetrics(RankingMetrics metrics)
102115
{
103-
Console.WriteLine($"DCG: {string.Join(", ", metrics.DiscountedCumulativeGains.Select((d, i) => $"@{i + 1}:{d:F2}").ToArray())}");
104-
Console.WriteLine($"NDCG: {string.Join(", ", metrics.NormalizedDiscountedCumulativeGains.Select((d, i) => $"@{i + 1}:{d:F2}").ToArray())}");
116+
Console.WriteLine("DCG: " + string.Join(", ",
117+
metrics.DiscountedCumulativeGains.Select(
118+
(d, i) => (i + 1) + ":" + d + ":F2").ToArray()));
119+
Console.WriteLine("NDCG: " + string.Join(", ",
120+
metrics.NormalizedDiscountedCumulativeGains.Select(
121+
(d, i) => (i + 1) + ":" + d + ":F2").ToArray()));
105122
}
106123
}
107124
}

docs/samples/Microsoft.ML.Samples/Dynamic/Trainers/Ranking/LightGbm.tt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ string TrainerOptions = null;
66

77
string OptionsInclude = "";
88
string Comments= @"
9-
// This example requires installation of additional NuGet package
10-
// <a href=""https://www.nuget.org/packages/Microsoft.ML.LightGbm/"">Microsoft.ML.LightGbm</a>.";
9+
// This example requires installation of additional NuGet package for
10+
// Microsoft.ML.FastTree at
11+
// https://www.nuget.org/packages/Microsoft.ML.FastTree/";
1112

1213
string ExpectedOutputPerInstance = @"// Expected output:
1314
// Label: 5, Score: 2.493263

0 commit comments

Comments
 (0)