Ejemplo n.º 1
0
        /// <summary>
        /// Help function used to execute trainers defined in <see cref="RandomizedPcaInMemory"/>.
        /// </summary>
        private static void ExecuteRandomizedPcaTrainerChangeThreshold(MLContext mlContext, Trainers.RandomizedPcaTrainer trainer)
        {
            var samples = new List <DataPoint>()
            {
                new DataPoint()
                {
                    Features = new float[3] {
                        0, 2, 1
                    }
                },
                new DataPoint()
                {
                    Features = new float[3] {
                        0, 2, 3
                    }
                },
                new DataPoint()
                {
                    Features = new float[3] {
                        0, 2, 4
                    }
                },
                new DataPoint()
                {
                    Features = new float[3] {
                        0, 2, 1
                    }
                },
                new DataPoint()
                {
                    Features = new float[3] {
                        0, 2, 2
                    }
                },
                new DataPoint()
                {
                    Features = new float[3] {
                        0, 2, 3
                    }
                },
                new DataPoint()
                {
                    Features = new float[3] {
                        0, 2, 4
                    }
                },
                new DataPoint()
                {
                    Features = new float[3] {
                        1, 0, 0
                    }
                }
            };

            // Convert the List<DataPoint> to IDataView, a consumble format to ML.NET functions.
            var data = mlContext.Data.LoadFromEnumerable(samples);

            // Train the anomaly detector.
            var model = trainer.Fit(data);

            var transformer = mlContext.AnomalyDetection.ChangeModelThreshold(model, 0.3f);

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

            // Read ML.NET predictions into IEnumerable<Result>.
            var results = mlContext.Data.CreateEnumerable <Result>(transformed, reuseRowObject: false).ToList();

            // Outlier should be predicted as true.
            Assert.True(results[0].PredictedLabel);
            Assert.InRange(results[0].Score, 0.3, 1);
            // Inlier should be predicted as false.
            Assert.False(results[1].PredictedLabel);
            Assert.InRange(results[1].Score, 0, 0.3);
            // Inlier should be predicted as false.
            Assert.False(results[2].PredictedLabel);
            Assert.InRange(results[2].Score, 0, 0.3);
            // Outlier should be predicted as true.
            Assert.True(results[3].PredictedLabel);
            Assert.InRange(results[3].Score, 0.3, 1);

            // Inlier should be predicted as false.
            Assert.False(results[4].PredictedLabel);
            Assert.InRange(results[4].Score, 0, 0.3);

            // Inlier should be predicted as false.
            Assert.False(results[5].PredictedLabel);
            Assert.InRange(results[5].Score, 0, 0.3);
            // Inlier should be predicted as false.
            Assert.False(results[6].PredictedLabel);
            Assert.InRange(results[6].Score, 0, 0.3);

            // Outlier should be predicted as true.
            Assert.True(results[7].PredictedLabel);
            Assert.InRange(results[7].Score, 0.3, 1);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Help function used to execute trainers defined in <see cref="RandomizedPcaInMemory"/>.
        /// </summary>
        private static void ExecutePipelineWithGivenRandomizedPcaTrainer(MLContext mlContext, Trainers.RandomizedPcaTrainer trainer)
        {
            var samples = new List <DataPoint>()
            {
                new DataPoint()
                {
                    Features = new float[3] {
                        0, 2, 1
                    }
                },
                new DataPoint()
                {
                    Features = new float[3] {
                        0, 2, 3
                    }
                },
                new DataPoint()
                {
                    Features = new float[3] {
                        0, 2, 4
                    }
                },
                new DataPoint()
                {
                    Features = new float[3] {
                        0, 2, 1
                    }
                },
                new DataPoint()
                {
                    Features = new float[3] {
                        0, 2, 2
                    }
                },
                new DataPoint()
                {
                    Features = new float[3] {
                        0, 2, 3
                    }
                },
                new DataPoint()
                {
                    Features = new float[3] {
                        0, 2, 4
                    }
                },
                new DataPoint()
                {
                    Features = new float[3] {
                        1, 0, 0
                    }
                }
            };

            // Convert the List<DataPoint> to IDataView, a consumable format to ML.NET functions.
            var data = mlContext.Data.LoadFromEnumerable(samples);

            // Train the anomaly detector.
            var model = trainer.Fit(data);

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

            // Read ML.NET predictions into IEnumerable<Result>.
            var results = mlContext.Data.CreateEnumerable <Result>(transformed, reuseRowObject: false).ToList();

            // First 5 examples are inliers.
            for (int i = 0; i < 7; ++i)
            {
                // Inlier should be predicted as false.
                Assert.False(results[i].PredictedLabel);
                // Higher score means closer to inlier.
                Assert.InRange(results[i].Score, 0, 0.5);
            }

            // Last example is outlier. Note that outlier should be predicted as true.
            Assert.True(results[7].PredictedLabel);
            Assert.InRange(results[7].Score, 0.5, 1);
        }