Skip to content

Commit b35f349

Browse files
committed
review comments
1 parent 8329ca0 commit b35f349

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+561
-385
lines changed

docs/samples/Microsoft.ML.Samples/Dynamic/Trainers/BinaryClassification/AveragedPerceptron.cs

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public static void Example()
3636
// Convert IDataView object to a list.
3737
var predictions = mlContext.Data.CreateEnumerable<Prediction>(transformedTestData, reuseRowObject: false).ToList();
3838

39-
// Look at 5 predictions
39+
// Print 5 predictions.
4040
foreach (var p in predictions.Take(5))
4141
Console.WriteLine($"Label: {p.Label}, Prediction: {p.PredictedLabel}");
4242

@@ -45,26 +45,20 @@ public static void Example()
4545
// Label: False, Prediction: False
4646
// Label: True, Prediction: True
4747
// Label: True, Prediction: False
48-
// Label: False, Prediction: True
48+
// Label: False, Prediction: False
4949

50-
// Evaluate the overall metrics
51-
var metrics = mlContext.BinaryClassification.Evaluate(transformedTestData);
52-
Console.WriteLine($"Accuracy: {metrics.Accuracy:F2}");
53-
Console.WriteLine($"AUC: {metrics.AreaUnderRocCurve:F2}");
54-
Console.WriteLine($"F1 Score: {metrics.F1Score:F2}");
55-
Console.WriteLine($"Negative Precision: {metrics.NegativePrecision:F2}");
56-
Console.WriteLine($"Negative Recall: {metrics.NegativeRecall:F2}");
57-
Console.WriteLine($"Positive Precision: {metrics.PositivePrecision:F2}");
58-
Console.WriteLine($"Positive Recall: {metrics.PositiveRecall:F2}");
50+
// Evaluate the overall metrics.
51+
var metrics = mlContext.BinaryClassification.EvaluateNonCalibrated(transformedTestData);
52+
PrintMetrics(metrics);
5953

6054
// Expected output:
61-
// Accuracy: 0.55
62-
// AUC: 0.58
63-
// F1 Score: 0.53
64-
// Negative Precision: 0.57
65-
// Negative Recall: 0.56
66-
// Positive Precision: 0.53
67-
// Positive Recall: 0.54
55+
// Accuracy: 0.72
56+
// AUC: 0.79
57+
// F1 Score: 0.68
58+
// Negative Precision: 0.71
59+
// Negative Recall: 0.80
60+
// Positive Precision: 0.74
61+
// Positive Recall: 0.63
6862
}
6963

7064
private static IEnumerable<DataPoint> GenerateRandomDataPoints(int count, int seed=0)
@@ -79,7 +73,7 @@ private static IEnumerable<DataPoint> GenerateRandomDataPoints(int count, int se
7973
Label = label,
8074
// Create random features that are correlated with the label.
8175
// For data points with false label, the feature values are slightly increased by adding a constant.
82-
Features = Enumerable.Repeat(label, 50).Select(x => x ? randomFloat() : randomFloat() + 0.03f).ToArray()
76+
Features = Enumerable.Repeat(label, 50).Select(x => x ? randomFloat() : randomFloat() + 0.1f).ToArray()
8377
};
8478
}
8579
}
@@ -100,5 +94,17 @@ private class Prediction
10094
// Predicted label from the trainer.
10195
public bool PredictedLabel { get; set; }
10296
}
97+
98+
// Pretty-print BinaryClassificationMetrics objects.
99+
private static void PrintMetrics(BinaryClassificationMetrics metrics)
100+
{
101+
Console.WriteLine($"Accuracy: {metrics.Accuracy:F2}");
102+
Console.WriteLine($"AUC: {metrics.AreaUnderRocCurve:F2}");
103+
Console.WriteLine($"F1 Score: {metrics.F1Score:F2}");
104+
Console.WriteLine($"Negative Precision: {metrics.NegativePrecision:F2}");
105+
Console.WriteLine($"Negative Recall: {metrics.NegativeRecall:F2}");
106+
Console.WriteLine($"Positive Precision: {metrics.PositivePrecision:F2}");
107+
Console.WriteLine($"Positive Recall: {metrics.PositiveRecall:F2}");
108+
}
103109
}
104110
}

docs/samples/Microsoft.ML.Samples/Dynamic/Trainers/BinaryClassification/AveragedPerceptron.tt

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
string ClassName = "AveragedPerceptron";
44
string Trainer = "AveragedPerceptron";
55
string TrainerOptions = null;
6-
bool IsCalibrated = true;
6+
bool IsCalibrated = false;
77
bool CacheData = false;
88

9-
string DataSepValue = "0.03f";
9+
string LabelThreshold = "0.5f";
10+
string DataSepValue = "0.1f";
1011
string OptionsInclude = "";
1112
string Comments= "";
1213

@@ -15,14 +16,14 @@ string ExpectedOutputPerInstance = @"// Expected output:
1516
// Label: False, Prediction: False
1617
// Label: True, Prediction: True
1718
// Label: True, Prediction: False
18-
// Label: False, Prediction: True";
19+
// Label: False, Prediction: False";
1920

2021
string ExpectedOutput = @"// Expected output:
21-
// Accuracy: 0.55
22-
// AUC: 0.58
23-
// F1 Score: 0.53
24-
// Negative Precision: 0.57
25-
// Negative Recall: 0.56
26-
// Positive Precision: 0.53
27-
// Positive Recall: 0.54";
22+
// Accuracy: 0.72
23+
// AUC: 0.79
24+
// F1 Score: 0.68
25+
// Negative Precision: 0.71
26+
// Negative Recall: 0.80
27+
// Positive Precision: 0.74
28+
// Positive Recall: 0.63";
2829
#>

docs/samples/Microsoft.ML.Samples/Dynamic/Trainers/BinaryClassification/AveragedPerceptronWithOptions.cs

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public static void Example()
4747
// Convert IDataView object to a list.
4848
var predictions = mlContext.Data.CreateEnumerable<Prediction>(transformedTestData, reuseRowObject: false).ToList();
4949

50-
// Look at 5 predictions
50+
// Print 5 predictions.
5151
foreach (var p in predictions.Take(5))
5252
Console.WriteLine($"Label: {p.Label}, Prediction: {p.PredictedLabel}");
5353

@@ -58,24 +58,18 @@ public static void Example()
5858
// Label: True, Prediction: True
5959
// Label: False, Prediction: False
6060

61-
// Evaluate the overall metrics
62-
var metrics = mlContext.BinaryClassification.Evaluate(transformedTestData);
63-
Console.WriteLine($"Accuracy: {metrics.Accuracy:F2}");
64-
Console.WriteLine($"AUC: {metrics.AreaUnderRocCurve:F2}");
65-
Console.WriteLine($"F1 Score: {metrics.F1Score:F2}");
66-
Console.WriteLine($"Negative Precision: {metrics.NegativePrecision:F2}");
67-
Console.WriteLine($"Negative Recall: {metrics.NegativeRecall:F2}");
68-
Console.WriteLine($"Positive Precision: {metrics.PositivePrecision:F2}");
69-
Console.WriteLine($"Positive Recall: {metrics.PositiveRecall:F2}");
61+
// Evaluate the overall metrics.
62+
var metrics = mlContext.BinaryClassification.EvaluateNonCalibrated(transformedTestData);
63+
PrintMetrics(metrics);
7064

7165
// Expected output:
72-
// Accuracy: 0.62
73-
// AUC: 0.66
74-
// F1 Score: 0.54
75-
// Negative Precision: 0.61
76-
// Negative Recall: 0.76
77-
// Positive Precision: 0.64
78-
// Positive Recall: 0.47
66+
// Accuracy: 0.89
67+
// AUC: 0.96
68+
// F1 Score: 0.88
69+
// Negative Precision: 0.87
70+
// Negative Recall: 0.92
71+
// Positive Precision: 0.91
72+
// Positive Recall: 0.85
7973
}
8074

8175
private static IEnumerable<DataPoint> GenerateRandomDataPoints(int count, int seed=0)
@@ -90,7 +84,7 @@ private static IEnumerable<DataPoint> GenerateRandomDataPoints(int count, int se
9084
Label = label,
9185
// Create random features that are correlated with the label.
9286
// For data points with false label, the feature values are slightly increased by adding a constant.
93-
Features = Enumerable.Repeat(label, 50).Select(x => x ? randomFloat() : randomFloat() + 0.03f).ToArray()
87+
Features = Enumerable.Repeat(label, 50).Select(x => x ? randomFloat() : randomFloat() + 0.1f).ToArray()
9488
};
9589
}
9690
}
@@ -111,5 +105,17 @@ private class Prediction
111105
// Predicted label from the trainer.
112106
public bool PredictedLabel { get; set; }
113107
}
108+
109+
// Pretty-print BinaryClassificationMetrics objects.
110+
private static void PrintMetrics(BinaryClassificationMetrics metrics)
111+
{
112+
Console.WriteLine($"Accuracy: {metrics.Accuracy:F2}");
113+
Console.WriteLine($"AUC: {metrics.AreaUnderRocCurve:F2}");
114+
Console.WriteLine($"F1 Score: {metrics.F1Score:F2}");
115+
Console.WriteLine($"Negative Precision: {metrics.NegativePrecision:F2}");
116+
Console.WriteLine($"Negative Recall: {metrics.NegativeRecall:F2}");
117+
Console.WriteLine($"Positive Precision: {metrics.PositivePrecision:F2}");
118+
Console.WriteLine($"Positive Recall: {metrics.PositiveRecall:F2}");
119+
}
114120
}
115121
}

docs/samples/Microsoft.ML.Samples/Dynamic/Trainers/BinaryClassification/AveragedPerceptronWithOptions.tt

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
<#+
33
string ClassName="AveragedPerceptronWithOptions";
44
string Trainer = "AveragedPerceptron";
5-
bool IsCalibrated = true;
5+
bool IsCalibrated = false;
66

7-
string DataSepValue = "0.03f";
7+
string LabelThreshold = "0.5f";
8+
string DataSepValue = "0.1f";
89
string OptionsInclude = "using Microsoft.ML.Trainers;";
910
string Comments= "";
1011
bool CacheData = false;
@@ -26,11 +27,11 @@ string ExpectedOutputPerInstance= @"// Expected output:
2627
// Label: False, Prediction: False";
2728

2829
string ExpectedOutput = @"// Expected output:
29-
// Accuracy: 0.62
30-
// AUC: 0.66
31-
// F1 Score: 0.54
32-
// Negative Precision: 0.61
33-
// Negative Recall: 0.76
34-
// Positive Precision: 0.64
35-
// Positive Recall: 0.47";
30+
// Accuracy: 0.89
31+
// AUC: 0.96
32+
// F1 Score: 0.88
33+
// Negative Precision: 0.87
34+
// Negative Recall: 0.92
35+
// Positive Precision: 0.91
36+
// Positive Recall: 0.85";
3637
#>

docs/samples/Microsoft.ML.Samples/Dynamic/Trainers/BinaryClassification/BinaryClassification.ttinclude

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ namespace Samples.Dynamic.Trainers.BinaryClassification
2626
<# if (CacheData) { #>
2727

2828
// ML.NET doesn't cache data set by default. Therefore, if one reads a data set from a file and accesses it many times,
29-
// it can be slow due to expensive featurization and disk operations. When the considered data can fit into memory,
30-
// a solution is to cache the data in memory. Caching is especially helpful when working with iterative algorithms
31-
// which needs many data passes.
32-
trainingData = mlContext.Data.Cache(trainingData);
29+
// it can be slow due to expensive featurization and disk operations. When the considered data can fit into memory,
30+
// a solution is to cache the data in memory. Caching is especially helpful when working with iterative algorithms
31+
// which needs many data passes.
32+
trainingData = mlContext.Data.Cache(trainingData);
3333
<# } #>
3434

3535
<# if (TrainerOptions == null) { #>
@@ -55,22 +55,16 @@ namespace Samples.Dynamic.Trainers.BinaryClassification
5555
// Convert IDataView object to a list.
5656
var predictions = mlContext.Data.CreateEnumerable<Prediction>(transformedTestData, reuseRowObject: false).ToList();
5757

58-
// Look at 5 predictions
58+
// Print 5 predictions.
5959
foreach (var p in predictions.Take(5))
6060
Console.WriteLine($"Label: {p.Label}, Prediction: {p.PredictedLabel}");
6161

6262
<#=ExpectedOutputPerInstance#>
6363
<# string Evaluator = IsCalibrated ? "Evaluate" : "EvaluateNonCalibrated"; #>
6464

65-
// Evaluate the overall metrics
65+
// Evaluate the overall metrics.
6666
var metrics = mlContext.BinaryClassification.<#=Evaluator#>(transformedTestData);
67-
Console.WriteLine($"Accuracy: {metrics.Accuracy:F2}");
68-
Console.WriteLine($"AUC: {metrics.AreaUnderRocCurve:F2}");
69-
Console.WriteLine($"F1 Score: {metrics.F1Score:F2}");
70-
Console.WriteLine($"Negative Precision: {metrics.NegativePrecision:F2}");
71-
Console.WriteLine($"Negative Recall: {metrics.NegativeRecall:F2}");
72-
Console.WriteLine($"Positive Precision: {metrics.PositivePrecision:F2}");
73-
Console.WriteLine($"Positive Recall: {metrics.PositiveRecall:F2}");
67+
PrintMetrics(metrics);
7468

7569
<#=ExpectedOutput#>
7670
}
@@ -81,7 +75,7 @@ namespace Samples.Dynamic.Trainers.BinaryClassification
8175
float randomFloat() => (float)random.NextDouble();
8276
for (int i = 0; i < count; i++)
8377
{
84-
var label = randomFloat() > 0.5f;
78+
var label = randomFloat() > <#=LabelThreshold#>;
8579
yield return new DataPoint
8680
{
8781
Label = label,
@@ -108,5 +102,17 @@ namespace Samples.Dynamic.Trainers.BinaryClassification
108102
// Predicted label from the trainer.
109103
public bool PredictedLabel { get; set; }
110104
}
105+
106+
// Pretty-print BinaryClassificationMetrics objects.
107+
private static void PrintMetrics(BinaryClassificationMetrics metrics)
108+
{
109+
Console.WriteLine($"Accuracy: {metrics.Accuracy:F2}");
110+
Console.WriteLine($"AUC: {metrics.AreaUnderRocCurve:F2}");
111+
Console.WriteLine($"F1 Score: {metrics.F1Score:F2}");
112+
Console.WriteLine($"Negative Precision: {metrics.NegativePrecision:F2}");
113+
Console.WriteLine($"Negative Recall: {metrics.NegativeRecall:F2}");
114+
Console.WriteLine($"Positive Precision: {metrics.PositivePrecision:F2}");
115+
Console.WriteLine($"Positive Recall: {metrics.PositiveRecall:F2}");
116+
}
111117
}
112118
}

docs/samples/Microsoft.ML.Samples/Dynamic/Trainers/BinaryClassification/FastForest.cs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public static void Example()
3838
// Convert IDataView object to a list.
3939
var predictions = mlContext.Data.CreateEnumerable<Prediction>(transformedTestData, reuseRowObject: false).ToList();
4040

41-
// Look at 5 predictions
41+
// Print 5 predictions.
4242
foreach (var p in predictions.Take(5))
4343
Console.WriteLine($"Label: {p.Label}, Prediction: {p.PredictedLabel}");
4444

@@ -49,15 +49,9 @@ public static void Example()
4949
// Label: True, Prediction: True
5050
// Label: False, Prediction: False
5151

52-
// Evaluate the overall metrics
52+
// Evaluate the overall metrics.
5353
var metrics = mlContext.BinaryClassification.EvaluateNonCalibrated(transformedTestData);
54-
Console.WriteLine($"Accuracy: {metrics.Accuracy:F2}");
55-
Console.WriteLine($"AUC: {metrics.AreaUnderRocCurve:F2}");
56-
Console.WriteLine($"F1 Score: {metrics.F1Score:F2}");
57-
Console.WriteLine($"Negative Precision: {metrics.NegativePrecision:F2}");
58-
Console.WriteLine($"Negative Recall: {metrics.NegativeRecall:F2}");
59-
Console.WriteLine($"Positive Precision: {metrics.PositivePrecision:F2}");
60-
Console.WriteLine($"Positive Recall: {metrics.PositiveRecall:F2}");
54+
PrintMetrics(metrics);
6155

6256
// Expected output:
6357
// Accuracy: 0.74
@@ -102,6 +96,18 @@ private class Prediction
10296
// Predicted label from the trainer.
10397
public bool PredictedLabel { get; set; }
10498
}
99+
100+
// Pretty-print BinaryClassificationMetrics objects.
101+
private static void PrintMetrics(BinaryClassificationMetrics metrics)
102+
{
103+
Console.WriteLine($"Accuracy: {metrics.Accuracy:F2}");
104+
Console.WriteLine($"AUC: {metrics.AreaUnderRocCurve:F2}");
105+
Console.WriteLine($"F1 Score: {metrics.F1Score:F2}");
106+
Console.WriteLine($"Negative Precision: {metrics.NegativePrecision:F2}");
107+
Console.WriteLine($"Negative Recall: {metrics.NegativeRecall:F2}");
108+
Console.WriteLine($"Positive Precision: {metrics.PositivePrecision:F2}");
109+
Console.WriteLine($"Positive Recall: {metrics.PositiveRecall:F2}");
110+
}
105111
}
106112
}
107113

docs/samples/Microsoft.ML.Samples/Dynamic/Trainers/BinaryClassification/FastForestWithOptions.cs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public static void Example()
5050
// Convert IDataView object to a list.
5151
var predictions = mlContext.Data.CreateEnumerable<Prediction>(transformedTestData, reuseRowObject: false).ToList();
5252

53-
// Look at 5 predictions
53+
// Print 5 predictions.
5454
foreach (var p in predictions.Take(5))
5555
Console.WriteLine($"Label: {p.Label}, Prediction: {p.PredictedLabel}");
5656

@@ -61,15 +61,9 @@ public static void Example()
6161
// Label: True, Prediction: True
6262
// Label: False, Prediction: True
6363

64-
// Evaluate the overall metrics
64+
// Evaluate the overall metrics.
6565
var metrics = mlContext.BinaryClassification.EvaluateNonCalibrated(transformedTestData);
66-
Console.WriteLine($"Accuracy: {metrics.Accuracy:F2}");
67-
Console.WriteLine($"AUC: {metrics.AreaUnderRocCurve:F2}");
68-
Console.WriteLine($"F1 Score: {metrics.F1Score:F2}");
69-
Console.WriteLine($"Negative Precision: {metrics.NegativePrecision:F2}");
70-
Console.WriteLine($"Negative Recall: {metrics.NegativeRecall:F2}");
71-
Console.WriteLine($"Positive Precision: {metrics.PositivePrecision:F2}");
72-
Console.WriteLine($"Positive Recall: {metrics.PositiveRecall:F2}");
66+
PrintMetrics(metrics);
7367

7468
// Expected output:
7569
// Accuracy: 0.73
@@ -114,6 +108,18 @@ private class Prediction
114108
// Predicted label from the trainer.
115109
public bool PredictedLabel { get; set; }
116110
}
111+
112+
// Pretty-print BinaryClassificationMetrics objects.
113+
private static void PrintMetrics(BinaryClassificationMetrics metrics)
114+
{
115+
Console.WriteLine($"Accuracy: {metrics.Accuracy:F2}");
116+
Console.WriteLine($"AUC: {metrics.AreaUnderRocCurve:F2}");
117+
Console.WriteLine($"F1 Score: {metrics.F1Score:F2}");
118+
Console.WriteLine($"Negative Precision: {metrics.NegativePrecision:F2}");
119+
Console.WriteLine($"Negative Recall: {metrics.NegativeRecall:F2}");
120+
Console.WriteLine($"Positive Precision: {metrics.PositivePrecision:F2}");
121+
Console.WriteLine($"Positive Recall: {metrics.PositiveRecall:F2}");
122+
}
117123
}
118124
}
119125

0 commit comments

Comments
 (0)