|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using Microsoft.ML; |
| 4 | +using Microsoft.ML.Data; |
| 5 | + |
| 6 | +namespace Samples.Dynamic |
| 7 | +{ |
| 8 | + class MapKeyToVector |
| 9 | + { |
| 10 | + /// This example demonstrates the use of MapKeyToVector by mapping keys to floats[]. |
| 11 | + /// Because the ML.NET KeyType maps the missing value to zero, counting starts at 1, so the uint values |
| 12 | + /// converted to KeyTypes will appear skewed by one. |
| 13 | + /// See https://github.com/dotnet/machinelearning/blob/master/docs/code/IDataViewTypeSystem.md#key-types |
| 14 | + public static void Example() |
| 15 | + { |
| 16 | + // Create a new ML context, for ML.NET operations. It can be used for exception tracking and logging, |
| 17 | + // as well as the source of randomness. |
| 18 | + var mlContext = new MLContext(); |
| 19 | + |
| 20 | + // Get a small dataset as an IEnumerable. |
| 21 | + var rawData = new[] { |
| 22 | + new DataPoint() { Timeframe = 9, Category = 5 }, |
| 23 | + new DataPoint() { Timeframe = 8, Category = 4 }, |
| 24 | + new DataPoint() { Timeframe = 8, Category = 4 }, |
| 25 | + new DataPoint() { Timeframe = 9, Category = 3 }, |
| 26 | + new DataPoint() { Timeframe = 2, Category = 3 }, |
| 27 | + new DataPoint() { Timeframe = 3, Category = 5 } |
| 28 | + }; |
| 29 | + |
| 30 | + var data = mlContext.Data.LoadFromEnumerable(rawData); |
| 31 | + |
| 32 | + // Constructs the ML.net pipeline |
| 33 | + var pipeline = mlContext.Transforms.Conversion.MapKeyToVector("TimeframeVector", "Timeframe") |
| 34 | + .Append(mlContext.Transforms.Conversion.MapKeyToVector("CategoryVector", "Category", outputCountVector: true)); |
| 35 | + |
| 36 | + // Fits the pipeline to the data. |
| 37 | + IDataView transformedData = pipeline.Fit(data).Transform(data); |
| 38 | + |
| 39 | + // Getting the resulting data as an IEnumerable. |
| 40 | + // This will contain the newly created columns. |
| 41 | + IEnumerable<TransformedData> features = mlContext.Data.CreateEnumerable<TransformedData>(transformedData, reuseRowObject: false); |
| 42 | + |
| 43 | + Console.WriteLine($" Timeframe TimeframeVector Category CategoryVector"); |
| 44 | + foreach (var featureRow in features) |
| 45 | + Console.WriteLine($"{featureRow.Timeframe}\t\t\t{string.Join(',', featureRow.TimeframeVector)}\t\t\t{featureRow.Category}\t\t{string.Join(',', featureRow.CategoryVector)}"); |
| 46 | + |
| 47 | + // TransformedData obtained post-transformation. |
| 48 | + // |
| 49 | + // Timeframe TimeframeVector Category CategoryVector |
| 50 | + // 10 0,0,0,0,0,0,0,0,0,1 6 0,0,0,0,0 |
| 51 | + // 9 0,0,0,0,0,0,0,0,1,0 5 0,0,0,0,1 |
| 52 | + // 9 0,0,0,0,0,0,0,0,1,0 5 0,0,0,0,1 |
| 53 | + // 10 0,0,0,0,0,0,0,0,0,1 4 0,0,0,1,0 |
| 54 | + // 3 0,0,1,0,0,0,0,0,0,0 4 0,0,0,1,0 |
| 55 | + // 4 0,0,0,1,0,0,0,0,0,0 6 0,0,0,0,0 |
| 56 | + } |
| 57 | + |
| 58 | + private class DataPoint |
| 59 | + { |
| 60 | + [KeyType(10)] |
| 61 | + public uint Timeframe { get; set; } |
| 62 | + |
| 63 | + [KeyType(6)] |
| 64 | + public uint Category { get; set; } |
| 65 | + |
| 66 | + } |
| 67 | + |
| 68 | + private class TransformedData : DataPoint |
| 69 | + { |
| 70 | + public float[] TimeframeVector { get; set; } |
| 71 | + public float[] CategoryVector { get; set; } |
| 72 | + } |
| 73 | + } |
| 74 | +} |
0 commit comments