|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using Microsoft.ML; |
| 5 | +using Microsoft.ML.Data; |
| 6 | +using Microsoft.ML.Trainers.FastTree; |
| 7 | + |
| 8 | +namespace Samples.Dynamic.Transforms.TreeFeaturization |
| 9 | +{ |
| 10 | + public static class FastForestBinaryFeaturizationWithOptions |
| 11 | + { |
| 12 | + // This example requires installation of additional NuGet package |
| 13 | + // <a href="https://www.nuget.org/packages/Microsoft.ML.FastTree/">Microsoft.ML.FastTree</a>. |
| 14 | + public static void Example() |
| 15 | + { |
| 16 | + // Create a new context for ML.NET operations. It can be used for exception tracking and logging, |
| 17 | + // as a catalog of available operations and as the source of randomness. |
| 18 | + // Setting the seed to a fixed number in this example to make outputs deterministic. |
| 19 | + var mlContext = new MLContext(seed: 0); |
| 20 | + |
| 21 | + // Create a list of data points to be transformed. |
| 22 | + var dataPoints = GenerateRandomDataPoints(100).ToList(); |
| 23 | + |
| 24 | + // Convert the list of data points to an IDataView object, which is consumable by ML.NET API. |
| 25 | + var dataView = mlContext.Data.LoadFromEnumerable(dataPoints); |
| 26 | + |
| 27 | + // ML.NET doesn't cache data set by default. Therefore, if one reads a data set from a file and accesses it many times, |
| 28 | + // it can be slow due to expensive featurization and disk operations. When the considered data can fit into memory, |
| 29 | + // a solution is to cache the data in memory. Caching is especially helpful when working with iterative algorithms |
| 30 | + // which needs many data passes. |
| 31 | + dataView = mlContext.Data.Cache(dataView); |
| 32 | + |
| 33 | + // Define input and output columns of tree-based featurizer. |
| 34 | + string labelColumnName = nameof(DataPoint.Label); |
| 35 | + string featureColumnName = nameof(DataPoint.Features); |
| 36 | + string treesColumnName = nameof(TransformedDataPoint.Trees); |
| 37 | + string leavesColumnName = nameof(TransformedDataPoint.Leaves); |
| 38 | + string pathsColumnName = nameof(TransformedDataPoint.Paths); |
| 39 | + |
| 40 | + // Define the configuration of the trainer used to train a tree-based model. |
| 41 | + var trainerOptions = new FastForestBinaryTrainer.Options |
| 42 | + { |
| 43 | + // Create a simpler model by penalizing usage of new features. |
| 44 | + FeatureFirstUsePenalty = 0.1, |
| 45 | + // Reduce the number of trees to 3. |
| 46 | + NumberOfTrees = 3, |
| 47 | + // Number of leaves per tree. |
| 48 | + NumberOfLeaves = 6, |
| 49 | + // Feature column name. |
| 50 | + FeatureColumnName = featureColumnName, |
| 51 | + // Label column name. |
| 52 | + LabelColumnName = labelColumnName |
| 53 | + }; |
| 54 | + |
| 55 | + // Define the tree-based featurizer's configuration. |
| 56 | + var options = new FastForestBinaryFeaturizationEstimator.Options |
| 57 | + { |
| 58 | + InputColumnName = featureColumnName, |
| 59 | + TreesColumnName = treesColumnName, |
| 60 | + LeavesColumnName = leavesColumnName, |
| 61 | + PathsColumnName = pathsColumnName, |
| 62 | + TrainerOptions = trainerOptions |
| 63 | + }; |
| 64 | + |
| 65 | + // Define the featurizer. |
| 66 | + var pipeline = mlContext.Transforms.FeaturizeByFastForestBinary(options); |
| 67 | + |
| 68 | + // Train the model. |
| 69 | + var model = pipeline.Fit(dataView); |
| 70 | + |
| 71 | + // Apply the trained transformer to the considered data set. |
| 72 | + var transformed = model.Transform(dataView); |
| 73 | + |
| 74 | + // Convert IDataView object to a list. Each element in the resulted list corresponds to a row in the IDataView. |
| 75 | + var transformedDataPoints = mlContext.Data.CreateEnumerable<TransformedDataPoint>(transformed, false).ToList(); |
| 76 | + |
| 77 | + // Print out the transformation of the first 3 data points. |
| 78 | + for (int i = 0; i < 3; ++i) |
| 79 | + { |
| 80 | + var dataPoint = dataPoints[i]; |
| 81 | + var transformedDataPoint = transformedDataPoints[i]; |
| 82 | + Console.WriteLine($"The original feature vector [{String.Join(",", dataPoint.Features)}] is transformed to three different tree-based feature vectors:"); |
| 83 | + Console.WriteLine($" Trees' output values: [{String.Join(",", transformedDataPoint.Trees)}]."); |
| 84 | + Console.WriteLine($" Leave IDs' 0-1 representation: [{String.Join(",", transformedDataPoint.Leaves)}]."); |
| 85 | + Console.WriteLine($" Paths IDs' 0-1 representation: [{String.Join(",", transformedDataPoint.Paths)}]."); |
| 86 | + } |
| 87 | + |
| 88 | + // Expected output: |
| 89 | + // The original feature vector [0.8173254,0.7680227,0.5581612] is transformed to three different tree-based feature vectors: |
| 90 | + // Trees' output values: [0.1111111,0.8823529]. |
| 91 | + // Leave IDs' 0-1 representation: [0,0,0,0,1,0,0,0,0,1,0]. |
| 92 | + // Paths IDs' 0-1 representation: [1,1,1,1,1,1,0,1,0]. |
| 93 | + // The original feature vector [0.5888848,0.9360271,0.4721779] is transformed to three different tree-based feature vectors: |
| 94 | + // Trees' output values: [0.4545455,0.8]. |
| 95 | + // Leave IDs' 0-1 representation: [0,0,0,1,0,0,0,0,0,0,1]. |
| 96 | + // Paths IDs' 0-1 representation: [1,1,1,1,0,1,0,1,1]. |
| 97 | + // The original feature vector [0.2737045,0.2919063,0.4673147] is transformed to three different tree-based feature vectors: |
| 98 | + // Trees' output values: [0.4545455,0.1111111]. |
| 99 | + // Leave IDs' 0-1 representation: [0,0,0,1,0,0,1,0,0,0,0]. |
| 100 | + // Paths IDs' 0-1 representation: [1,1,1,1,0,1,0,1,1]. |
| 101 | + } |
| 102 | + |
| 103 | + private static IEnumerable<DataPoint> GenerateRandomDataPoints(int count, int seed=0) |
| 104 | + { |
| 105 | + var random = new Random(seed); |
| 106 | + float randomFloat() => (float)random.NextDouble(); |
| 107 | + for (int i = 0; i < count; i++) |
| 108 | + { |
| 109 | + var label = randomFloat() > 0.5f; |
| 110 | + yield return new DataPoint |
| 111 | + { |
| 112 | + Label = label, |
| 113 | + // Create random features that are correlated with the label. |
| 114 | + // For data points with false label, the feature values are slightly increased by adding a constant. |
| 115 | + Features = Enumerable.Repeat(label, 3).Select(x => x ? randomFloat() : randomFloat() + 0.03f).ToArray() |
| 116 | + }; |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + // Example with label and 3 feature values. A data set is a collection of such examples. |
| 121 | + private class DataPoint |
| 122 | + { |
| 123 | + public bool Label { get; set; } |
| 124 | + [VectorType(3)] |
| 125 | + public float[] Features { get; set; } |
| 126 | + } |
| 127 | + |
| 128 | + // Class used to capture the output of tree-base featurization. |
| 129 | + private class TransformedDataPoint : DataPoint |
| 130 | + { |
| 131 | + // The i-th value is the output value of the i-th decision tree. |
| 132 | + public float[] Trees { get; set; } |
| 133 | + // The 0-1 encoding of leaves the input feature vector falls into. |
| 134 | + public float[] Leaves { get; set; } |
| 135 | + // The 0-1 encoding of paths the input feature vector reaches the leaves. |
| 136 | + public float[] Paths { get; set; } |
| 137 | + } |
| 138 | + } |
| 139 | +} |
0 commit comments