|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using Microsoft.ML.Data; |
| 4 | + |
| 5 | +namespace Microsoft.ML.Samples.Dynamic |
| 6 | +{ |
| 7 | + public static class BootstrapSample |
| 8 | + { |
| 9 | + public static void Example() |
| 10 | + { |
| 11 | + // Create a new context for ML.NET operations. It can be used for exception tracking and logging, |
| 12 | + // as a catalog of available operations and as the source of randomness. |
| 13 | + var mlContext = new MLContext(); |
| 14 | + |
| 15 | + // Get a small dataset as an IEnumerable and them read it as ML.NET's data type. |
| 16 | + IEnumerable<SamplesUtils.DatasetUtils.BinaryLabelFloatFeatureVectorSample> enumerableOfData = SamplesUtils.DatasetUtils.GenerateBinaryLabelFloatFeatureVectorSamples(5); |
| 17 | + var data = mlContext.Data.ReadFromEnumerable(enumerableOfData); |
| 18 | + |
| 19 | + // Look at the original dataset |
| 20 | + Console.WriteLine($"Label\tFeatures[0]"); |
| 21 | + foreach (var row in enumerableOfData) |
| 22 | + { |
| 23 | + Console.WriteLine($"{row.Label}\t{row.Features[0]}"); |
| 24 | + } |
| 25 | + Console.WriteLine(); |
| 26 | + // Expected output: |
| 27 | + // Label Features[0] |
| 28 | + // True 1.017325 |
| 29 | + // False 0.6326591 |
| 30 | + // False 0.0326252 |
| 31 | + // True 0.8426974 |
| 32 | + // True 0.9947656 |
| 33 | + |
| 34 | + // Now take a bootstrap sample of this dataset to create a new dataset. The bootstrap is a resampling technique that |
| 35 | + // creates a training set of the same size by picking with replacement from the original dataset. With the bootstrap, |
| 36 | + // we expect that the resampled dataset will have about 63% of the rows of the original dataset (i.e. 1-e^-1), with some |
| 37 | + // rows represented more than once. |
| 38 | + // BootstrapSample is a streaming implementation of the boostrap that enables sampling from a dataset too large to hold in memory. |
| 39 | + // To enable streaming, BootstrapSample approximates the bootstrap by sampling each row according to a Poisson(1) distribution. |
| 40 | + // Note that this streaming approximation treats each row independently, thus the resampled dataset is not guaranteed to be the |
| 41 | + // same length as the input dataset. |
| 42 | + // Let's take a look at the behavior of the BootstrapSample by examining a few draws: |
| 43 | + for (int i = 0; i < 3; i++) |
| 44 | + { |
| 45 | + var resample = mlContext.Data.BootstrapSample(data, seed: (uint) i); |
| 46 | + |
| 47 | + var enumerable = mlContext.CreateEnumerable<SamplesUtils.DatasetUtils.BinaryLabelFloatFeatureVectorSample>(resample, reuseRowObject: false); |
| 48 | + Console.WriteLine($"Label\tFeatures[0]"); |
| 49 | + foreach (var row in enumerable) |
| 50 | + { |
| 51 | + Console.WriteLine($"{row.Label}\t{row.Features[0]}"); |
| 52 | + } |
| 53 | + Console.WriteLine(); |
| 54 | + } |
| 55 | + // Expected output: |
| 56 | + // Label Features[0] |
| 57 | + // True 1.017325 |
| 58 | + // False 0.6326591 |
| 59 | + // False 0.6326591 |
| 60 | + // False 0.6326591 |
| 61 | + // False 0.0326252 |
| 62 | + // False 0.0326252 |
| 63 | + // True 0.8426974 |
| 64 | + // True 0.8426974 |
| 65 | + |
| 66 | + // Label Features[0] |
| 67 | + // True 1.017325 |
| 68 | + // True 1.017325 |
| 69 | + // False 0.6326591 |
| 70 | + // False 0.6326591 |
| 71 | + // False 0.0326252 |
| 72 | + // False 0.0326252 |
| 73 | + // False 0.0326252 |
| 74 | + // True 0.9947656 |
| 75 | + |
| 76 | + // Label Features[0] |
| 77 | + // False 0.6326591 |
| 78 | + // False 0.0326252 |
| 79 | + // True 0.8426974 |
| 80 | + // True 0.8426974 |
| 81 | + // True 0.8426974 |
| 82 | + } |
| 83 | + } |
| 84 | +} |
0 commit comments