Skip to content

Commit

Permalink
Reformatting BinaryClassification samples to width 85 (#3946)
Browse files Browse the repository at this point in the history
* reformatted BinaryClassification samples

* Update AveragedPerceptron.cs

fixing spacing

* Update AveragedPerceptronWithOptions.cs

fixing whitespace

* Update AveragedPerceptron.cs

* Update AveragedPerceptron.cs

* Update BinaryClassification.ttinclude

fixing whitespace

* Update FactorizationMachine.cs

fixing whitespace

* Update FastForest.cs

fixing whitespace

* Update FastForestWithOptions.cs

fixing whitespace

* Update FastTree.cs

fixing whitespace

* Update FastTreeWithOptions.cs

fixing whitespace

* Update FieldAwareFactorizationMachine.cs

fixing whitespace

* Update FieldAwareFactorizationMachine.cs

* Update FieldAwareFactorizationMachine.tt

fixing whitespace

* Update FieldAwareFactorizationMachineWithOptions.cs

fixing whitespace

* Update FieldAwareFactorizationMachine.cs

* Update FieldAwareFactorizationMachineWithOptions.tt

fixing whitespace

* Update LbfgsLogisticRegression.cs

fixing whitespace

* Update LbfgsLogisticRegressionWithOptions.cs

fixing whitespace

* Update LightGbm.cs

fixing whitespace

* Update LightGbmWithOptions.cs

fixing whitespace

* Update LinearSvm.cs

fixing whitespace

* Update LinearSvmWithOptions.cs

fixing whitespace

* Update MultipleFeatureColumnsBinaryClassification.ttinclude

fixing whitespace

* Update PriorTrainer.cs

fixing whitespace

* Update AveragedPerceptron.cs

* Update AveragedPerceptronWithOptions.cs

* Update BinaryClassification.ttinclude

* Update FactorizationMachine.cs

* Update FastForestWithOptions.cs

* Update FastTree.cs

* Update FastTreeWithOptions.cs

* Update LbfgsLogisticRegression.cs

* Update LbfgsLogisticRegressionWithOptions.cs

* Update LightGbm.cs

* Update LightGbmWithOptions.cs

* Update LinearSvm.cs

* Update LinearSvmWithOptions.cs

* Update SdcaLogisticRegression.cs

* Update SdcaLogisticRegressionWithOptions.cs

* Update SdcaNonCalibrated.cs

* Update SdcaNonCalibratedWithOptions.cs

* Update SdcaNonCalibrated.cs

* Update SdcaLogisticRegressionWithOptions.cs

* Update SdcaLogisticRegression.cs

* Update SgdCalibrated.cs

* Update SgdCalibratedWithOptions.cs

* Update SgdNonCalibrated.cs

* Update SgdNonCalibratedWithOptions.cs

* Update SymbolicSgdLogisticRegression.cs

* Update SymbolicSgdLogisticRegressionWithOptions.cs

* Update Program.cs

changing back

* Update Program.cs

* Update Program.cs

* Update Program.cs

* Update Program.cs

* Update Program.cs

* fixed tab issues

* fixed indentations

* fixed commented-on parts
  • Loading branch information
sierralee51 authored Jul 3, 2019
1 parent 4fecfb2 commit ce9b38b
Show file tree
Hide file tree
Showing 43 changed files with 1,513 additions and 713 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,43 @@ 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
Expand All @@ -48,7 +56,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 +81,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 +94,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 +128,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 @@ -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,29 @@ public static void Example()
};

// Define the trainer.
var pipeline = mlContext.BinaryClassification.Trainers.AveragedPerceptron(options);
var pipeline = mlContext.BinaryClassification.Trainers
.AveragedPerceptron(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}");

// Expected output:
// Label: True, Prediction: True
Expand All @@ -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:
Expand All @@ -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();
Expand All @@ -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; }
Expand All @@ -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());
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -13,63 +13,79 @@ 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 +96,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 +130,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

0 comments on commit ce9b38b

Please sign in to comment.