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

Reformatted Regression samples to width 85 #3948

Merged
merged 4 commits into from
Jul 3, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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 @@ -8,37 +8,47 @@ namespace Samples.Dynamic.Trainers.Regression
{
public static class FastForestRegression
{
// This example requires installation of additional NuGet package
// <a href="https://www.nuget.org/packages/Microsoft.ML.FastTree/">Microsoft.ML.FastTree</a>.

Choose a reason for hiding this comment

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

There's an extra line here that I don't think you need.

// This example requires installation of additional NuGet package for
// Microsoft.ML.FastTree found 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 = mlContext.Regression.Trainers.FastForest(labelColumnName: nameof(DataPoint.Label), featureColumnName: nameof(DataPoint.Features));
var pipeline = mlContext.Regression.Trainers.FastForest(
labelColumnName: nameof(DataPoint.Label),
featureColumnName: nameof(DataPoint.Features));

// 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(5, seed: 123));
// Create testing data. Use different random seed to make it different
// from training data.
Copy link
Member

Choose a reason for hiding this comment

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

  	 [](start = 0, length = 3)

please use four spaces instead of tabs. https://blogs.msdn.microsoft.com/zainnab/2010/03/14/how-to-convert-tabs-to-spaces-and-vice-versa/

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 sorry, I think I missed a few lines while untabifying the tt files. Will fix that and commit.

var testData = mlContext.Data.LoadFromEnumerable(
GenerateRandomDataPoints(5, 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 for the Label, side by side with the actual Label for comparison.
// Look at 5 predictions for the Label, side by side with the actual
// Label for comparison.
foreach (var p in predictions)
Console.WriteLine($"Label: {p.Label:F3}, Prediction: {p.Score:F3}");

Expand All @@ -60,7 +70,8 @@ public static void Example()
// RSquared: 0.96 (closer to 1 is better. The worest case is 0)
}

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);
for (int i = 0; i < count; i++)
Expand All @@ -70,12 +81,14 @@ private static IEnumerable<DataPoint> GenerateRandomDataPoints(int count, int se
{
Label = label,
// Create random features that are correlated with the label.
Features = Enumerable.Repeat(label, 50).Select(x => x + (float)random.NextDouble()).ToArray()
Features = Enumerable.Repeat(label, 50).Select(
x => x + (float)random.NextDouble()).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 float Label { get; set; }
Expand All @@ -95,10 +108,12 @@ private class Prediction
// Print some evaluation metrics to regression problems.
private static void PrintMetrics(RegressionMetrics metrics)
{
Console.WriteLine($"Mean Absolute Error: {metrics.MeanAbsoluteError:F2}");
Console.WriteLine($"Mean Squared Error: {metrics.MeanSquaredError:F2}");
Console.WriteLine($"Root Mean Squared Error: {metrics.RootMeanSquaredError:F2}");
Console.WriteLine($"RSquared: {metrics.RSquared:F2}");
Console.WriteLine("Mean Absolute Error: " + metrics.MeanAbsoluteError);
Console.WriteLine("Mean Squared Error: " + metrics.MeanSquaredError);
Console.WriteLine(
"Root Mean Squared Error: " + metrics.RootMeanSquaredError);

Console.WriteLine("RSquared: " + metrics.RSquared);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
<#@ include file="RegressionSamplesTemplate.ttinclude"#>

<#+
string ClassHeader = @"// This example requires installation of additional NuGet package
// <a href=""https://www.nuget.org/packages/Microsoft.ML.FastTree/"">Microsoft.ML.FastTree</a>. ";
string ClassHeader = @"
// This example requires installation of additional NuGet package for

Choose a reason for hiding this comment

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

Since you put this on a new line, it added an extra empty line for all the .cs files that use this .tt file. If you fix this and run custom tool, it'll fix the other cases too (I commented about one)

// Microsoft.ML.FastTree found at
// https://www.nuget.org/packages/Microsoft.ML.FastTree/";

string ClassName="FastForestRegression";
string ExtraUsing = null;
string Trainer = @"FastForest(labelColumnName: nameof(DataPoint.Label), featureColumnName: nameof(DataPoint.Features))";
string Trainer = @"FastForest(
labelColumnName: nameof(DataPoint.Label),
featureColumnName: nameof(DataPoint.Features))";

string TrainerOptions = null;

string ExpectedOutputPerInstance= @"// Expected output:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,23 @@ namespace Samples.Dynamic.Trainers.Regression
{
public static class FastForestWithOptionsRegression
{
// This example requires installation of additional NuGet package
// <a href="https://www.nuget.org/packages/Microsoft.ML.FastTree/">Microsoft.ML.FastTree</a>.

Copy link
Contributor

Choose a reason for hiding this comment

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

extra line here too?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep @RadicalRayan explained why below. I think he's asking Zeeshan about the feasibility of removing the line from all of the tt files with the NuGet package installation comment.

// This example requires installation of additional NuGet package for
// Microsoft.ML.FastTree found 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 trainer options.
Expand All @@ -43,16 +47,20 @@ public static void Example()
// 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(5, seed: 123));
// Create testing data. Use different random seed to make it different
// from training data.
Copy link
Contributor

Choose a reason for hiding this comment

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

tab issue here

var testData = mlContext.Data.LoadFromEnumerable(
GenerateRandomDataPoints(5, 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 for the Label, side by side with the actual Label for comparison.
// Look at 5 predictions for the Label, side by side with the actual
// Label for comparison.
foreach (var p in predictions)
Console.WriteLine($"Label: {p.Label:F3}, Prediction: {p.Score:F3}");

Expand All @@ -74,7 +82,8 @@ public static void Example()
// RSquared: 0.95 (closer to 1 is better. The worest case is 0)
}

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);
for (int i = 0; i < count; i++)
Expand All @@ -84,12 +93,14 @@ private static IEnumerable<DataPoint> GenerateRandomDataPoints(int count, int se
{
Label = label,
// Create random features that are correlated with the label.
Features = Enumerable.Repeat(label, 50).Select(x => x + (float)random.NextDouble()).ToArray()
Features = Enumerable.Repeat(label, 50).Select(
x => x + (float)random.NextDouble()).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 float Label { get; set; }
Expand All @@ -109,10 +120,12 @@ private class Prediction
// Print some evaluation metrics to regression problems.
private static void PrintMetrics(RegressionMetrics metrics)
{
Console.WriteLine($"Mean Absolute Error: {metrics.MeanAbsoluteError:F2}");
Console.WriteLine($"Mean Squared Error: {metrics.MeanSquaredError:F2}");
Console.WriteLine($"Root Mean Squared Error: {metrics.RootMeanSquaredError:F2}");
Console.WriteLine($"RSquared: {metrics.RSquared:F2}");
Console.WriteLine("Mean Absolute Error: " + metrics.MeanAbsoluteError);
Console.WriteLine("Mean Squared Error: " + metrics.MeanSquaredError);
Console.WriteLine(
"Root Mean Squared Error: " + metrics.RootMeanSquaredError);

Console.WriteLine("RSquared: " + metrics.RSquared);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<#@ include file="RegressionSamplesTemplate.ttinclude"#>

<#+
string ClassHeader = @"// This example requires installation of additional NuGet package
// <a href=""https://www.nuget.org/packages/Microsoft.ML.FastTree/"">Microsoft.ML.FastTree</a>. ";
string ClassHeader = @"
// This example requires installation of additional NuGet package for

Choose a reason for hiding this comment

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

I think this will cause the same problem too.

// Microsoft.ML.FastTree found at
// https://www.nuget.org/packages/Microsoft.ML.FastTree/";

string ClassName="FastForestWithOptionsRegression";
string ExtraUsing = "using Microsoft.ML.Trainers.FastTree;";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,47 @@ namespace Samples.Dynamic.Trainers.Regression
{
public static class FastTreeRegression
{
// 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 found 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 = mlContext.Regression.Trainers.FastTree(labelColumnName: nameof(DataPoint.Label), featureColumnName: nameof(DataPoint.Features));
var pipeline = mlContext.Regression.Trainers.FastForest(
labelColumnName: nameof(DataPoint.Label),
featureColumnName: nameof(DataPoint.Features));

// 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(5, seed: 123));
// Create testing data. Use different random seed to make it different
// from training data.
var testData = mlContext.Data.LoadFromEnumerable(
GenerateRandomDataPoints(5, 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 for the Label, side by side with the actual Label for comparison.
// Look at 5 predictions for the Label, side by side with the actual
// Label for comparison.
foreach (var p in predictions)
Console.WriteLine($"Label: {p.Label:F3}, Prediction: {p.Score:F3}");

Expand All @@ -60,7 +70,8 @@ public static void Example()
// RSquared: 0.99 (closer to 1 is better. The worest case is 0)
}

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);
for (int i = 0; i < count; i++)
Expand All @@ -70,12 +81,14 @@ private static IEnumerable<DataPoint> GenerateRandomDataPoints(int count, int se
{
Label = label,
// Create random features that are correlated with the label.
Features = Enumerable.Repeat(label, 50).Select(x => x + (float)random.NextDouble()).ToArray()
Features = Enumerable.Repeat(label, 50).Select(
x => x + (float)random.NextDouble()).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 float Label { get; set; }
Expand All @@ -95,10 +108,12 @@ private class Prediction
// Print some evaluation metrics to regression problems.
private static void PrintMetrics(RegressionMetrics metrics)
{
Console.WriteLine($"Mean Absolute Error: {metrics.MeanAbsoluteError:F2}");
Console.WriteLine($"Mean Squared Error: {metrics.MeanSquaredError:F2}");
Console.WriteLine($"Root Mean Squared Error: {metrics.RootMeanSquaredError:F2}");
Console.WriteLine($"RSquared: {metrics.RSquared:F2}");
Console.WriteLine("Mean Absolute Error: " + metrics.MeanAbsoluteError);
Console.WriteLine("Mean Squared Error: " + metrics.MeanSquaredError);
Console.WriteLine(
"Root Mean Squared Error: " + metrics.RootMeanSquaredError);

Console.WriteLine("RSquared: " + metrics.RSquared);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
<#@ include file="RegressionSamplesTemplate.ttinclude"#>

<#+
string ClassHeader = @"// This example requires installation of additional NuGet package
// <a href=""https://www.nuget.org/packages/Microsoft.ML.FastTree/"">Microsoft.ML.FastTree</a>. ";
string ClassHeader = @"
// This example requires installation of additional NuGet package for
// Microsoft.ML.FastTree found at
// https://www.nuget.org/packages/Microsoft.ML.FastTree/";

string ClassName="FastTreeRegression";
string ExtraUsing = null;
string Trainer = @"FastTree(labelColumnName: nameof(DataPoint.Label), featureColumnName: nameof(DataPoint.Features))";
string Trainer = @"FastForest(
labelColumnName: nameof(DataPoint.Label),
featureColumnName: nameof(DataPoint.Features))";
string TrainerOptions = null;

string ExpectedOutputPerInstance= @"// Expected output:
Expand Down
Loading