Skip to content

Commit 01b3ec7

Browse files
authored
Reformatted Recommendation samples to width 85 (dotnet#3941)
* Reformatted Recommendation samples * Fix for tab spacing errors * Removed precision for reformatting in Console.WriteLine and fixed KeyType error
1 parent 3139697 commit 01b3ec7

6 files changed

+311
-162
lines changed

docs/samples/Microsoft.ML.Samples/Dynamic/Trainers/Recommendation/MatrixFactorization.cs

Lines changed: 53 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,32 @@ namespace Samples.Dynamic.Trainers.Recommendation
99
public static class MatrixFactorization
1010
{
1111

12-
// This example requires installation of additional nuget package <a href="https://www.nuget.org/packages/Microsoft.ML.Recommender/">Microsoft.ML.Recommender</a>.
13-
// In this example we will create in-memory data and then use it to train
14-
// a matrix factorization model with default parameters. Afterward, quality metrics are reported.
12+
// This example requires installation of additional nuget package at
13+
// for Microsoft.ML.Recommender at
14+
// https://www.nuget.org/packages/Microsoft.ML.Recommender/
15+
// In this example we will create in-memory data and then use it to train
16+
// a matrix factorization model with default parameters. Afterward, quality
17+
// metrics are reported.
1518
public static void Example()
1619
{
17-
// Create a new context for ML.NET operations. It can be used for exception tracking and logging,
18-
// as a catalog of available operations and as the source of randomness.
19-
// Setting the seed to a fixed number in this example to make outputs deterministic.
20+
// Create a new context for ML.NET operations. It can be used for
21+
// exception tracking and logging, as a catalog of available operations
22+
// and as the source of randomness. Setting the seed to a fixed number
23+
// in this example to make outputs deterministic.
2024
var mlContext = new MLContext(seed: 0);
2125

2226
// Create a list of training data points.
2327
var dataPoints = GenerateMatrix();
2428

25-
// Convert the list of data points to an IDataView object, which is consumable by ML.NET API.
29+
// Convert the list of data points to an IDataView object, which is
30+
// consumable by ML.NET API.
2631
var trainingData = mlContext.Data.LoadFromEnumerable(dataPoints);
2732

2833
// Define the trainer.
29-
var pipeline = mlContext.Recommendation().Trainers.MatrixFactorization(nameof(MatrixElement.Value), nameof(MatrixElement.MatrixColumnIndex),
30-
nameof(MatrixElement.MatrixRowIndex), 10, 0.2, 1);
34+
var pipeline = mlContext.Recommendation().Trainers.
35+
MatrixFactorization(nameof(MatrixElement.Value),
36+
nameof(MatrixElement.MatrixColumnIndex),
37+
nameof(MatrixElement.MatrixRowIndex), 10, 0.2, 1);
3138

3239
// Train the model.
3340
var model = pipeline.Fit(trainingData);
@@ -36,11 +43,15 @@ public static void Example()
3643
var transformedData = model.Transform(trainingData);
3744

3845
// Convert IDataView object to a list.
39-
var predictions = mlContext.Data.CreateEnumerable<MatrixElement>(transformedData, reuseRowObject: false).Take(5).ToList();
46+
var predictions = mlContext.Data
47+
.CreateEnumerable<MatrixElement>(transformedData,
48+
reuseRowObject: false).Take(5).ToList();
4049

41-
// Look at 5 predictions for the Label, side by side with the actual Label for comparison.
50+
// Look at 5 predictions for the Label, side by side with the actual
51+
// Label for comparison.
4252
foreach (var p in predictions)
43-
Console.WriteLine($"Actual value: {p.Value:F3}, Predicted score: {p.Score:F3}");
53+
Console.WriteLine($"Actual value: {p.Value:F3}," +
54+
$"Predicted score: {p.Score:F3}");
4455

4556
// Expected output:
4657
// Actual value: 0.000, Predicted score: 1.234
@@ -50,7 +61,10 @@ public static void Example()
5061
// Actual value: 4.000, Predicted score: 2.362
5162

5263
// Evaluate the overall metrics
53-
var metrics = mlContext.Regression.Evaluate(transformedData, labelColumnName: nameof(MatrixElement.Value), scoreColumnName: nameof(MatrixElement.Score));
64+
var metrics = mlContext.Regression.Evaluate(transformedData,
65+
labelColumnName: nameof(MatrixElement.Value),
66+
scoreColumnName: nameof(MatrixElement.Score));
67+
5468
PrintMetrics(metrics);
5569

5670
// Expected output:
@@ -60,11 +74,15 @@ public static void Example()
6074
// RSquared: 0.61 (closer to 1 is better. The worest case is 0)
6175
}
6276

63-
// The following variables are used to define the shape of the example matrix. Its shape is MatrixRowCount-by-MatrixColumnCount.
64-
// 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
65-
// and MatrixRowIndex=0 in MatrixElement below specifies the value at the upper-left corner in the training matrix). If user's row index
66-
// 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.
67-
// This behavior is also true to column index.
77+
// The following variables are used to define the shape of the example
78+
// matrix. Its shape is MatrixRowCount-by-MatrixColumnCount. Because in
79+
// ML.NET key type's minimal value is zero, the first row index is always
80+
// zero in C# data structure (e.g., MatrixColumnIndex=0 and MatrixRowIndex=0
81+
// in MatrixElement below specifies the value at the upper-left corner in
82+
// the training matrix). If user's row index starts with 1, their row index
83+
// 1 would be mapped to the 2nd row in matrix factorization module and their
84+
// first row may contain no values. This behavior is also true to column
85+
// index.
6886
private const uint MatrixColumnCount = 60;
6987
private const uint MatrixRowCount = 100;
7088

@@ -74,32 +92,40 @@ private static List<MatrixElement> GenerateMatrix()
7492
var dataMatrix = new List<MatrixElement>();
7593
for (uint i = 0; i < MatrixColumnCount; ++i)
7694
for (uint j = 0; j < MatrixRowCount; ++j)
77-
dataMatrix.Add(new MatrixElement() { MatrixColumnIndex = i, MatrixRowIndex = j, Value = (i + j) % 5 });
95+
dataMatrix.Add(new MatrixElement() { MatrixColumnIndex = i,
96+
MatrixRowIndex = j, Value = (i + j) % 5 });
97+
7898
return dataMatrix;
7999
}
80100

81-
// A class used to define a matrix element and capture its prediction result.
101+
// A class used to define a matrix element and capture its prediction
102+
// result.
82103
private class MatrixElement
83104
{
84-
// Matrix column index. Its allowed range is from 0 to MatrixColumnCount - 1.
105+
// Matrix column index. Its allowed range is from 0 to
106+
// MatrixColumnCount - 1.
85107
[KeyType(MatrixColumnCount)]
86108
public uint MatrixColumnIndex { get; set; }
87109
// Matrix row index. Its allowed range is from 0 to MatrixRowCount - 1.
88110
[KeyType(MatrixRowCount)]
89111
public uint MatrixRowIndex { get; set; }
90-
// The actual value at the MatrixColumnIndex-th column and the MatrixRowIndex-th row.
112+
// The actual value at the MatrixColumnIndex-th column and the
113+
// MatrixRowIndex-th row.
91114
public float Value { get; set; }
92-
// The predicted value at the MatrixColumnIndex-th column and the MatrixRowIndex-th row.
115+
// The predicted value at the MatrixColumnIndex-th column and the
116+
// MatrixRowIndex-th row.
93117
public float Score { get; set; }
94118
}
95119

96120
// Print some evaluation metrics to regression problems.
97121
private static void PrintMetrics(RegressionMetrics metrics)
98122
{
99-
Console.WriteLine($"Mean Absolute Error: {metrics.MeanAbsoluteError:F2}");
100-
Console.WriteLine($"Mean Squared Error: {metrics.MeanSquaredError:F2}");
101-
Console.WriteLine($"Root Mean Squared Error: {metrics.RootMeanSquaredError:F2}");
102-
Console.WriteLine($"RSquared: {metrics.RSquared:F2}");
123+
Console.WriteLine("Mean Absolute Error: " + metrics.MeanAbsoluteError);
124+
Console.WriteLine("Mean Squared Error: " + metrics.MeanSquaredError);
125+
Console.WriteLine("Root Mean Squared Error: " +
126+
metrics.RootMeanSquaredError);
127+
128+
Console.WriteLine("RSquared: " + metrics.RSquared);
103129
}
104130
}
105131
}

docs/samples/Microsoft.ML.Samples/Dynamic/Trainers/Recommendation/MatrixFactorization.tt

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,18 @@
22

33
<#+
44
string ClassHeader = @"
5-
// This example requires installation of additional nuget package <a href=""https://www.nuget.org/packages/Microsoft.ML.Recommender/"">Microsoft.ML.Recommender</a>.
6-
// In this example we will create in-memory data and then use it to train
7-
// a matrix factorization model with default parameters. Afterward, quality metrics are reported.";
5+
// This example requires installation of additional nuget package at
6+
// for Microsoft.ML.Recommender at
7+
// https://www.nuget.org/packages/Microsoft.ML.Recommender/
8+
// In this example we will create in-memory data and then use it to train
9+
// a matrix factorization model with default parameters. Afterward, quality
10+
// metrics are reported.";
811
string ClassName="MatrixFactorization";
912
string ExtraUsing = null;
10-
string Trainer = @"MatrixFactorization(nameof(MatrixElement.Value), nameof(MatrixElement.MatrixColumnIndex),
11-
nameof(MatrixElement.MatrixRowIndex), 10, 0.2, 1)";
13+
string Trainer = @"
14+
MatrixFactorization(nameof(MatrixElement.Value),
15+
nameof(MatrixElement.MatrixColumnIndex),
16+
nameof(MatrixElement.MatrixRowIndex), 10, 0.2, 1)";
1217
string TrainerOptions = null;
1318

1419
string ExpectedOutputPerInstance= @"// Expected output:
@@ -23,4 +28,4 @@ string ExpectedOutput = @"// Expected output:
2328
// Mean Squared Error: 0.79
2429
// Root Mean Squared Error: 0.89
2530
// RSquared: 0.61 (closer to 1 is better. The worest case is 0)";
26-
#>
31+
#>

docs/samples/Microsoft.ML.Samples/Dynamic/Trainers/Recommendation/MatrixFactorizationTemplate.ttinclude

Lines changed: 45 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,17 @@ namespace Samples.Dynamic.Trainers.Recommendation
1616
<# } #>
1717
public static void Example()
1818
{
19-
// Create a new context for ML.NET operations. It can be used for exception tracking and logging,
20-
// as a catalog of available operations and as the source of randomness.
21-
// Setting the seed to a fixed number in this example to make outputs deterministic.
19+
// Create a new context for ML.NET operations. It can be used for
20+
// exception tracking and logging, as a catalog of available operations
21+
// and as the source of randomness. Setting the seed to a fixed number
22+
// in this example to make outputs deterministic.
2223
var mlContext = new MLContext(seed: 0);
2324

2425
// Create a list of training data points.
2526
var dataPoints = GenerateMatrix();
2627

27-
// Convert the list of data points to an IDataView object, which is consumable by ML.NET API.
28+
// Convert the list of data points to an IDataView object, which is
29+
// consumable by ML.NET API.
2830
var trainingData = mlContext.Data.LoadFromEnumerable(dataPoints);
2931

3032
<# if (TrainerOptions == null) { #>
@@ -35,7 +37,8 @@ namespace Samples.Dynamic.Trainers.Recommendation
3537
var options = new <#=TrainerOptions#>;
3638

3739
// Define the trainer.
38-
var pipeline = mlContext.Recommendation().Trainers.<#=Trainer#>(options);
40+
var pipeline = mlContext.Recommendation().Trainers.<#=Trainer#>(
41+
options);
3942
<# } #>
4043

4144
// Train the model.
@@ -45,26 +48,37 @@ namespace Samples.Dynamic.Trainers.Recommendation
4548
var transformedData = model.Transform(trainingData);
4649

4750
// Convert IDataView object to a list.
48-
var predictions = mlContext.Data.CreateEnumerable<MatrixElement>(transformedData, reuseRowObject: false).Take(5).ToList();
51+
var predictions = mlContext.Data
52+
.CreateEnumerable<MatrixElement>(transformedData,
53+
reuseRowObject: false).Take(5).ToList();
4954

50-
// Look at 5 predictions for the Label, side by side with the actual Label for comparison.
55+
// Look at 5 predictions for the Label, side by side with the actual
56+
// Label for comparison.
5157
foreach (var p in predictions)
52-
Console.WriteLine($"Actual value: {p.Value:F3}, Predicted score: {p.Score:F3}");
58+
Console.WriteLine($"Actual value: {p.Value:F3}," +
59+
$"Predicted score: {p.Score:F3}");
5360

5461
<#=ExpectedOutputPerInstance#>
5562

5663
// Evaluate the overall metrics
57-
var metrics = mlContext.Regression.Evaluate(transformedData, labelColumnName: nameof(MatrixElement.Value), scoreColumnName: nameof(MatrixElement.Score));
64+
var metrics = mlContext.Regression.Evaluate(transformedData,
65+
labelColumnName: nameof(MatrixElement.Value),
66+
scoreColumnName: nameof(MatrixElement.Score));
67+
5868
PrintMetrics(metrics);
5969

6070
<#=ExpectedOutput#>
6171
}
6272

63-
// The following variables are used to define the shape of the example matrix. Its shape is MatrixRowCount-by-MatrixColumnCount.
64-
// 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
65-
// and MatrixRowIndex=0 in MatrixElement below specifies the value at the upper-left corner in the training matrix). If user's row index
66-
// 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.
67-
// This behavior is also true to column index.
73+
// The following variables are used to define the shape of the example
74+
// matrix. Its shape is MatrixRowCount-by-MatrixColumnCount. Because in
75+
// ML.NET key type's minimal value is zero, the first row index is always
76+
// zero in C# data structure (e.g., MatrixColumnIndex=0 and MatrixRowIndex=0
77+
// in MatrixElement below specifies the value at the upper-left corner in
78+
// the training matrix). If user's row index starts with 1, their row index
79+
// 1 would be mapped to the 2nd row in matrix factorization module and their
80+
// first row may contain no values. This behavior is also true to column
81+
// index.
6882
private const uint MatrixColumnCount = 60;
6983
private const uint MatrixRowCount = 100;
7084

@@ -74,32 +88,40 @@ namespace Samples.Dynamic.Trainers.Recommendation
7488
var dataMatrix = new List<MatrixElement>();
7589
for (uint i = 0; i < MatrixColumnCount; ++i)
7690
for (uint j = 0; j < MatrixRowCount; ++j)
77-
dataMatrix.Add(new MatrixElement() { MatrixColumnIndex = i, MatrixRowIndex = j, Value = (i + j) % 5 });
91+
dataMatrix.Add(new MatrixElement() { MatrixColumnIndex = i,
92+
MatrixRowIndex = j, Value = (i + j) % 5 });
93+
7894
return dataMatrix;
7995
}
8096

81-
// A class used to define a matrix element and capture its prediction result.
97+
// A class used to define a matrix element and capture its prediction
98+
// result.
8299
private class MatrixElement
83100
{
84-
// Matrix column index. Its allowed range is from 0 to MatrixColumnCount - 1.
101+
// Matrix column index. Its allowed range is from 0 to
102+
// MatrixColumnCount - 1.
85103
[KeyType(MatrixColumnCount)]
86104
public uint MatrixColumnIndex { get; set; }
87105
// Matrix row index. Its allowed range is from 0 to MatrixRowCount - 1.
88106
[KeyType(MatrixRowCount)]
89107
public uint MatrixRowIndex { get; set; }
90-
// The actual value at the MatrixColumnIndex-th column and the MatrixRowIndex-th row.
108+
// The actual value at the MatrixColumnIndex-th column and the
109+
// MatrixRowIndex-th row.
91110
public float Value { get; set; }
92-
// The predicted value at the MatrixColumnIndex-th column and the MatrixRowIndex-th row.
111+
// The predicted value at the MatrixColumnIndex-th column and the
112+
// MatrixRowIndex-th row.
93113
public float Score { get; set; }
94114
}
95115

96116
// Print some evaluation metrics to regression problems.
97117
private static void PrintMetrics(RegressionMetrics metrics)
98118
{
99-
Console.WriteLine($"Mean Absolute Error: {metrics.MeanAbsoluteError:F2}");
100-
Console.WriteLine($"Mean Squared Error: {metrics.MeanSquaredError:F2}");
101-
Console.WriteLine($"Root Mean Squared Error: {metrics.RootMeanSquaredError:F2}");
102-
Console.WriteLine($"RSquared: {metrics.RSquared:F2}");
119+
Console.WriteLine("Mean Absolute Error: " + metrics.MeanAbsoluteError);
120+
Console.WriteLine("Mean Squared Error: " + metrics.MeanSquaredError);
121+
Console.WriteLine("Root Mean Squared Error: " +
122+
metrics.RootMeanSquaredError);
123+
124+
Console.WriteLine("RSquared: " + metrics.RSquared);
103125
}
104126
}
105127
}

0 commit comments

Comments
 (0)