-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Reformatting BinaryClassification samples to width 85 #3946
Changes from 58 commits
cdf6b36
7abb9d1
5c3214b
74935ee
e9891f9
c9dcd03
384f983
885f820
1d1679b
f3ec594
fb62ed5
c243e2e
5458e79
60130f3
edcb4f5
3d8613e
5aabe0e
85036e2
92c6c19
f0990a1
672b785
c41905d
e93c699
cbbee59
2fc5e20
a43123c
58484e2
a42c2e8
a9afb72
e780b0d
617a5c5
f95144b
ff86eca
5b7734e
b0c0813
92d564d
061a83a
0402c90
5c28cf6
d39a4a2
fef50fe
deac2b1
a808724
31f95e3
1b05112
47481ed
371e043
254fdef
b860f30
664aa71
f6871c8
539fe74
ed51348
d112945
3af17a8
93a98fe
652f39e
0857a13
6eac51a
109a4cd
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,35 +10,44 @@ public static class AveragedPerceptron | |
{ | ||
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 = mlContext.BinaryClassification.Trainers.AveragedPerceptron(); | ||
var pipeline = mlContext.BinaryClassification.Trainers | ||
.AveragedPerceptron(); | ||
|
||
|
||
// 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(); | ||
|
||
// Print 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: True, Prediction: True | ||
|
@@ -48,7 +57,9 @@ public static void Example() | |
// Label: False, Prediction: False | ||
|
||
// Evaluate the overall metrics. | ||
var metrics = mlContext.BinaryClassification.EvaluateNonCalibrated(transformedTestData); | ||
var metrics = mlContext.BinaryClassification | ||
.EvaluateNonCalibrated(transformedTestData); | ||
|
||
PrintMetrics(metrics); | ||
|
||
// Expected output: | ||
|
@@ -71,7 +82,9 @@ public static void Example() | |
// Precision || 0.7402 | 0.7061 | | ||
} | ||
|
||
private static IEnumerable<DataPoint> GenerateRandomDataPoints(int count, int seed=0) | ||
private static IEnumerable<DataPoint> GenerateRandomDataPoints(int count, | ||
int seed=0) | ||
|
||
{ | ||
var random = new Random(seed); | ||
float randomFloat() => (float)random.NextDouble(); | ||
|
@@ -82,13 +95,18 @@ private static IEnumerable<DataPoint> GenerateRandomDataPoints(int count, int se | |
{ | ||
Label = label, | ||
// Create random features that are correlated with the label. | ||
// For data points with false label, the feature values are slightly increased by adding a constant. | ||
Features = Enumerable.Repeat(label, 50).Select(x => x ? randomFloat() : randomFloat() + 0.1f).ToArray() | ||
// For data points with false label, the feature values are | ||
// slightly increased by adding a constant. | ||
Features = Enumerable.Repeat(label, 50) | ||
.Select(x => x ? randomFloat() : randomFloat() + | ||
0.1f).ToArray() | ||
|
||
}; | ||
} | ||
} | ||
|
||
// Example with label and 50 feature values. A data set is a collection of such examples. | ||
// Example with label and 50 feature values. A data set is a collection of | ||
// such examples. | ||
private class DataPoint | ||
{ | ||
public bool Label { get; set; } | ||
|
@@ -111,11 +129,16 @@ private static void PrintMetrics(BinaryClassificationMetrics metrics) | |
Console.WriteLine($"Accuracy: {metrics.Accuracy:F2}"); | ||
Console.WriteLine($"AUC: {metrics.AreaUnderRocCurve:F2}"); | ||
Console.WriteLine($"F1 Score: {metrics.F1Score:F2}"); | ||
Console.WriteLine($"Negative Precision: {metrics.NegativePrecision:F2}"); | ||
Console.WriteLine($"Negative Precision: {metrics.NegativePrecision:F2}") | ||
; | ||
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. I don't think this is the best formatting. Maybe do it like this:
|
||
|
||
Console.WriteLine($"Negative Recall: {metrics.NegativeRecall:F2}"); | ||
Console.WriteLine($"Positive Precision: {metrics.PositivePrecision:F2}"); | ||
Console.WriteLine($"Positive Precision: {metrics.PositivePrecision:F2}") | ||
; | ||
|
||
Console.WriteLine($"Positive Recall: {metrics.PositiveRecall:F2}\n"); | ||
Console.WriteLine(metrics.ConfusionMatrix.GetFormattedConfusionTable()); | ||
} | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,15 +11,17 @@ public static class AveragedPerceptronWithOptions | |
{ | ||
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. | ||
|
@@ -33,23 +35,30 @@ public static void Example() | |
}; | ||
|
||
// Define the trainer. | ||
var pipeline = mlContext.BinaryClassification.Trainers.AveragedPerceptron(options); | ||
var pipeline = mlContext.BinaryClassification.Trainers | ||
.AveragedPerceptron(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. extra line 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. yeah noticed that a couple minutes ago...looking for the tt/ttinclude file that is probably causing it |
||
|
||
// 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(); | ||
|
||
// Print 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: True, Prediction: True | ||
|
@@ -59,7 +68,9 @@ public static void Example() | |
// Label: False, Prediction: False | ||
|
||
// Evaluate the overall metrics. | ||
var metrics = mlContext.BinaryClassification.EvaluateNonCalibrated(transformedTestData); | ||
var metrics = mlContext.BinaryClassification | ||
.EvaluateNonCalibrated(transformedTestData); | ||
|
||
PrintMetrics(metrics); | ||
|
||
// Expected output: | ||
|
@@ -82,7 +93,9 @@ public static void Example() | |
// Precision || 0.7402 | 0.7061 | | ||
} | ||
|
||
private static IEnumerable<DataPoint> GenerateRandomDataPoints(int count, int seed=0) | ||
private static IEnumerable<DataPoint> GenerateRandomDataPoints(int count, | ||
int seed=0) | ||
|
||
{ | ||
var random = new Random(seed); | ||
float randomFloat() => (float)random.NextDouble(); | ||
|
@@ -93,13 +106,18 @@ private static IEnumerable<DataPoint> GenerateRandomDataPoints(int count, int se | |
{ | ||
Label = label, | ||
// Create random features that are correlated with the label. | ||
// For data points with false label, the feature values are slightly increased by adding a constant. | ||
Features = Enumerable.Repeat(label, 50).Select(x => x ? randomFloat() : randomFloat() + 0.1f).ToArray() | ||
// For data points with false label, the feature values are | ||
// slightly increased by adding a constant. | ||
Features = Enumerable.Repeat(label, 50) | ||
.Select(x => x ? randomFloat() : randomFloat() + | ||
0.1f).ToArray() | ||
|
||
}; | ||
} | ||
} | ||
|
||
// Example with label and 50 feature values. A data set is a collection of such examples. | ||
// Example with label and 50 feature values. A data set is a collection of | ||
// such examples. | ||
private class DataPoint | ||
{ | ||
public bool Label { get; set; } | ||
|
@@ -122,11 +140,16 @@ private static void PrintMetrics(BinaryClassificationMetrics metrics) | |
Console.WriteLine($"Accuracy: {metrics.Accuracy:F2}"); | ||
Console.WriteLine($"AUC: {metrics.AreaUnderRocCurve:F2}"); | ||
Console.WriteLine($"F1 Score: {metrics.F1Score:F2}"); | ||
Console.WriteLine($"Negative Precision: {metrics.NegativePrecision:F2}"); | ||
Console.WriteLine($"Negative Precision: {metrics.NegativePrecision:F2}") | ||
; | ||
|
||
Console.WriteLine($"Negative Recall: {metrics.NegativeRecall:F2}"); | ||
Console.WriteLine($"Positive Precision: {metrics.PositivePrecision:F2}"); | ||
Console.WriteLine($"Positive Precision: {metrics.PositivePrecision:F2}") | ||
; | ||
|
||
Console.WriteLine($"Positive Recall: {metrics.PositiveRecall:F2}\n"); | ||
Console.WriteLine(metrics.ConfusionMatrix.GetFormattedConfusionTable()); | ||
} | ||
} | ||
} | ||
|
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.
extra line