Skip to content

Commit 4a41ee9

Browse files
sierralee51Dmitry-A
authored andcommitted
Reformatting MulticlassClassification samples to width 85 (dotnet#3942)
* reformatted multiclassclassification samples * fixing errors * reformatted MulticlassClassification samples * Update LbfgsMaximumEntropy.cs getting rid of whitespace * Update LbfgsMaximumEntropy.cs * Update LbfgsMaximumEntropyWithOptions.cs getting rid of whitespace * Update LightGbmWithOptions.cs fixing whitespace * Update LbfgsMaximumEntropy.cs * Update LightGbm.cs fixing whitespace * Update LightGbm.cs fixing whitespace * Update LightGbmWithOptions.cs fixing whitespace * Update MulticlassClassification.ttinclude fixing whitespace * Update MulticlassClassification.ttinclude fixing whitespace * Update NaiveBayes.cs fixing whitespace * Update NaiveBayes.tt fixing whitespace * Update NaiveBayes.tt * Update OneVersusAll.cs fixing whitespace * Update PairwiseCoupling.cs fixing whitespace * Update SdcaMaximumEntropy.cs fixing whitespace * Update SdcaMaximumEntropyWithOptions.cs fixing whitespace * Update SdcaNonCalibrated.cs fixing whitespace * Update SdcaNonCalibratedWithOptions.cs fixing whitespace * Update SdcaNonCalibrated.cs fixing whitespace * Update SdcaNonCalibrated.cs * Update LbfgsMaximumEntropy.cs * Update LbfgsMaximumEntropy.cs * Update LbfgsMaximumEntropyWithOptions.cs * Update LightGbm.cs * Update LightGbmWithOptions.cs * Update MulticlassClassification.ttinclude * Update NaiveBayes.cs * Update OneVersusAll.cs * Update PairwiseCoupling.cs * Update SdcaMaximumEntropy.cs * Update SdcaMaximumEntropy.cs * Update SdcaMaximumEntropyWithOptions.cs * Update SdcaNonCalibrated.cs * Update SdcaNonCalibratedWithOptions.cs * fixed tabbing issue * fixed indentations * aligned comments * fixed some indentation and spacing issues * fixed extra empty lines * fixed some more indentation issue
1 parent 8b60fba commit 4a41ee9

25 files changed

+649
-336
lines changed

docs/samples/Microsoft.ML.Samples/Dynamic/Trainers/MulticlassClassification/LbfgsMaximumEntropy.cs

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,39 +10,48 @@ public static class LbfgsMaximumEntropy
1010
{
1111
public static void Example()
1212
{
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.
13+
// Create a new context for ML.NET operations. It can be used for
14+
// exception tracking and logging, as a catalog of available operations
15+
// and as the source of randomness. Setting the seed to a fixed number
16+
// in this example to make outputs deterministic.
1617
var mlContext = new MLContext(seed: 0);
1718

1819
// Create a list of training data points.
1920
var dataPoints = GenerateRandomDataPoints(1000);
2021

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

2426
// Define the trainer.
2527
var pipeline =
26-
// Convert the string labels into key types.
27-
mlContext.Transforms.Conversion.MapValueToKey(nameof(DataPoint.Label))
28-
// Apply LbfgsMaximumEntropy multiclass trainer.
29-
.Append(mlContext.MulticlassClassification.Trainers.LbfgsMaximumEntropy());
28+
// Convert the string labels into key types.
29+
mlContext.Transforms.Conversion
30+
.MapValueToKey(nameof(DataPoint.Label))
31+
// Apply LbfgsMaximumEntropy multiclass trainer.
32+
.Append(mlContext.MulticlassClassification.Trainers
33+
.LbfgsMaximumEntropy());
3034

3135
// Train the model.
3236
var model = pipeline.Fit(trainingData);
3337

34-
// Create testing data. Use different random seed to make it different from training data.
35-
var testData = mlContext.Data.LoadFromEnumerable(GenerateRandomDataPoints(500, seed: 123));
38+
// Create testing data. Use different random seed to make it different
39+
// from training data.
40+
var testData = mlContext.Data
41+
.LoadFromEnumerable(GenerateRandomDataPoints(500, seed: 123));
3642

3743
// Run the model on test data set.
3844
var transformedTestData = model.Transform(testData);
3945

4046
// Convert IDataView object to a list.
41-
var predictions = mlContext.Data.CreateEnumerable<Prediction>(transformedTestData, reuseRowObject: false).ToList();
47+
var predictions = mlContext.Data
48+
.CreateEnumerable<Prediction>(transformedTestData,
49+
reuseRowObject: false).ToList();
4250

4351
// Look at 5 predictions
4452
foreach (var p in predictions.Take(5))
45-
Console.WriteLine($"Label: {p.Label}, Prediction: {p.PredictedLabel}");
53+
Console.WriteLine($"Label: {p.Label}, " +
54+
$"Prediction: {p.PredictedLabel}");
4655

4756
// Expected output:
4857
// Label: 1, Prediction: 1
@@ -52,7 +61,9 @@ public static void Example()
5261
// Label: 3, Prediction: 3
5362

5463
// Evaluate the overall metrics
55-
var metrics = mlContext.MulticlassClassification.Evaluate(transformedTestData);
64+
var metrics = mlContext.MulticlassClassification
65+
.Evaluate(transformedTestData);
66+
5667
PrintMetrics(metrics);
5768

5869
// Expected output:
@@ -72,8 +83,11 @@ public static void Example()
7283
// Precision ||0.9308 |0.9593 |0.8580 |
7384
}
7485

75-
// Generates random uniform doubles in [-0.5, 0.5) range with labels 1, 2 or 3.
76-
private static IEnumerable<DataPoint> GenerateRandomDataPoints(int count, int seed=0)
86+
// Generates random uniform doubles in [-0.5, 0.5)
87+
// range with labels 1, 2 or 3.
88+
private static IEnumerable<DataPoint> GenerateRandomDataPoints(int count,
89+
int seed=0)
90+
7791
{
7892
var random = new Random(seed);
7993
float randomFloat() => (float)(random.NextDouble() - 0.5);
@@ -85,13 +99,17 @@ private static IEnumerable<DataPoint> GenerateRandomDataPoints(int count, int se
8599
{
86100
Label = (uint)label,
87101
// Create random features that are correlated with the label.
88-
// The feature values are slightly increased by adding a constant multiple of label.
89-
Features = Enumerable.Repeat(label, 20).Select(x => randomFloat() + label * 0.2f).ToArray()
102+
// The feature values are slightly increased by adding a
103+
// constant multiple of label.
104+
Features = Enumerable.Repeat(label, 20)
105+
.Select(x => randomFloat() + label * 0.2f).ToArray()
106+
90107
};
91108
}
92109
}
93110

94-
// Example with label and 20 feature values. A data set is a collection of such examples.
111+
// Example with label and 20 feature values. A data set is a collection of
112+
// such examples.
95113
private class DataPoint
96114
{
97115
public uint Label { get; set; }
@@ -114,8 +132,11 @@ public static void PrintMetrics(MulticlassClassificationMetrics metrics)
114132
Console.WriteLine($"Micro Accuracy: {metrics.MicroAccuracy:F2}");
115133
Console.WriteLine($"Macro Accuracy: {metrics.MacroAccuracy:F2}");
116134
Console.WriteLine($"Log Loss: {metrics.LogLoss:F2}");
117-
Console.WriteLine($"Log Loss Reduction: {metrics.LogLossReduction:F2}\n");
135+
Console.WriteLine(
136+
$"Log Loss Reduction: {metrics.LogLossReduction:F2}\n");
137+
118138
Console.WriteLine(metrics.ConfusionMatrix.GetFormattedConfusionTable());
119139
}
120140
}
121141
}
142+

docs/samples/Microsoft.ML.Samples/Dynamic/Trainers/MulticlassClassification/LbfgsMaximumEntropy.tt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ string TrainerOptions = null;
88
string OptionsInclude = "";
99
string Comments = "";
1010
bool CacheData = false;
11-
string DataGenerationComments= "// Generates random uniform doubles in [-0.5, 0.5) range with labels 1, 2 or 3.";
11+
string DataGenerationComments= "// Generates random uniform doubles in [-0.5, 0.5)"
12+
+ "\n // range with labels 1, 2 or 3.";
1213

1314
string ExpectedOutputPerInstance = @"// Expected output:
1415
// Label: 1, Prediction: 1

docs/samples/Microsoft.ML.Samples/Dynamic/Trainers/MulticlassClassification/LbfgsMaximumEntropyWithOptions.cs

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,17 @@ public static class LbfgsMaximumEntropyWithOptions
1111
{
1212
public static void Example()
1313
{
14-
// Create a new context for ML.NET operations. It can be used for exception tracking and logging,
15-
// as a catalog of available operations and as the source of randomness.
16-
// Setting the seed to a fixed number in this example to make outputs deterministic.
14+
// Create a new context for ML.NET operations. It can be used for
15+
// exception tracking and logging, as a catalog of available operations
16+
// and as the source of randomness. Setting the seed to a fixed number
17+
// in this example to make outputs deterministic.
1718
var mlContext = new MLContext(seed: 0);
1819

1920
// Create a list of training data points.
2021
var dataPoints = GenerateRandomDataPoints(1000);
2122

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

2527
// Define trainer options.
@@ -32,27 +34,32 @@ public static void Example()
3234

3335
// Define the trainer.
3436
var pipeline =
35-
// Convert the string labels into key types.
36-
mlContext.Transforms.Conversion.MapValueToKey("Label")
37-
// Apply LbfgsMaximumEntropy multiclass trainer.
38-
.Append(mlContext.MulticlassClassification.Trainers.LbfgsMaximumEntropy(options));
39-
37+
// Convert the string labels into key types.
38+
mlContext.Transforms.Conversion.MapValueToKey("Label")
39+
// Apply LbfgsMaximumEntropy multiclass trainer.
40+
.Append(mlContext.MulticlassClassification.Trainers
41+
.LbfgsMaximumEntropy(options));
4042

4143
// Train the model.
4244
var model = pipeline.Fit(trainingData);
4345

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

4751
// Run the model on test data set.
4852
var transformedTestData = model.Transform(testData);
4953

5054
// Convert IDataView object to a list.
51-
var predictions = mlContext.Data.CreateEnumerable<Prediction>(transformedTestData, reuseRowObject: false).ToList();
55+
var predictions = mlContext.Data
56+
.CreateEnumerable<Prediction>(transformedTestData,
57+
reuseRowObject: false).ToList();
5258

5359
// Look at 5 predictions
5460
foreach (var p in predictions.Take(5))
55-
Console.WriteLine($"Label: {p.Label}, Prediction: {p.PredictedLabel}");
61+
Console.WriteLine($"Label: {p.Label}, " +
62+
$"Prediction: {p.PredictedLabel}");
5663

5764
// Expected output:
5865
// Label: 1, Prediction: 1
@@ -62,7 +69,9 @@ public static void Example()
6269
// Label: 3, Prediction: 3
6370

6471
// Evaluate the overall metrics
65-
var metrics = mlContext.MulticlassClassification.Evaluate(transformedTestData);
72+
var metrics = mlContext.MulticlassClassification
73+
.Evaluate(transformedTestData);
74+
6675
PrintMetrics(metrics);
6776

6877
// Expected output:
@@ -82,8 +91,11 @@ public static void Example()
8291
// Precision ||0.9304 |0.9593 |0.8529 |
8392
}
8493

85-
// Generates random uniform doubles in [-0.5, 0.5) range with labels 1, 2 or 3.
86-
private static IEnumerable<DataPoint> GenerateRandomDataPoints(int count, int seed=0)
94+
// Generates random uniform doubles in [-0.5, 0.5)
95+
// range with labels 1, 2 or 3.
96+
private static IEnumerable<DataPoint> GenerateRandomDataPoints(int count,
97+
int seed=0)
98+
8799
{
88100
var random = new Random(seed);
89101
float randomFloat() => (float)(random.NextDouble() - 0.5);
@@ -95,13 +107,17 @@ private static IEnumerable<DataPoint> GenerateRandomDataPoints(int count, int se
95107
{
96108
Label = (uint)label,
97109
// Create random features that are correlated with the label.
98-
// The feature values are slightly increased by adding a constant multiple of label.
99-
Features = Enumerable.Repeat(label, 20).Select(x => randomFloat() + label * 0.2f).ToArray()
110+
// The feature values are slightly increased by adding a
111+
// constant multiple of label.
112+
Features = Enumerable.Repeat(label, 20)
113+
.Select(x => randomFloat() + label * 0.2f).ToArray()
114+
100115
};
101116
}
102117
}
103118

104-
// Example with label and 20 feature values. A data set is a collection of such examples.
119+
// Example with label and 20 feature values. A data set is a collection of
120+
// such examples.
105121
private class DataPoint
106122
{
107123
public uint Label { get; set; }
@@ -124,8 +140,11 @@ public static void PrintMetrics(MulticlassClassificationMetrics metrics)
124140
Console.WriteLine($"Micro Accuracy: {metrics.MicroAccuracy:F2}");
125141
Console.WriteLine($"Macro Accuracy: {metrics.MacroAccuracy:F2}");
126142
Console.WriteLine($"Log Loss: {metrics.LogLoss:F2}");
127-
Console.WriteLine($"Log Loss Reduction: {metrics.LogLossReduction:F2}\n");
143+
Console.WriteLine(
144+
$"Log Loss Reduction: {metrics.LogLossReduction:F2}\n");
145+
128146
Console.WriteLine(metrics.ConfusionMatrix.GetFormattedConfusionTable());
129147
}
130148
}
131149
}
150+

docs/samples/Microsoft.ML.Samples/Dynamic/Trainers/MulticlassClassification/LbfgsMaximumEntropyWithOptions.tt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ string TrainerOptions = @"LbfgsMaximumEntropyMulticlassTrainer.Options
1212

1313
string OptionsInclude = "using Microsoft.ML.Trainers;";
1414
string Comments = "";
15-
string DataGenerationComments= "// Generates random uniform doubles in [-0.5, 0.5) range with labels 1, 2 or 3.";
15+
string DataGenerationComments= "// Generates random uniform doubles in [-0.5, 0.5)"
16+
+ "\n // range with labels 1, 2 or 3.";
17+
1618
bool CacheData = false;
1719

1820
string ExpectedOutputPerInstance = @"// Expected output:

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

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,43 +8,53 @@ namespace Samples.Dynamic.Trainers.MulticlassClassification
88
{
99
public static class LightGbm
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.
2730
var pipeline =
28-
// Convert the string labels into key types.
29-
mlContext.Transforms.Conversion.MapValueToKey(nameof(DataPoint.Label))
30-
// Apply LightGbm multiclass trainer.
31-
.Append(mlContext.MulticlassClassification.Trainers.LightGbm());
31+
// Convert the string labels into key types.
32+
mlContext.Transforms.Conversion
33+
.MapValueToKey(nameof(DataPoint.Label))
34+
// Apply LightGbm multiclass trainer.
35+
.Append(mlContext.MulticlassClassification.Trainers
36+
.LightGbm());
3237

3338
// Train the model.
3439
var model = pipeline.Fit(trainingData);
3540

36-
// Create testing data. Use different random seed to make it different from training data.
37-
var testData = mlContext.Data.LoadFromEnumerable(GenerateRandomDataPoints(500, seed: 123));
41+
// Create testing data. Use different random seed to make it different
42+
// from training data.
43+
var testData = mlContext.Data
44+
.LoadFromEnumerable(GenerateRandomDataPoints(500, seed: 123));
3845

3946
// Run the model on test data set.
4047
var transformedTestData = model.Transform(testData);
4148

4249
// Convert IDataView object to a list.
43-
var predictions = mlContext.Data.CreateEnumerable<Prediction>(transformedTestData, reuseRowObject: false).ToList();
50+
var predictions = mlContext.Data
51+
.CreateEnumerable<Prediction>(transformedTestData,
52+
reuseRowObject: false).ToList();
4453

4554
// Look at 5 predictions
4655
foreach (var p in predictions.Take(5))
47-
Console.WriteLine($"Label: {p.Label}, Prediction: {p.PredictedLabel}");
56+
Console.WriteLine($"Label: {p.Label}, " +
57+
$"Prediction: {p.PredictedLabel}");
4858

4959
// Expected output:
5060
// Label: 1, Prediction: 1
@@ -54,7 +64,9 @@ public static void Example()
5464
// Label: 3, Prediction: 3
5565

5666
// Evaluate the overall metrics
57-
var metrics = mlContext.MulticlassClassification.Evaluate(transformedTestData);
67+
var metrics = mlContext.MulticlassClassification
68+
.Evaluate(transformedTestData);
69+
5870
PrintMetrics(metrics);
5971

6072
// Expected output:
@@ -74,8 +86,11 @@ public static void Example()
7486
// Precision ||0.9936 |1.0000 |0.9701 |
7587
}
7688

77-
// Generates random uniform doubles in [-0.5, 0.5) range with labels 1, 2 or 3.
78-
private static IEnumerable<DataPoint> GenerateRandomDataPoints(int count, int seed=0)
89+
// Generates random uniform doubles in [-0.5, 0.5)
90+
// range with labels 1, 2 or 3.
91+
private static IEnumerable<DataPoint> GenerateRandomDataPoints(int count,
92+
int seed=0)
93+
7994
{
8095
var random = new Random(seed);
8196
float randomFloat() => (float)(random.NextDouble() - 0.5);
@@ -87,13 +102,17 @@ private static IEnumerable<DataPoint> GenerateRandomDataPoints(int count, int se
87102
{
88103
Label = (uint)label,
89104
// Create random features that are correlated with the label.
90-
// The feature values are slightly increased by adding a constant multiple of label.
91-
Features = Enumerable.Repeat(label, 20).Select(x => randomFloat() + label * 0.2f).ToArray()
105+
// The feature values are slightly increased by adding a
106+
// constant multiple of label.
107+
Features = Enumerable.Repeat(label, 20)
108+
.Select(x => randomFloat() + label * 0.2f).ToArray()
109+
92110
};
93111
}
94112
}
95113

96-
// Example with label and 20 feature values. A data set is a collection of such examples.
114+
// Example with label and 20 feature values. A data set is a collection of
115+
// such examples.
97116
private class DataPoint
98117
{
99118
public uint Label { get; set; }
@@ -116,8 +135,11 @@ public static void PrintMetrics(MulticlassClassificationMetrics metrics)
116135
Console.WriteLine($"Micro Accuracy: {metrics.MicroAccuracy:F2}");
117136
Console.WriteLine($"Macro Accuracy: {metrics.MacroAccuracy:F2}");
118137
Console.WriteLine($"Log Loss: {metrics.LogLoss:F2}");
119-
Console.WriteLine($"Log Loss Reduction: {metrics.LogLossReduction:F2}\n");
138+
Console.WriteLine(
139+
$"Log Loss Reduction: {metrics.LogLossReduction:F2}\n");
140+
120141
Console.WriteLine(metrics.ConfusionMatrix.GetFormattedConfusionTable());
121142
}
122143
}
123144
}
145+

0 commit comments

Comments
 (0)