|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using Microsoft.ML.Data; |
| 5 | + |
| 6 | +namespace Microsoft.ML.Samples.Dynamic.Trainers.AnomalyDetection |
| 7 | +{ |
| 8 | + public static class RandomizedPcaSampleWithOptions |
| 9 | + { |
| 10 | + public static void Example() |
| 11 | + { |
| 12 | + // Create a new context for ML.NET operations. It can be used for exception tracking and logging, |
| 13 | + // as a catalog of available operations and as the source of randomness. |
| 14 | + // Setting the seed to a fixed number in this example to make outputs deterministic. |
| 15 | + var mlContext = new MLContext(seed: 0); |
| 16 | + |
| 17 | + // Training data. |
| 18 | + var samples = new List<DataPoint>() |
| 19 | + { |
| 20 | + new DataPoint(){ Features = new float[3] {1, 0, 0} }, |
| 21 | + new DataPoint(){ Features = new float[3] {0, 2, 1} }, |
| 22 | + new DataPoint(){ Features = new float[3] {1, 2, 3} }, |
| 23 | + new DataPoint(){ Features = new float[3] {0, 1, 0} }, |
| 24 | + new DataPoint(){ Features = new float[3] {0, 2, 1} }, |
| 25 | + new DataPoint(){ Features = new float[3] {-100, 50, -100} } |
| 26 | + }; |
| 27 | + |
| 28 | + // Convert the List<DataPoint> to IDataView, a consumble format to ML.NET functions. |
| 29 | + var data = mlContext.Data.LoadFromEnumerable(samples); |
| 30 | + |
| 31 | + var options = new ML.Trainers.RandomizedPcaTrainer.Options() |
| 32 | + { |
| 33 | + FeatureColumnName = nameof(DataPoint.Features), |
| 34 | + Rank = 1, |
| 35 | + Seed = 10, |
| 36 | + }; |
| 37 | + |
| 38 | + // Create an anomaly detector. Its underlying algorithm is randomized PCA. |
| 39 | + var pipeline = mlContext.AnomalyDetection.Trainers.RandomizedPca(options); |
| 40 | + |
| 41 | + // Train the anomaly detector. |
| 42 | + var model = pipeline.Fit(data); |
| 43 | + |
| 44 | + // Apply the trained model on the training data. |
| 45 | + var transformed = model.Transform(data); |
| 46 | + |
| 47 | + // Read ML.NET predictions into IEnumerable<Result>. |
| 48 | + var results = mlContext.Data.CreateEnumerable<Result>(transformed, reuseRowObject: false).ToList(); |
| 49 | + |
| 50 | + // Let's go through all predictions. |
| 51 | + for (int i = 0; i < samples.Count; ++i) |
| 52 | + { |
| 53 | + // The i-th example's prediction result. |
| 54 | + var result = results[i]; |
| 55 | + |
| 56 | + // The i-th example's feature vector in text format. |
| 57 | + var featuresInText = string.Join(',', samples[i].Features); |
| 58 | + |
| 59 | + if (result.PredictedLabel) |
| 60 | + // The i-th sample is predicted as an inlier. |
| 61 | + Console.WriteLine("The {0}-th example with features [{1}] is an inlier with a score of being inlier {2}", |
| 62 | + i, featuresInText, result.Score); |
| 63 | + else |
| 64 | + // The i-th sample is predicted as an outlier. |
| 65 | + Console.WriteLine("The {0}-th example with features [{1}] is an outlier with a score of being inlier {2}", |
| 66 | + i, featuresInText, result.Score); |
| 67 | + } |
| 68 | + // Lines printed out should be |
| 69 | + // The 0 - th example with features[1, 0, 0] is an inlier with a score of being inlier 0.7453707 |
| 70 | + // The 1 - th example with features[0, 2, 1] is an inlier with a score of being inlier 0.9999999 |
| 71 | + // The 2 - th example with features[1, 2, 3] is an inlier with a score of being inlier 0.8450122 |
| 72 | + // The 3 - th example with features[0, 1, 0] is an inlier with a score of being inlier 0.9428905 |
| 73 | + // The 4 - th example with features[0, 2, 1] is an inlier with a score of being inlier 0.9999999 |
| 74 | + // The 5 - th example with features[-100, 50, -100] is an outlier with a score of being inlier 0 |
| 75 | + } |
| 76 | + |
| 77 | + // Example with 3 feature values. A training data set is a collection of such examples. |
| 78 | + private class DataPoint |
| 79 | + { |
| 80 | + [VectorType(3)] |
| 81 | + public float[] Features { get; set; } |
| 82 | + } |
| 83 | + |
| 84 | + // Class used to capture prediction of DataPoint. |
| 85 | + private class Result |
| 86 | + { |
| 87 | + // Outlier gets false while inlier has true. |
| 88 | + public bool PredictedLabel { get; set; } |
| 89 | + // Outlier gets smaller score. |
| 90 | + public float Score { get; set; } |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments