Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 2k
Reformatting BinaryClassification samples to width 85#3946
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
cdf6b367abb9d15c3214b74935eee9891f9c9dcd03384f983885f8201d1679bf3ec594fb62ed5c243e2e5458e7960130f3edcb4f53d8613e5aabe0e85036e292c6c19f0990a1672b785c41905de93c699cbbee592fc5e20a43123c58484e2a42c2e8a9afb72e780b0d617a5c5f95144bff86eca5b7734eb0c081392d564d061a83a0402c905c28cf6d39a4a2fef50fedeac2b1a80872431f95e31b0511247481ed371e043254fdefb860f30664aa71f6871c8539fe74ed51348d1129453af17a893a98fe652f39e0857a136eac51a109a4cdFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| 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,29 @@ 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 ContributorAuthor 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 +67,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 +92,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 +105,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 +139,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()); | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
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