-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Reformatting MulticlassClassification samples to width 85 #3942
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
539a0f7
d18c419
0b4aff6
f84939f
130c6da
71e4c19
8166627
5ea92f3
b63c891
4d37328
6421a8b
79093c8
4c2f723
4a48dc5
cc1b3f8
c6d4cfd
97b3c99
6be7d8a
3f74187
0d23ad8
c728577
6e82a10
1f1ff3b
239e26a
32db971
e3e595b
969af4d
b18c508
1af1554
d0e1a01
092808d
e943230
9d014e3
407c4d8
b4c9575
053c85b
0d2baa6
6e410cc
278b743
85ed0e6
f51c147
1e9a3d8
c3d3499
e497794
6224ed5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,39 +10,48 @@ public static class LbfgsMaximumEntropy | |
{ | ||
public static void Example() | ||
{ | ||
// Create a new context for ML.NET operations. It can be used for exception tracking and logging, | ||
// as a catalog of available operations and as the source of randomness. | ||
// Setting the seed to a fixed number in this example to make outputs deterministic. | ||
// Create a new context for ML.NET operations. It can be used for | ||
// exception tracking and logging, as a catalog of available operations | ||
// and as the source of randomness. Setting the seed to a fixed number | ||
// in this example to make outputs deterministic. | ||
var mlContext = new MLContext(seed: 0); | ||
|
||
// Create a list of training data points. | ||
var dataPoints = GenerateRandomDataPoints(1000); | ||
|
||
// Convert the list of data points to an IDataView object, which is consumable by ML.NET API. | ||
// Convert the list of data points to an IDataView object, which is | ||
// consumable by ML.NET API. | ||
var trainingData = mlContext.Data.LoadFromEnumerable(dataPoints); | ||
|
||
// Define the trainer. | ||
var pipeline = | ||
// Convert the string labels into key types. | ||
mlContext.Transforms.Conversion.MapValueToKey(nameof(DataPoint.Label)) | ||
// Apply LbfgsMaximumEntropy multiclass trainer. | ||
.Append(mlContext.MulticlassClassification.Trainers.LbfgsMaximumEntropy()); | ||
// Convert the string labels into key types. | ||
mlContext.Transforms.Conversion | ||
.MapValueToKey(nameof(DataPoint.Label)) | ||
// Apply LbfgsMaximumEntropy multiclass trainer. | ||
.Append(mlContext.MulticlassClassification.Trainers | ||
.LbfgsMaximumEntropy()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
// Train the model. | ||
var model = pipeline.Fit(trainingData); | ||
|
||
// Create testing data. Use different random seed to make it different from training data. | ||
var testData = mlContext.Data.LoadFromEnumerable(GenerateRandomDataPoints(500, seed: 123)); | ||
// Create testing data. Use different random seed to make it different | ||
// from training data. | ||
var testData = mlContext.Data | ||
.LoadFromEnumerable(GenerateRandomDataPoints(500, seed: 123)); | ||
|
||
// Run the model on test data set. | ||
var transformedTestData = model.Transform(testData); | ||
|
||
// Convert IDataView object to a list. | ||
var predictions = mlContext.Data.CreateEnumerable<Prediction>(transformedTestData, reuseRowObject: false).ToList(); | ||
var predictions = mlContext.Data | ||
.CreateEnumerable<Prediction>(transformedTestData, | ||
reuseRowObject: false).ToList(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📝 This is difficult to read. See preferred form in #3941 (comment) |
||
|
||
// Look at 5 predictions | ||
foreach (var p in predictions.Take(5)) | ||
Console.WriteLine($"Label: {p.Label}, Prediction: {p.PredictedLabel}"); | ||
Console.WriteLine($"Label: {p.Label}, " + | ||
$"Prediction: {p.PredictedLabel}"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📝 Multi-line body of |
||
|
||
// Expected output: | ||
// Label: 1, Prediction: 1 | ||
|
@@ -52,7 +61,9 @@ public static void Example() | |
// Label: 3, Prediction: 3 | ||
|
||
// Evaluate the overall metrics | ||
var metrics = mlContext.MulticlassClassification.Evaluate(transformedTestData); | ||
var metrics = mlContext.MulticlassClassification | ||
.Evaluate(transformedTestData); | ||
|
||
PrintMetrics(metrics); | ||
|
||
// Expected output: | ||
|
@@ -72,8 +83,11 @@ public static void Example() | |
// Precision ||0.9308 |0.9593 |0.8580 | | ||
} | ||
|
||
// Generates random uniform doubles in [-0.5, 0.5) range with labels 1, 2 or 3. | ||
private static IEnumerable<DataPoint> GenerateRandomDataPoints(int count, int seed=0) | ||
// Generates random uniform doubles in [-0.5, 0.5) | ||
// range with labels 1, 2 or 3. | ||
private static IEnumerable<DataPoint> GenerateRandomDataPoints(int count, | ||
int seed=0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📝 Wrap no arguments, or wrap all arguments with one argument per line |
||
|
||
{ | ||
var random = new Random(seed); | ||
float randomFloat() => (float)(random.NextDouble() - 0.5); | ||
|
@@ -85,13 +99,17 @@ private static IEnumerable<DataPoint> GenerateRandomDataPoints(int count, int se | |
{ | ||
Label = (uint)label, | ||
// Create random features that are correlated with the label. | ||
// The feature values are slightly increased by adding a constant multiple of label. | ||
Features = Enumerable.Repeat(label, 20).Select(x => randomFloat() + label * 0.2f).ToArray() | ||
// The feature values are slightly increased by adding a | ||
// constant multiple of label. | ||
Features = Enumerable.Repeat(label, 20) | ||
.Select(x => randomFloat() + label * 0.2f).ToArray() | ||
|
||
}; | ||
} | ||
} | ||
|
||
// Example with label and 20 feature values. A data set is a collection of such examples. | ||
// Example with label and 20 feature values. A data set is a collection of | ||
// such examples. | ||
private class DataPoint | ||
{ | ||
public uint Label { get; set; } | ||
|
@@ -114,8 +132,11 @@ public static void PrintMetrics(MulticlassClassificationMetrics metrics) | |
Console.WriteLine($"Micro Accuracy: {metrics.MicroAccuracy:F2}"); | ||
Console.WriteLine($"Macro Accuracy: {metrics.MacroAccuracy:F2}"); | ||
Console.WriteLine($"Log Loss: {metrics.LogLoss:F2}"); | ||
Console.WriteLine($"Log Loss Reduction: {metrics.LogLossReduction:F2}\n"); | ||
Console.WriteLine( | ||
$"Log Loss Reduction: {metrics.LogLossReduction:F2}\n"); | ||
|
||
Console.WriteLine(metrics.ConfusionMatrix.GetFormattedConfusionTable()); | ||
} | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,8 @@ string TrainerOptions = null; | |
string OptionsInclude = ""; | ||
string Comments = ""; | ||
bool CacheData = false; | ||
string DataGenerationComments= "// Generates random uniform doubles in [-0.5, 0.5) range with labels 1, 2 or 3."; | ||
string DataGenerationComments= "// Generates random uniform doubles in [-0.5, 0.5)" | ||
+ "\n // range with labels 1, 2 or 3."; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📝 Place |
||
|
||
string ExpectedOutputPerInstance = @"// Expected output: | ||
// Label: 1, Prediction: 1 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,15 +11,17 @@ public static class LbfgsMaximumEntropyWithOptions | |
{ | ||
public static void Example() | ||
{ | ||
// Create a new context for ML.NET operations. It can be used for exception tracking and logging, | ||
// as a catalog of available operations and as the source of randomness. | ||
// Setting the seed to a fixed number in this example to make outputs deterministic. | ||
// Create a new context for ML.NET operations. It can be used for | ||
// exception tracking and logging, as a catalog of available operations | ||
// and as the source of randomness. Setting the seed to a fixed number | ||
// in this example to make outputs deterministic. | ||
var mlContext = new MLContext(seed: 0); | ||
|
||
// Create a list of training data points. | ||
var dataPoints = GenerateRandomDataPoints(1000); | ||
|
||
// Convert the list of data points to an IDataView object, which is consumable by ML.NET API. | ||
// Convert the list of data points to an IDataView object, which is | ||
// consumable by ML.NET API. | ||
var trainingData = mlContext.Data.LoadFromEnumerable(dataPoints); | ||
|
||
// Define trainer options. | ||
|
@@ -32,27 +34,32 @@ public static void Example() | |
|
||
// Define the trainer. | ||
var pipeline = | ||
// Convert the string labels into key types. | ||
mlContext.Transforms.Conversion.MapValueToKey("Label") | ||
// Apply LbfgsMaximumEntropy multiclass trainer. | ||
.Append(mlContext.MulticlassClassification.Trainers.LbfgsMaximumEntropy(options)); | ||
// Convert the string labels into key types. | ||
mlContext.Transforms.Conversion.MapValueToKey("Label") | ||
// Apply LbfgsMaximumEntropy multiclass trainer. | ||
.Append(mlContext.MulticlassClassification.Trainers | ||
.LbfgsMaximumEntropy(options)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📝 See earlier comment regarding this wrapping |
||
|
||
// Train the model. | ||
var model = pipeline.Fit(trainingData); | ||
|
||
// Create testing data. Use different random seed to make it different from training data. | ||
var testData = mlContext.Data.LoadFromEnumerable(GenerateRandomDataPoints(500, seed: 123)); | ||
// Create testing data. Use different random seed to make it different | ||
// from training data. | ||
var testData = mlContext.Data | ||
.LoadFromEnumerable(GenerateRandomDataPoints(500, seed: 123)); | ||
|
||
// Run the model on test data set. | ||
var transformedTestData = model.Transform(testData); | ||
|
||
// Convert IDataView object to a list. | ||
var predictions = mlContext.Data.CreateEnumerable<Prediction>(transformedTestData, reuseRowObject: false).ToList(); | ||
var predictions = mlContext.Data | ||
.CreateEnumerable<Prediction>(transformedTestData, | ||
reuseRowObject: false).ToList(); | ||
|
||
// Look at 5 predictions | ||
foreach (var p in predictions.Take(5)) | ||
Console.WriteLine($"Label: {p.Label}, Prediction: {p.PredictedLabel}"); | ||
Console.WriteLine($"Label: {p.Label}, " + | ||
$"Prediction: {p.PredictedLabel}"); | ||
|
||
// Expected output: | ||
// Label: 1, Prediction: 1 | ||
|
@@ -62,7 +69,9 @@ public static void Example() | |
// Label: 3, Prediction: 3 | ||
|
||
// Evaluate the overall metrics | ||
var metrics = mlContext.MulticlassClassification.Evaluate(transformedTestData); | ||
var metrics = mlContext.MulticlassClassification | ||
.Evaluate(transformedTestData); | ||
|
||
PrintMetrics(metrics); | ||
|
||
// Expected output: | ||
|
@@ -82,8 +91,11 @@ public static void Example() | |
// Precision ||0.9304 |0.9593 |0.8529 | | ||
} | ||
|
||
// Generates random uniform doubles in [-0.5, 0.5) range with labels 1, 2 or 3. | ||
private static IEnumerable<DataPoint> GenerateRandomDataPoints(int count, int seed=0) | ||
// Generates random uniform doubles in [-0.5, 0.5) | ||
// range with labels 1, 2 or 3. | ||
private static IEnumerable<DataPoint> GenerateRandomDataPoints(int count, | ||
int seed=0) | ||
|
||
{ | ||
var random = new Random(seed); | ||
float randomFloat() => (float)(random.NextDouble() - 0.5); | ||
|
@@ -95,13 +107,17 @@ private static IEnumerable<DataPoint> GenerateRandomDataPoints(int count, int se | |
{ | ||
Label = (uint)label, | ||
// Create random features that are correlated with the label. | ||
// The feature values are slightly increased by adding a constant multiple of label. | ||
Features = Enumerable.Repeat(label, 20).Select(x => randomFloat() + label * 0.2f).ToArray() | ||
// The feature values are slightly increased by adding a | ||
// constant multiple of label. | ||
Features = Enumerable.Repeat(label, 20) | ||
.Select(x => randomFloat() + label * 0.2f).ToArray() | ||
|
||
}; | ||
} | ||
} | ||
|
||
// Example with label and 20 feature values. A data set is a collection of such examples. | ||
// Example with label and 20 feature values. A data set is a collection of | ||
// such examples. | ||
private class DataPoint | ||
{ | ||
public uint Label { get; set; } | ||
|
@@ -124,8 +140,11 @@ public static void PrintMetrics(MulticlassClassificationMetrics metrics) | |
Console.WriteLine($"Micro Accuracy: {metrics.MicroAccuracy:F2}"); | ||
Console.WriteLine($"Macro Accuracy: {metrics.MacroAccuracy:F2}"); | ||
Console.WriteLine($"Log Loss: {metrics.LogLoss:F2}"); | ||
Console.WriteLine($"Log Loss Reduction: {metrics.LogLossReduction:F2}\n"); | ||
Console.WriteLine( | ||
$"Log Loss Reduction: {metrics.LogLossReduction:F2}\n"); | ||
|
||
Console.WriteLine(metrics.ConfusionMatrix.GetFormattedConfusionTable()); | ||
} | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,9 @@ string TrainerOptions = @"LbfgsMaximumEntropyMulticlassTrainer.Options | |
|
||
string OptionsInclude = "using Microsoft.ML.Trainers;"; | ||
string Comments = ""; | ||
string DataGenerationComments= "// Generates random uniform doubles in [-0.5, 0.5) range with labels 1, 2 or 3."; | ||
string DataGenerationComments= "// Generates random uniform doubles in [-0.5, 0.5)" | ||
+ "\n // range with labels 1, 2 or 3."; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe indent the comment "// range with labels . . ." with the same indent as the comment above it "Generates random . . . " ? There are more instances of this, so I guess there's a tt file you'd have to change. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That isn't a comment. It's part of the string |
||
|
||
bool CacheData = false; | ||
|
||
string ExpectedOutputPerInstance = @"// Expected output: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📝 Line should be indented by 4 more spaces