|
3 | 3 | // See the LICENSE file in the project root for more information.
|
4 | 4 |
|
5 | 5 | using Microsoft.ML;
|
| 6 | +using Microsoft.ML.Runtime.Api; |
6 | 7 | using Microsoft.ML.TestFramework;
|
| 8 | +using Microsoft.ML.Transforms; |
7 | 9 | using System.Linq;
|
8 | 10 | using Xunit;
|
9 | 11 | using Xunit.Abstractions;
|
@@ -42,5 +44,40 @@ public void CanAddAndRemoveFromPipeline()
|
42 | 44 | pipeline.Add(new Trainers.StochasticDualCoordinateAscentRegressor());
|
43 | 45 | Assert.Equal(3, pipeline.Count);
|
44 | 46 | }
|
| 47 | + |
| 48 | + private class InputData |
| 49 | + { |
| 50 | + [Column(ordinal: "1")] |
| 51 | + public string F1; |
| 52 | + } |
| 53 | + |
| 54 | + private class TransformedData |
| 55 | + { |
| 56 | +#pragma warning disable 649 |
| 57 | + [ColumnName("F1")] |
| 58 | + public float[] TransformedF1; |
| 59 | +#pragma warning restore 649 |
| 60 | + } |
| 61 | + |
| 62 | + [Fact] |
| 63 | + public void TransformOnlyPipeline() |
| 64 | + { |
| 65 | + const string _dataPath = @"..\..\Data\breast-cancer.txt"; |
| 66 | + var pipeline = new LearningPipeline(); |
| 67 | + pipeline.Add(new TextLoader<InputData>(_dataPath, useHeader: false)); |
| 68 | + pipeline.Add(new CategoricalHashOneHotVectorizer("F1") { HashBits = 10, Seed = 314489979, OutputKind = CategoricalTransformOutputKind.Bag }); |
| 69 | + var model = pipeline.Train<InputData, TransformedData>(); |
| 70 | + var predictionModel = model.Predict(new InputData() { F1 = "5" }); |
| 71 | + |
| 72 | + Assert.NotNull(predictionModel); |
| 73 | + Assert.NotNull(predictionModel.TransformedF1); |
| 74 | + Assert.Equal(1024, predictionModel.TransformedF1.Length); |
| 75 | + |
| 76 | + for (int index = 0; index < 1024; index++) |
| 77 | + if (index == 265) |
| 78 | + Assert.Equal(1, predictionModel.TransformedF1[index]); |
| 79 | + else |
| 80 | + Assert.Equal(0, predictionModel.TransformedF1[index]); |
| 81 | + } |
45 | 82 | }
|
46 | 83 | }
|
0 commit comments