Skip to content

Commit 48c01d3

Browse files
committed
Add another example with options
1 parent db5d71d commit 48c01d3

File tree

3 files changed

+50
-15
lines changed

3 files changed

+50
-15
lines changed

docs/samples/Microsoft.ML.Samples/Dynamic/Trainers/AnomalyDetection/RandomizedPcaSample.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public static void Example()
4444
new DataPoint(){ Features= new float[3] {1, 2, 3} },
4545
new DataPoint(){ Features= new float[3] {0, 1, 0} },
4646
new DataPoint(){ Features= new float[3] {0, 2, 1} },
47-
new DataPoint(){ Features= new float[3] {-100, -50, -100} }
47+
new DataPoint(){ Features= new float[3] {-100, 50, -100} }
4848
};
4949

5050
// Convert native C# class to IDataView, a consumble format to ML.NET functions.

src/Microsoft.ML.PCA/PCACatalog.cs

+12
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ public static PrincipalComponentAnalysisEstimator ProjectToPrincipalComponents(t
4747
/// <param name="oversampling">Oversampling parameter for randomized PCA training.</param>
4848
/// <param name="center">If enabled, data is centered to be zero mean.</param>
4949
/// <param name="seed">The seed for random number generation.</param>
50+
/// <example>
51+
/// <format type="text/markdown">
52+
/// <![CDATA[
53+
/// [!code-csharp[RPCA](~/../docs/samples/docs/samples/Microsoft.ML.Samples/Dynamic/Trainers/AnomalyDetection/RandomizedPcaSample.cs)]
54+
/// ]]></format>
55+
/// </example>
5056
public static RandomizedPcaTrainer RandomizedPca(this AnomalyDetectionCatalog.AnomalyDetectionTrainers catalog,
5157
string featureColumnName = DefaultColumnNames.Features,
5258
string exampleWeightColumnName = null,
@@ -65,6 +71,12 @@ public static RandomizedPcaTrainer RandomizedPca(this AnomalyDetectionCatalog.An
6571
/// </summary>
6672
/// <param name="catalog">The anomaly detection catalog trainer object.</param>
6773
/// <param name="options">Advanced options to the algorithm.</param>
74+
/// <example>
75+
/// <format type="text/markdown">
76+
/// <![CDATA[
77+
/// [!code-csharp[RPCA](~/../docs/samples/docs/samples/Microsoft.ML.Samples/Dynamic/Trainers/AnomalyDetection/RandomizedPcaSampleWithOptions.cs)]
78+
/// ]]></format>
79+
/// </example>
6880
public static RandomizedPcaTrainer RandomizedPca(this AnomalyDetectionCatalog.AnomalyDetectionTrainers catalog, Options options)
6981
{
7082
Contracts.CheckValue(catalog, nameof(catalog));

test/Microsoft.ML.Tests/AnomalyDetectionTests.cs

+37-14
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,37 @@ public void NoAnomalyTest()
5050
Assert.Throws<ArgumentOutOfRangeException>(() => ML.AnomalyDetection.Evaluate(transformedData));
5151
}
5252

53+
[Fact]
54+
public static void RandomizedPcaInMemory()
55+
{
56+
// Create a new context for ML.NET operations. It can be used for exception tracking and logging,
57+
// as a catalog of available operations and as the source of randomness.
58+
// Setting the seed to a fixed number in this example to make outputs deterministic.
59+
var mlContext = new MLContext(seed: 0);
60+
61+
// Create an anomaly detector. Its underlying algorithm is randomized PCA.
62+
var trainer1 = mlContext.AnomalyDetection.Trainers.RandomizedPca(featureColumnName: nameof(DataPoint.Features), rank: 1, center: false);
63+
64+
// Test the first detector.
65+
ExecutePipelineWithGivenRandomizedPcaTrainer(mlContext, trainer1);
66+
67+
// Object required in the creation of another detector.
68+
var options = new Trainers.RandomizedPcaTrainer.Options()
69+
{
70+
FeatureColumn = nameof(DataPoint.Features),
71+
Rank = 1,
72+
Center = false
73+
};
74+
75+
// Create anther anomaly detector. Its underlying algorithm is randomized PCA.
76+
var trainer2 = mlContext.AnomalyDetection.Trainers.RandomizedPca(options);
77+
78+
// Test the second detector.
79+
ExecutePipelineWithGivenRandomizedPcaTrainer(mlContext, trainer2);
80+
}
81+
5382
/// <summary>
54-
/// Example with 3 feature values.
83+
/// Example with 3 feature values used in <see cref="ExecutePipelineWithGivenRandomizedPcaTrainer"/>.
5584
/// </summary>
5685
private class DataPoint
5786
{
@@ -60,7 +89,7 @@ private class DataPoint
6089
}
6190

6291
/// <summary>
63-
/// Class used to capture prediction of <see cref="DataPoint"/> in <see cref="RandomizedPcaInMemory"/>.
92+
/// Class used to capture prediction of <see cref="DataPoint"/> in <see cref="ExecutePipelineWithGivenRandomizedPcaTrainer"/>.
6493
/// </summary>
6594
#pragma warning disable 649
6695
private class Result
@@ -72,32 +101,26 @@ private class Result
72101
}
73102
#pragma warning restore 649
74103

75-
[Fact]
76-
public void RandomizedPcaInMemory()
104+
/// <summary>
105+
/// Help function used to execute trainers defined in <see cref="RandomizedPcaInMemory"/>.
106+
/// </summary>
107+
private static void ExecutePipelineWithGivenRandomizedPcaTrainer(MLContext mlContext, Trainers.RandomizedPcaTrainer trainer)
77108
{
78-
// Create a new context for ML.NET operations. It can be used for exception tracking and logging,
79-
// as a catalog of available operations and as the source of randomness.
80-
// Setting the seed to a fixed number in this example to make outputs deterministic.
81-
var mlContext = new MLContext(seed: 0);
82-
83109
var samples = new List<DataPoint>()
84110
{
85111
new DataPoint(){ Features= new float[3] {1, 0, 0} },
86112
new DataPoint(){ Features= new float[3] {0, 2, 1} },
87113
new DataPoint(){ Features= new float[3] {1, 2, 3} },
88114
new DataPoint(){ Features= new float[3] {0, 1, 0} },
89115
new DataPoint(){ Features= new float[3] {0, 2, 1} },
90-
new DataPoint(){ Features= new float[3] {-100, -50, -100} }
116+
new DataPoint(){ Features= new float[3] {-100, 50, -100} }
91117
};
92118

93119
// Convert native C# class to IDataView, a consumble format to ML.NET functions.
94120
var data = mlContext.Data.LoadFromEnumerable(samples);
95121

96-
// Create an anomaly detector. Its underlying algorithm is randomized PCA.
97-
var pipeline = mlContext.AnomalyDetection.Trainers.RandomizedPca(featureColumnName: nameof(DataPoint.Features), rank: 1, center: false);
98-
99122
// Train the anomaly detector.
100-
var model = pipeline.Fit(data);
123+
var model = trainer.Fit(data);
101124

102125
// Apply the trained model on the training data.
103126
var transformed = model.Transform(data);

0 commit comments

Comments
 (0)