@@ -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