Skip to content

Commit

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

* fixing errors

* reformatted MulticlassClassification samples

* Update LbfgsMaximumEntropy.cs

getting rid of whitespace

* Update LbfgsMaximumEntropy.cs

* Update LbfgsMaximumEntropyWithOptions.cs

getting rid of whitespace

* Update LightGbmWithOptions.cs

fixing whitespace

* Update LbfgsMaximumEntropy.cs

* Update LightGbm.cs

fixing whitespace

* Update LightGbm.cs

fixing whitespace

* Update LightGbmWithOptions.cs

fixing whitespace

* Update MulticlassClassification.ttinclude

fixing whitespace

* Update MulticlassClassification.ttinclude

fixing whitespace

* Update NaiveBayes.cs

fixing whitespace

* Update NaiveBayes.tt

fixing whitespace

* Update NaiveBayes.tt

* Update OneVersusAll.cs

fixing whitespace

* Update PairwiseCoupling.cs

fixing whitespace

* Update SdcaMaximumEntropy.cs

fixing whitespace

* Update SdcaMaximumEntropyWithOptions.cs

fixing whitespace

* Update SdcaNonCalibrated.cs

fixing whitespace

* Update SdcaNonCalibratedWithOptions.cs

fixing whitespace

* Update SdcaNonCalibrated.cs

fixing whitespace

* Update SdcaNonCalibrated.cs

* Update LbfgsMaximumEntropy.cs

* Update LbfgsMaximumEntropy.cs

* Update LbfgsMaximumEntropyWithOptions.cs

* Update LightGbm.cs

* Update LightGbmWithOptions.cs

* Update MulticlassClassification.ttinclude

* Update NaiveBayes.cs

* Update OneVersusAll.cs

* Update PairwiseCoupling.cs

* Update SdcaMaximumEntropy.cs

* Update SdcaMaximumEntropy.cs

* Update SdcaMaximumEntropyWithOptions.cs

* Update SdcaNonCalibrated.cs

* Update SdcaNonCalibratedWithOptions.cs

* fixed tabbing issue

* fixed indentations

* aligned comments

* fixed some indentation and spacing issues

* fixed extra empty lines

* fixed some more indentation issue
  • Loading branch information
sierralee51 authored Jul 3, 2019
1 parent 882a6d9 commit 4fecfb2
Show file tree
Hide file tree
Showing 25 changed files with 649 additions and 336 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,48 @@ public static class LbfgsMaximumEntropy
{
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 =
// Convert the string labels into key types.
mlContext.Transforms.Conversion.MapValueToKey(nameof(DataPoint.Label))
// Apply LbfgsMaximumEntropy multiclass trainer.
.Append(mlContext.MulticlassClassification.Trainers.LbfgsMaximumEntropy());
// Convert the string labels into key types.
mlContext.Transforms.Conversion
.MapValueToKey(nameof(DataPoint.Label))
// Apply LbfgsMaximumEntropy multiclass trainer.
.Append(mlContext.MulticlassClassification.Trainers
.LbfgsMaximumEntropy());

// 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();

// Look at 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: 1, Prediction: 1
Expand All @@ -52,7 +61,9 @@ public static void Example()
// Label: 3, Prediction: 3

// Evaluate the overall metrics
var metrics = mlContext.MulticlassClassification.Evaluate(transformedTestData);
var metrics = mlContext.MulticlassClassification
.Evaluate(transformedTestData);

PrintMetrics(metrics);

// Expected output:
Expand All @@ -72,8 +83,11 @@ public static void Example()
// Precision ||0.9308 |0.9593 |0.8580 |
}

// Generates random uniform doubles in [-0.5, 0.5) range with labels 1, 2 or 3.
private static IEnumerable<DataPoint> GenerateRandomDataPoints(int count, int seed=0)
// Generates random uniform doubles in [-0.5, 0.5)
// range with labels 1, 2 or 3.
private static IEnumerable<DataPoint> GenerateRandomDataPoints(int count,
int seed=0)

{
var random = new Random(seed);
float randomFloat() => (float)(random.NextDouble() - 0.5);
Expand All @@ -85,13 +99,17 @@ private static IEnumerable<DataPoint> GenerateRandomDataPoints(int count, int se
{
Label = (uint)label,
// Create random features that are correlated with the label.
// The feature values are slightly increased by adding a constant multiple of label.
Features = Enumerable.Repeat(label, 20).Select(x => randomFloat() + label * 0.2f).ToArray()
// The feature values are slightly increased by adding a
// constant multiple of label.
Features = Enumerable.Repeat(label, 20)
.Select(x => randomFloat() + label * 0.2f).ToArray()

};
}
}

// Example with label and 20 feature values. A data set is a collection of such examples.
// Example with label and 20 feature values. A data set is a collection of
// such examples.
private class DataPoint
{
public uint Label { get; set; }
Expand All @@ -114,8 +132,11 @@ public static void PrintMetrics(MulticlassClassificationMetrics metrics)
Console.WriteLine($"Micro Accuracy: {metrics.MicroAccuracy:F2}");
Console.WriteLine($"Macro Accuracy: {metrics.MacroAccuracy:F2}");
Console.WriteLine($"Log Loss: {metrics.LogLoss:F2}");
Console.WriteLine($"Log Loss Reduction: {metrics.LogLossReduction:F2}\n");
Console.WriteLine(
$"Log Loss Reduction: {metrics.LogLossReduction:F2}\n");

Console.WriteLine(metrics.ConfusionMatrix.GetFormattedConfusionTable());
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ string TrainerOptions = null;
string OptionsInclude = "";
string Comments = "";
bool CacheData = false;
string DataGenerationComments= "// Generates random uniform doubles in [-0.5, 0.5) range with labels 1, 2 or 3.";
string DataGenerationComments= "// Generates random uniform doubles in [-0.5, 0.5)"
+ "\n // range with labels 1, 2 or 3.";

string ExpectedOutputPerInstance = @"// Expected output:
// Label: 1, Prediction: 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@ public static class LbfgsMaximumEntropyWithOptions
{
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 @@ -32,27 +34,32 @@ public static void Example()

// Define the trainer.
var pipeline =
// Convert the string labels into key types.
mlContext.Transforms.Conversion.MapValueToKey("Label")
// Apply LbfgsMaximumEntropy multiclass trainer.
.Append(mlContext.MulticlassClassification.Trainers.LbfgsMaximumEntropy(options));
// Convert the string labels into key types.
mlContext.Transforms.Conversion.MapValueToKey("Label")
// Apply LbfgsMaximumEntropy multiclass trainer.
.Append(mlContext.MulticlassClassification.Trainers
.LbfgsMaximumEntropy(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();

// Look at 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: 1, Prediction: 1
Expand All @@ -62,7 +69,9 @@ public static void Example()
// Label: 3, Prediction: 3

// Evaluate the overall metrics
var metrics = mlContext.MulticlassClassification.Evaluate(transformedTestData);
var metrics = mlContext.MulticlassClassification
.Evaluate(transformedTestData);

PrintMetrics(metrics);

// Expected output:
Expand All @@ -82,8 +91,11 @@ public static void Example()
// Precision ||0.9304 |0.9593 |0.8529 |
}

// Generates random uniform doubles in [-0.5, 0.5) range with labels 1, 2 or 3.
private static IEnumerable<DataPoint> GenerateRandomDataPoints(int count, int seed=0)
// Generates random uniform doubles in [-0.5, 0.5)
// range with labels 1, 2 or 3.
private static IEnumerable<DataPoint> GenerateRandomDataPoints(int count,
int seed=0)

{
var random = new Random(seed);
float randomFloat() => (float)(random.NextDouble() - 0.5);
Expand All @@ -95,13 +107,17 @@ private static IEnumerable<DataPoint> GenerateRandomDataPoints(int count, int se
{
Label = (uint)label,
// Create random features that are correlated with the label.
// The feature values are slightly increased by adding a constant multiple of label.
Features = Enumerable.Repeat(label, 20).Select(x => randomFloat() + label * 0.2f).ToArray()
// The feature values are slightly increased by adding a
// constant multiple of label.
Features = Enumerable.Repeat(label, 20)
.Select(x => randomFloat() + label * 0.2f).ToArray()

};
}
}

// Example with label and 20 feature values. A data set is a collection of such examples.
// Example with label and 20 feature values. A data set is a collection of
// such examples.
private class DataPoint
{
public uint Label { get; set; }
Expand All @@ -124,8 +140,11 @@ public static void PrintMetrics(MulticlassClassificationMetrics metrics)
Console.WriteLine($"Micro Accuracy: {metrics.MicroAccuracy:F2}");
Console.WriteLine($"Macro Accuracy: {metrics.MacroAccuracy:F2}");
Console.WriteLine($"Log Loss: {metrics.LogLoss:F2}");
Console.WriteLine($"Log Loss Reduction: {metrics.LogLossReduction:F2}\n");
Console.WriteLine(
$"Log Loss Reduction: {metrics.LogLossReduction:F2}\n");

Console.WriteLine(metrics.ConfusionMatrix.GetFormattedConfusionTable());
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ string TrainerOptions = @"LbfgsMaximumEntropyMulticlassTrainer.Options

string OptionsInclude = "using Microsoft.ML.Trainers;";
string Comments = "";
string DataGenerationComments= "// Generates random uniform doubles in [-0.5, 0.5) range with labels 1, 2 or 3.";
string DataGenerationComments= "// Generates random uniform doubles in [-0.5, 0.5)"
+ "\n // range with labels 1, 2 or 3.";

bool CacheData = false;

string ExpectedOutputPerInstance = @"// Expected output:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,43 +8,53 @@ namespace Samples.Dynamic.Trainers.MulticlassClassification
{
public static class LightGbm
{
// This example requires installation of additional NuGet package
// <a href="https://www.nuget.org/packages/Microsoft.ML.FastTree/">Microsoft.ML.FastTree</a>.
// This example requires installation of additional NuGet package for
// Microsoft.ML.FastTree at
// https://www.nuget.org/packages/Microsoft.ML.FastTree/
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 =
// Convert the string labels into key types.
mlContext.Transforms.Conversion.MapValueToKey(nameof(DataPoint.Label))
// Apply LightGbm multiclass trainer.
.Append(mlContext.MulticlassClassification.Trainers.LightGbm());
// Convert the string labels into key types.
mlContext.Transforms.Conversion
.MapValueToKey(nameof(DataPoint.Label))
// Apply LightGbm multiclass trainer.
.Append(mlContext.MulticlassClassification.Trainers
.LightGbm());

// 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();

// Look at 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: 1, Prediction: 1
Expand All @@ -54,7 +64,9 @@ public static void Example()
// Label: 3, Prediction: 3

// Evaluate the overall metrics
var metrics = mlContext.MulticlassClassification.Evaluate(transformedTestData);
var metrics = mlContext.MulticlassClassification
.Evaluate(transformedTestData);

PrintMetrics(metrics);

// Expected output:
Expand All @@ -74,8 +86,11 @@ public static void Example()
// Precision ||0.9936 |1.0000 |0.9701 |
}

// Generates random uniform doubles in [-0.5, 0.5) range with labels 1, 2 or 3.
private static IEnumerable<DataPoint> GenerateRandomDataPoints(int count, int seed=0)
// Generates random uniform doubles in [-0.5, 0.5)
// range with labels 1, 2 or 3.
private static IEnumerable<DataPoint> GenerateRandomDataPoints(int count,
int seed=0)

{
var random = new Random(seed);
float randomFloat() => (float)(random.NextDouble() - 0.5);
Expand All @@ -87,13 +102,17 @@ private static IEnumerable<DataPoint> GenerateRandomDataPoints(int count, int se
{
Label = (uint)label,
// Create random features that are correlated with the label.
// The feature values are slightly increased by adding a constant multiple of label.
Features = Enumerable.Repeat(label, 20).Select(x => randomFloat() + label * 0.2f).ToArray()
// The feature values are slightly increased by adding a
// constant multiple of label.
Features = Enumerable.Repeat(label, 20)
.Select(x => randomFloat() + label * 0.2f).ToArray()

};
}
}

// Example with label and 20 feature values. A data set is a collection of such examples.
// Example with label and 20 feature values. A data set is a collection of
// such examples.
private class DataPoint
{
public uint Label { get; set; }
Expand All @@ -116,8 +135,11 @@ public static void PrintMetrics(MulticlassClassificationMetrics metrics)
Console.WriteLine($"Micro Accuracy: {metrics.MicroAccuracy:F2}");
Console.WriteLine($"Macro Accuracy: {metrics.MacroAccuracy:F2}");
Console.WriteLine($"Log Loss: {metrics.LogLoss:F2}");
Console.WriteLine($"Log Loss Reduction: {metrics.LogLossReduction:F2}\n");
Console.WriteLine(
$"Log Loss Reduction: {metrics.LogLossReduction:F2}\n");

Console.WriteLine(metrics.ConfusionMatrix.GetFormattedConfusionTable());
}
}
}

Loading

0 comments on commit 4fecfb2

Please sign in to comment.