@@ -50,8 +50,37 @@ public void NoAnomalyTest()
50
50
Assert . Throws < ArgumentOutOfRangeException > ( ( ) => ML . AnomalyDetection . Evaluate ( transformedData ) ) ;
51
51
}
52
52
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
+
53
82
/// <summary>
54
- /// Example with 3 feature values.
83
+ /// Example with 3 feature values used in <see cref="ExecutePipelineWithGivenRandomizedPcaTrainer"/> .
55
84
/// </summary>
56
85
private class DataPoint
57
86
{
@@ -60,7 +89,7 @@ private class DataPoint
60
89
}
61
90
62
91
/// <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 "/>.
64
93
/// </summary>
65
94
#pragma warning disable 649
66
95
private class Result
@@ -72,32 +101,26 @@ private class Result
72
101
}
73
102
#pragma warning restore 649
74
103
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 )
77
108
{
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
-
83
109
var samples = new List < DataPoint > ( )
84
110
{
85
111
new DataPoint ( ) { Features = new float [ 3 ] { 1 , 0 , 0 } } ,
86
112
new DataPoint ( ) { Features = new float [ 3 ] { 0 , 2 , 1 } } ,
87
113
new DataPoint ( ) { Features = new float [ 3 ] { 1 , 2 , 3 } } ,
88
114
new DataPoint ( ) { Features = new float [ 3 ] { 0 , 1 , 0 } } ,
89
115
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 } }
91
117
} ;
92
118
93
119
// Convert native C# class to IDataView, a consumble format to ML.NET functions.
94
120
var data = mlContext . Data . LoadFromEnumerable ( samples ) ;
95
121
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
-
99
122
// Train the anomaly detector.
100
- var model = pipeline . Fit ( data ) ;
123
+ var model = trainer . Fit ( data ) ;
101
124
102
125
// Apply the trained model on the training data.
103
126
var transformed = model . Transform ( data ) ;
0 commit comments