Skip to content
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

Merged
merged 60 commits into from
Jul 3, 2019
Merged
Show file tree
Hide file tree
Changes from 58 commits
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
cdf6b36
reformatted BinaryClassification samples
sierralee51 Jul 2, 2019
7abb9d1
Update AveragedPerceptron.cs
sierralee51 Jul 2, 2019
5c3214b
Update AveragedPerceptronWithOptions.cs
sierralee51 Jul 2, 2019
74935ee
Update AveragedPerceptron.cs
sierralee51 Jul 2, 2019
e9891f9
Update AveragedPerceptron.cs
sierralee51 Jul 2, 2019
c9dcd03
Update BinaryClassification.ttinclude
sierralee51 Jul 2, 2019
384f983
Update FactorizationMachine.cs
sierralee51 Jul 2, 2019
885f820
Update FastForest.cs
sierralee51 Jul 2, 2019
1d1679b
Update FastForestWithOptions.cs
sierralee51 Jul 2, 2019
f3ec594
Update FastTree.cs
sierralee51 Jul 2, 2019
fb62ed5
Update FastTreeWithOptions.cs
sierralee51 Jul 2, 2019
c243e2e
Update FieldAwareFactorizationMachine.cs
sierralee51 Jul 2, 2019
5458e79
Update FieldAwareFactorizationMachine.cs
sierralee51 Jul 2, 2019
60130f3
Update FieldAwareFactorizationMachine.tt
sierralee51 Jul 2, 2019
edcb4f5
Update FieldAwareFactorizationMachineWithOptions.cs
sierralee51 Jul 2, 2019
3d8613e
Update FieldAwareFactorizationMachine.cs
sierralee51 Jul 2, 2019
5aabe0e
Update FieldAwareFactorizationMachineWithOptions.tt
sierralee51 Jul 2, 2019
85036e2
Update LbfgsLogisticRegression.cs
sierralee51 Jul 2, 2019
92c6c19
Update LbfgsLogisticRegressionWithOptions.cs
sierralee51 Jul 2, 2019
f0990a1
Update LightGbm.cs
sierralee51 Jul 2, 2019
672b785
Update LightGbmWithOptions.cs
sierralee51 Jul 2, 2019
c41905d
Update LinearSvm.cs
sierralee51 Jul 2, 2019
e93c699
Update LinearSvmWithOptions.cs
sierralee51 Jul 2, 2019
cbbee59
Update MultipleFeatureColumnsBinaryClassification.ttinclude
sierralee51 Jul 2, 2019
2fc5e20
Update PriorTrainer.cs
sierralee51 Jul 2, 2019
a43123c
Update AveragedPerceptron.cs
sierralee51 Jul 2, 2019
58484e2
Update AveragedPerceptronWithOptions.cs
sierralee51 Jul 2, 2019
a42c2e8
Update BinaryClassification.ttinclude
sierralee51 Jul 2, 2019
a9afb72
Update FactorizationMachine.cs
sierralee51 Jul 2, 2019
e780b0d
Update FastForestWithOptions.cs
sierralee51 Jul 2, 2019
617a5c5
Update FastTree.cs
sierralee51 Jul 2, 2019
f95144b
Update FastTreeWithOptions.cs
sierralee51 Jul 2, 2019
ff86eca
Update LbfgsLogisticRegression.cs
sierralee51 Jul 2, 2019
5b7734e
Update LbfgsLogisticRegressionWithOptions.cs
sierralee51 Jul 2, 2019
b0c0813
Update LightGbm.cs
sierralee51 Jul 2, 2019
92d564d
Update LightGbmWithOptions.cs
sierralee51 Jul 2, 2019
061a83a
Update LinearSvm.cs
sierralee51 Jul 2, 2019
0402c90
Update LinearSvmWithOptions.cs
sierralee51 Jul 2, 2019
5c28cf6
Update SdcaLogisticRegression.cs
sierralee51 Jul 2, 2019
d39a4a2
Update SdcaLogisticRegressionWithOptions.cs
sierralee51 Jul 2, 2019
fef50fe
Update SdcaNonCalibrated.cs
sierralee51 Jul 2, 2019
deac2b1
Update SdcaNonCalibratedWithOptions.cs
sierralee51 Jul 2, 2019
a808724
Update SdcaNonCalibrated.cs
sierralee51 Jul 2, 2019
31f95e3
Update SdcaLogisticRegressionWithOptions.cs
sierralee51 Jul 2, 2019
1b05112
Update SdcaLogisticRegression.cs
sierralee51 Jul 2, 2019
47481ed
Update SgdCalibrated.cs
sierralee51 Jul 2, 2019
371e043
Update SgdCalibratedWithOptions.cs
sierralee51 Jul 2, 2019
254fdef
Update SgdNonCalibrated.cs
sierralee51 Jul 2, 2019
b860f30
Update SgdNonCalibratedWithOptions.cs
sierralee51 Jul 2, 2019
664aa71
Update SymbolicSgdLogisticRegression.cs
sierralee51 Jul 2, 2019
f6871c8
Update SymbolicSgdLogisticRegressionWithOptions.cs
sierralee51 Jul 2, 2019
539fe74
Update Program.cs
sierralee51 Jul 2, 2019
ed51348
Update Program.cs
sierralee51 Jul 2, 2019
d112945
Update Program.cs
sierralee51 Jul 2, 2019
3af17a8
Update Program.cs
sierralee51 Jul 2, 2019
93a98fe
Update Program.cs
sierralee51 Jul 2, 2019
652f39e
Update Program.cs
sierralee51 Jul 2, 2019
0857a13
fixed tab issues
sierralee51 Jul 2, 2019
6eac51a
fixed indentations
sierralee51 Jul 2, 2019
109a4cd
fixed commented-on parts
sierralee51 Jul 3, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extra line


// 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
Expand All @@ -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:
Expand All @@ -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();
Expand All @@ -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; }
Expand All @@ -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}")
;
Copy link

@rayankrish rayankrish Jul 2, 2019

Choose a reason for hiding this comment

The 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 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());
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extra line

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
Expand All @@ -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:
Expand All @@ -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();
Expand All @@ -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; }
Expand All @@ -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());
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -13,63 +13,81 @@ namespace Samples.Dynamic.Trainers.BinaryClassification
{<#=Comments#>
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);
<# if (CacheData) { #>

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

<# if (TrainerOptions == null) { #>
// Define the trainer.
var pipeline = mlContext.BinaryClassification.Trainers.<#=Trainer#>();
var pipeline = mlContext.BinaryClassification.Trainers
.<#=Trainer#>();

<# } else { #>
// Define trainer options.
var options = new <#=TrainerOptions#>;

// Define the trainer.
var pipeline = mlContext.BinaryClassification.Trainers.<#=Trainer#>(options);
var pipeline = mlContext.BinaryClassification.Trainers
.<#=Trainer#>(options);

<# } #>

// 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}");

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

// Evaluate the overall metrics.
var metrics = mlContext.BinaryClassification.<#=Evaluator#>(transformedTestData);
var metrics = mlContext.BinaryClassification
.<#=Evaluator#>(transformedTestData);

PrintMetrics(metrics);

<#=ExpectedOutput#>
}

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();
Expand All @@ -80,13 +98,18 @@ namespace Samples.Dynamic.Trainers.BinaryClassification
{
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() + <#=DataSepValue#>).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() +
<#=DataSepValue#>).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; }
Expand All @@ -109,11 +132,15 @@ namespace Samples.Dynamic.Trainers.BinaryClassification
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());
}
}
}
}
Loading