Skip to content

Commit 9b9fd6a

Browse files
authored
BinaryClassification Calibrators: Replace Preview API with IDataView based getter API. (#3353)
* Replace Preview API with IDataView based getter API. * clean up. * PR feedback. * PR feedback.
1 parent 3b576fe commit 9b9fd6a

File tree

4 files changed

+105
-68
lines changed

4 files changed

+105
-68
lines changed

docs/samples/Microsoft.ML.Samples/Dynamic/Trainers/BinaryClassification/Calibrators/FixedPlatt.cs

+25-16
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Linq;
34
using Microsoft.ML;
45

@@ -27,9 +28,8 @@ public static void Example()
2728
// Let's score the new data. The score will give us a numerical estimation of the chance that the particular sample
2829
// bears positive sentiment. This estimate is relative to the numbers obtained.
2930
var scoredData = transformer.Transform(trainTestData.TestSet);
30-
var scoredDataPreview = scoredData.Preview();
31-
32-
PrintRowViewValues(scoredDataPreview);
31+
var outScores = mlContext.Data.CreateEnumerable<ScoreValue>(scoredData, reuseRowObject: false);
32+
PrintScore(outScores, 5);
3333
// Preview of scoredDataPreview.RowView
3434
// Score 4.18144
3535
// Score -14.10248
@@ -45,28 +45,37 @@ public static void Example()
4545
// Transform the scored data with a calibrator transfomer by adding a new column names "Probability".
4646
// This column is a calibrated version of the "Score" column, meaning its values are a valid probability value in the [0, 1] interval
4747
// representing the chance that the respective sample bears positive sentiment.
48-
var finalData = calibratorTransformer.Transform(scoredData).Preview();
49-
PrintRowViewValues(finalData);
48+
var finalData = calibratorTransformer.Transform(scoredData);
49+
var outScoresAndProbabilities = mlContext.Data.CreateEnumerable<ScoreAndProbabilityValue>(finalData, reuseRowObject: false);
50+
PrintScoreAndProbability(outScoresAndProbabilities, 5);
5051
// Score 4.18144 Probability 0.9856767
5152
// Score -14.10248 Probability 7.890148E-07
5253
// Score 2.731951 Probability 0.9416927
5354
// Score -2.554229 Probability 0.07556222
5455
// Score 5.36571 Probability 0.9955735
5556
}
5657

57-
private static void PrintRowViewValues(Microsoft.ML.Data.DataDebuggerPreview data)
58+
private static void PrintScore(IEnumerable<ScoreValue> values, int numRows)
5859
{
59-
var firstRows = data.RowView.Take(5);
60+
foreach (var value in values.Take(numRows))
61+
Console.WriteLine("{0, -10} {1, -10}", "Score", value.Score);
62+
}
63+
64+
private static void PrintScoreAndProbability(IEnumerable<ScoreAndProbabilityValue> values, int numRows)
65+
{
66+
foreach (var value in values.Take(numRows))
67+
Console.WriteLine("{0, -10} {1, -10} {2, -10} {3, -10}", "Score", value.Score, "Probability", value.Probability);
68+
}
6069

61-
foreach (Microsoft.ML.Data.DataDebuggerPreview.RowInfo row in firstRows)
62-
{
63-
foreach (var kvPair in row.Values)
64-
{
65-
if (kvPair.Key.Equals("Score") || kvPair.Key.Equals("Probability"))
66-
Console.Write($" {kvPair.Key} {kvPair.Value} ");
67-
}
68-
Console.WriteLine();
69-
}
70+
private class ScoreValue
71+
{
72+
public float Score { get; set; }
73+
}
74+
75+
private class ScoreAndProbabilityValue
76+
{
77+
public float Score { get; set; }
78+
public float Probability { get; set; }
7079
}
7180
}
7281
}

docs/samples/Microsoft.ML.Samples/Dynamic/Trainers/BinaryClassification/Calibrators/Isotonic.cs

+25-15
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Linq;
34
using Microsoft.ML;
45

@@ -27,9 +28,9 @@ public static void Example()
2728
// Let's score the new data. The score will give us a numerical estimation of the chance that the particular sample
2829
// bears positive sentiment. This estimate is relative to the numbers obtained.
2930
var scoredData = transformer.Transform(trainTestData.TestSet);
30-
var scoredDataPreview = scoredData.Preview();
31+
var outScores = mlContext.Data.CreateEnumerable<ScoreValue>(scoredData, reuseRowObject: false);
3132

32-
PrintRowViewValues(scoredDataPreview);
33+
PrintScore(outScores, 5);
3334
// Preview of scoredDataPreview.RowView
3435
// Score 4.18144
3536
// Score -14.10248
@@ -45,28 +46,37 @@ public static void Example()
4546
// Transform the scored data with a calibrator transfomer by adding a new column names "Probability".
4647
// This column is a calibrated version of the "Score" column, meaning its values are a valid probability value in the [0, 1] interval
4748
// representing the chance that the respective sample bears positive sentiment.
48-
var finalData = calibratorTransformer.Transform(scoredData).Preview();
49-
PrintRowViewValues(finalData);
49+
var finalData = calibratorTransformer.Transform(scoredData);
50+
var outScoresAndProbabilities = mlContext.Data.CreateEnumerable<ScoreAndProbabilityValue>(finalData, reuseRowObject: false);
51+
PrintScoreAndProbability(outScoresAndProbabilities, 5);
5052
// Score 4.18144 Probability 0.8
5153
// Score -14.10248 Probability 1E-15
5254
// Score 2.731951 Probability 0.7370371
5355
// Score -2.554229 Probability 0.2063954
5456
// Score 5.36571 Probability 0.8958333
5557
}
5658

57-
private static void PrintRowViewValues(Microsoft.ML.Data.DataDebuggerPreview data)
59+
private static void PrintScore(IEnumerable<ScoreValue> values, int numRows)
5860
{
59-
var firstRows = data.RowView.Take(5);
61+
foreach (var value in values.Take(numRows))
62+
Console.WriteLine("{0, -10} {1, -10}", "Score", value.Score);
63+
}
64+
65+
private static void PrintScoreAndProbability(IEnumerable<ScoreAndProbabilityValue> values, int numRows)
66+
{
67+
foreach (var value in values.Take(numRows))
68+
Console.WriteLine("{0, -10} {1, -10} {2, -10} {3, -10}", "Score", value.Score, "Probability", value.Probability);
69+
}
6070

61-
foreach (Microsoft.ML.Data.DataDebuggerPreview.RowInfo row in firstRows)
62-
{
63-
foreach (var kvPair in row.Values)
64-
{
65-
if (kvPair.Key.Equals("Score") || kvPair.Key.Equals("Probability"))
66-
Console.Write($" {kvPair.Key} {kvPair.Value} ");
67-
}
68-
Console.WriteLine();
69-
}
71+
private class ScoreValue
72+
{
73+
public float Score { get; set; }
74+
}
75+
76+
private class ScoreAndProbabilityValue
77+
{
78+
public float Score { get; set; }
79+
public float Probability { get; set; }
7080
}
7181
}
7282
}

docs/samples/Microsoft.ML.Samples/Dynamic/Trainers/BinaryClassification/Calibrators/Naive.cs

+30-21
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Linq;
34
using Microsoft.ML;
45

@@ -27,9 +28,8 @@ public static void Example()
2728
// Let's score the new data. The score will give us a numerical estimation of the chance that the particular sample
2829
// bears positive sentiment. This estimate is relative to the numbers obtained.
2930
var scoredData = transformer.Transform(trainTestData.TestSet);
30-
var scoredDataPreview = scoredData.Preview();
31-
32-
PrintRowViewValues(scoredDataPreview);
31+
var outScores = mlContext.Data.CreateEnumerable<ScoreValue>(scoredData, reuseRowObject: false);
32+
PrintScore(outScores, 5);
3333
// Preview of scoredDataPreview.RowView
3434
// Score 4.18144
3535
// Score -14.10248
@@ -45,28 +45,37 @@ public static void Example()
4545
// Transform the scored data with a calibrator transfomer by adding a new column names "Probability".
4646
// This column is a calibrated version of the "Score" column, meaning its values are a valid probability value in the [0, 1] interval
4747
// representing the chance that the respective sample bears positive sentiment.
48-
var finalData = calibratorTransformer.Transform(scoredData).Preview();
49-
PrintRowViewValues(finalData);
50-
// Score 4.18144 Probability 0.775
51-
// Score -14.10248 Probability 0.01923077
52-
// Score 2.731951 Probability 0.7738096
53-
// Score -2.554229 Probability 0.2011494
54-
// Score 5.36571 Probability 0.9117647
48+
var finalData = calibratorTransformer.Transform(scoredData);
49+
var outScoresAndProbabilities = mlContext.Data.CreateEnumerable<ScoreAndProbabilityValue>(finalData, reuseRowObject: false);
50+
PrintScoreAndProbability(outScoresAndProbabilities, 5);
51+
// Score 4.18144 Probability 0.775
52+
// Score -14.10248 Probability 0.01923077
53+
// Score 2.731951 Probability 0.7738096
54+
// Score -2.554229 Probability 0.2011494
55+
// Score 5.36571 Probability 0.9117647
5556
}
5657

57-
private static void PrintRowViewValues(Microsoft.ML.Data.DataDebuggerPreview data)
58+
private static void PrintScore(IEnumerable<ScoreValue> values, int numRows)
5859
{
59-
var firstRows = data.RowView.Take(5);
60+
foreach (var value in values.Take(numRows))
61+
Console.WriteLine("{0, -10} {1, -10}", "Score", value.Score);
62+
}
63+
64+
private static void PrintScoreAndProbability(IEnumerable<ScoreAndProbabilityValue> values, int numRows)
65+
{
66+
foreach (var value in values.Take(numRows))
67+
Console.WriteLine("{0, -10} {1, -10} {2, -10} {3, -10}", "Score", value.Score, "Probability", value.Probability);
68+
}
6069

61-
foreach (Microsoft.ML.Data.DataDebuggerPreview.RowInfo row in firstRows)
62-
{
63-
foreach (var kvPair in row.Values)
64-
{
65-
if (kvPair.Key.Equals("Score") || kvPair.Key.Equals("Probability"))
66-
Console.Write($" {kvPair.Key} {kvPair.Value} ");
67-
}
68-
Console.WriteLine();
69-
}
70+
private class ScoreValue
71+
{
72+
public float Score { get; set; }
73+
}
74+
75+
private class ScoreAndProbabilityValue
76+
{
77+
public float Score { get; set; }
78+
public float Probability { get; set; }
7079
}
7180
}
7281
}

docs/samples/Microsoft.ML.Samples/Dynamic/Trainers/BinaryClassification/Calibrators/Platt.cs

+25-16
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Linq;
34
using Microsoft.ML;
45

@@ -27,9 +28,8 @@ public static void Example()
2728
// Let's score the new data. The score will give us a numerical estimation of the chance that the particular sample
2829
// bears positive sentiment. This estimate is relative to the numbers obtained.
2930
var scoredData = transformer.Transform(trainTestData.TestSet);
30-
var scoredDataPreview = scoredData.Preview();
31-
32-
PrintRowViewValues(scoredDataPreview);
31+
var outScores = mlContext.Data.CreateEnumerable<ScoreValue>(scoredData, reuseRowObject: false);
32+
PrintScore(outScores, 5);
3333
// Preview of scoredDataPreview.RowView
3434
// Score 4.18144
3535
// Score -14.10248
@@ -45,28 +45,37 @@ public static void Example()
4545
// Transform the scored data with a calibrator transfomer by adding a new column names "Probability".
4646
// This column is a calibrated version of the "Score" column, meaning its values are a valid probability value in the [0, 1] interval
4747
// representing the chance that the respective sample bears positive sentiment.
48-
var finalData = calibratorTransformer.Transform(scoredData).Preview();
49-
PrintRowViewValues(finalData);
48+
var finalData = calibratorTransformer.Transform(scoredData);
49+
var outScoresAndProbabilities = mlContext.Data.CreateEnumerable<ScoreAndProbabilityValue>(finalData, reuseRowObject: false);
50+
PrintScoreAndProbability(outScoresAndProbabilities, 5);
5051
// Score 4.18144 Probability 0.8511352
5152
// Score -14.10248 Probability 0.001633563
5253
// Score 2.731951 Probability 0.7496456
5354
// Score -2.554229 Probability 0.2206048
5455
// Score 5.36571 Probability 0.9065308
5556
}
5657

57-
private static void PrintRowViewValues(Microsoft.ML.Data.DataDebuggerPreview data)
58+
private static void PrintScore(IEnumerable<ScoreValue> values, int numRows)
5859
{
59-
var firstRows = data.RowView.Take(5);
60+
foreach (var value in values.Take(numRows))
61+
Console.WriteLine("{0, -10} {1, -10}", "Score", value.Score);
62+
}
63+
64+
private static void PrintScoreAndProbability(IEnumerable<ScoreAndProbabilityValue> values, int numRows)
65+
{
66+
foreach (var value in values.Take(numRows))
67+
Console.WriteLine("{0, -10} {1, -10} {2, -10} {3, -10}", "Score", value.Score, "Probability", value.Probability);
68+
}
6069

61-
foreach (Microsoft.ML.Data.DataDebuggerPreview.RowInfo row in firstRows)
62-
{
63-
foreach (var kvPair in row.Values)
64-
{
65-
if (kvPair.Key.Equals("Score") || kvPair.Key.Equals("Probability"))
66-
Console.Write($" {kvPair.Key} {kvPair.Value} ");
67-
}
68-
Console.WriteLine();
69-
}
70+
private class ScoreValue
71+
{
72+
public float Score { get; set; }
73+
}
74+
75+
private class ScoreAndProbabilityValue
76+
{
77+
public float Score { get; set; }
78+
public float Probability { get; set; }
7079
}
7180
}
7281
}

0 commit comments

Comments
 (0)