-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Reformatted Recommendation samples to width 85 #3941
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,25 +9,32 @@ namespace Samples.Dynamic.Trainers.Recommendation | |
| public static class MatrixFactorization | ||
| { | ||
|
|
||
| // This example requires installation of additional nuget package <a href="https://www.nuget.org/packages/Microsoft.ML.Recommender/">Microsoft.ML.Recommender</a>. | ||
| // In this example we will create in-memory data and then use it to train | ||
| // a matrix factorization model with default parameters. Afterward, quality metrics are reported. | ||
| // This example requires installation of additional nuget package at | ||
| // for Microsoft.ML.Recommender at | ||
| // https://www.nuget.org/packages/Microsoft.ML.Recommender/ | ||
| // In this example we will create in-memory data and then use it to train | ||
| // a matrix factorization model with default parameters. Afterward, quality | ||
| // metrics are reported. | ||
| 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 = GenerateMatrix(); | ||
|
|
||
| // 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.Recommendation().Trainers.MatrixFactorization(nameof(MatrixElement.Value), nameof(MatrixElement.MatrixColumnIndex), | ||
| nameof(MatrixElement.MatrixRowIndex), 10, 0.2, 1); | ||
| var pipeline = mlContext.Recommendation().Trainers. | ||
| MatrixFactorization(nameof(MatrixElement.Value), | ||
| nameof(MatrixElement.MatrixColumnIndex), | ||
| nameof(MatrixElement.MatrixRowIndex), 10, 0.2, 1); | ||
|
|
||
| // Train the model. | ||
| var model = pipeline.Fit(trainingData); | ||
|
|
@@ -36,11 +43,15 @@ public static void Example() | |
| var transformedData = model.Transform(trainingData); | ||
|
|
||
| // Convert IDataView object to a list. | ||
| var predictions = mlContext.Data.CreateEnumerable<MatrixElement>(transformedData, reuseRowObject: false).Take(5).ToList(); | ||
| var predictions = mlContext.Data | ||
| .CreateEnumerable<MatrixElement>(transformedData, | ||
| reuseRowObject: false).Take(5).ToList(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📝 This layout is very difficult to read. The following would be preferable: var predictions = mlContext.Data
.CreateEnumerable<MatrixElement>(
transformedData,
reuseRowObject: false)
.Take(5)
.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($"Actual value: {p.Value:F3}, Predicted score: {p.Score:F3}"); | ||
| Console.WriteLine($"Actual value: {p.Value:F3}," + | ||
| $"Predicted score: {p.Score:F3}"); | ||
|
|
||
| // Expected output: | ||
| // Actual value: 0.000, Predicted score: 1.234 | ||
|
|
@@ -50,7 +61,10 @@ public static void Example() | |
| // Actual value: 4.000, Predicted score: 2.362 | ||
|
|
||
| // Evaluate the overall metrics | ||
| var metrics = mlContext.Regression.Evaluate(transformedData, labelColumnName: nameof(MatrixElement.Value), scoreColumnName: nameof(MatrixElement.Score)); | ||
| var metrics = mlContext.Regression.Evaluate(transformedData, | ||
| labelColumnName: nameof(MatrixElement.Value), | ||
| scoreColumnName: nameof(MatrixElement.Score)); | ||
|
|
||
| PrintMetrics(metrics); | ||
|
|
||
| // Expected output: | ||
|
|
@@ -60,11 +74,15 @@ public static void Example() | |
| // RSquared: 0.61 (closer to 1 is better. The worest case is 0) | ||
| } | ||
|
|
||
| // The following variables are used to define the shape of the example matrix. Its shape is MatrixRowCount-by-MatrixColumnCount. | ||
| // Because in ML.NET key type's minimal value is zero, the first row index is always zero in C# data structure (e.g., MatrixColumnIndex=0 | ||
| // and MatrixRowIndex=0 in MatrixElement below specifies the value at the upper-left corner in the training matrix). If user's row index | ||
| // starts with 1, their row index 1 would be mapped to the 2nd row in matrix factorization module and their first row may contain no values. | ||
| // This behavior is also true to column index. | ||
| // The following variables are used to define the shape of the example | ||
| // matrix. Its shape is MatrixRowCount-by-MatrixColumnCount. Because in | ||
| // ML.NET key type's minimal value is zero, the first row index is always | ||
| // zero in C# data structure (e.g., MatrixColumnIndex=0 and MatrixRowIndex=0 | ||
| // in MatrixElement below specifies the value at the upper-left corner in | ||
| // the training matrix). If user's row index starts with 1, their row index | ||
| // 1 would be mapped to the 2nd row in matrix factorization module and their | ||
| // first row may contain no values. This behavior is also true to column | ||
| // index. | ||
| private const uint MatrixColumnCount = 60; | ||
| private const uint MatrixRowCount = 100; | ||
|
|
||
|
|
@@ -74,32 +92,40 @@ private static List<MatrixElement> GenerateMatrix() | |
| var dataMatrix = new List<MatrixElement>(); | ||
| for (uint i = 0; i < MatrixColumnCount; ++i) | ||
| for (uint j = 0; j < MatrixRowCount; ++j) | ||
| dataMatrix.Add(new MatrixElement() { MatrixColumnIndex = i, MatrixRowIndex = j, Value = (i + j) % 5 }); | ||
| dataMatrix.Add(new MatrixElement() { MatrixColumnIndex = i, | ||
| MatrixRowIndex = j, Value = (i + j) % 5 }); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📝 Incorrect line wrapping and indentation |
||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Empty line. Is it produced by an auto-formatting tool?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, Zeeshan asked us to do that, so that's what we've been doing as a norm. |
||
| return dataMatrix; | ||
| } | ||
|
|
||
| // A class used to define a matrix element and capture its prediction result. | ||
| // A class used to define a matrix element and capture its prediction | ||
| // result. | ||
| private class MatrixElement | ||
| { | ||
| // Matrix column index. Its allowed range is from 0 to MatrixColumnCount - 1. | ||
| // Matrix column index. Its allowed range is from 0 to | ||
| // MatrixColumnCount - 1. | ||
| [KeyType(MatrixColumnCount)] | ||
| public uint MatrixColumnIndex { get; set; } | ||
| // Matrix row index. Its allowed range is from 0 to MatrixRowCount - 1. | ||
| [KeyType(MatrixRowCount)] | ||
| public uint MatrixRowIndex { get; set; } | ||
| // The actual value at the MatrixColumnIndex-th column and the MatrixRowIndex-th row. | ||
| // The actual value at the MatrixColumnIndex-th column and the | ||
| // MatrixRowIndex-th row. | ||
| public float Value { get; set; } | ||
| // The predicted value at the MatrixColumnIndex-th column and the MatrixRowIndex-th row. | ||
| // The predicted value at the MatrixColumnIndex-th column and the | ||
| // MatrixRowIndex-th row. | ||
| public float Score { get; set; } | ||
| } | ||
|
|
||
| // 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); | ||
| } | ||
| } | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📝 This is incorrectly indented