-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Conversion of NAIndicatorTransform to estimator with related pigstensions #1217
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 16 commits
e0c85cd
9e23955
11b7dd9
e0e7d3b
593f7d5
9c1e758
5e20acd
4062bed
76a893a
8f331fd
fe94d30
122d48c
ef8c999
28e9570
f720a75
90784fb
5d4b6bc
dd49b4b
0f4507f
25c9b9e
4a6a5d3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| #@ TextLoader{ | ||
| #@ header+ | ||
| #@ sep=tab | ||
| #@ col=ScalarFloat:R4:0 | ||
| #@ col=ScalarDouble:R8:1 | ||
| #@ col=VectorFloat:R4:2-5 | ||
| #@ col=VectorDoulbe:R8:6-9 | ||
| #@ col=A:BL:10 | ||
| #@ col=B:BL:11 | ||
| #@ col=C:BL:12-15 | ||
| #@ col=D:BL:16-19 | ||
| #@ } | ||
| ScalarFloat ScalarDouble 18 8:A 9:B | ||
| 5 5 5 1 1 1 5 1 1 1 10 0:0 | ||
| 5 5 5 4 4 5 5 4 4 5 10 0:0 | ||
| 3 3 3 1 1 1 3 1 1 1 10 0:0 | ||
| 6 6 6 8 8 1 6 8 8 1 10 0:0 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| #@ TextLoader{ | ||
| #@ header+ | ||
| #@ sep=tab | ||
| #@ col=ScalarFloat:R4:0 | ||
| #@ col=ScalarDouble:R8:1 | ||
| #@ col=VectorFloat:R4:2-5 | ||
| #@ col=VectorDoulbe:R8:6-9 | ||
| #@ col=A:BL:10 | ||
| #@ col=B:BL:11 | ||
| #@ col=C:BL:12-15 | ||
| #@ col=D:BL:16-19 | ||
| #@ } | ||
| ScalarFloat ScalarDouble 18 8:A 9:B | ||
| 5 5 5 1 1 1 5 1 1 1 10 0:0 | ||
| 5 5 5 4 4 5 5 4 4 5 10 0:0 | ||
| 3 3 3 1 1 1 3 1 1 1 10 0:0 | ||
| 6 6 6 8 8 1 6 8 8 1 10 0:0 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,150 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| using Microsoft.ML.Runtime.Api; | ||
| using Microsoft.ML.Runtime.Data; | ||
| using Microsoft.ML.Runtime.Data.IO; | ||
| using Microsoft.ML.Runtime.Model; | ||
| using Microsoft.ML.Runtime.RunTests; | ||
| using Microsoft.ML.Runtime.Tools; | ||
| using Microsoft.ML.Transforms; | ||
| using System; | ||
| using System.IO; | ||
| using Xunit; | ||
| using Xunit.Abstractions; | ||
|
|
||
| namespace Microsoft.ML.Tests.Transformers | ||
| { | ||
| public class NAIndicatorTests : TestDataPipeBase | ||
| { | ||
| private class TestClass | ||
| { | ||
| public float A; | ||
| public double B; | ||
| [VectorType(2)] | ||
| public float[] C; | ||
| [VectorType(2)] | ||
| public double[] D; | ||
| } | ||
|
|
||
| public NAIndicatorTests(ITestOutputHelper output) : base(output) | ||
| { | ||
| } | ||
|
|
||
| [Fact] | ||
| public void NAIndicatorWorkout() | ||
| { | ||
| var data = new[] { | ||
| new TestClass() { A = 1, B = 3, C = new float[2]{ 1, 2 } , D = new double[2]{ 3,4} }, | ||
| new TestClass() { A = float.NaN, B = double.NaN, C = new float[2]{ float.NaN, float.NaN } , D = new double[2]{ double.NaN,double.NaN}}, | ||
| new TestClass() { A = float.NegativeInfinity, B = double.NegativeInfinity, C = new float[2]{ float.NegativeInfinity, float.NegativeInfinity } , D = new double[2]{ double.NegativeInfinity, double.NegativeInfinity}}, | ||
| new TestClass() { A = float.PositiveInfinity, B = double.PositiveInfinity, C = new float[2]{ float.PositiveInfinity, float.PositiveInfinity, } , D = new double[2]{ double.PositiveInfinity, double.PositiveInfinity}}, | ||
| new TestClass() { A = 2, B = 1, C = new float[2]{ 3, 4 } , D = new double[2]{ 5,6}}, | ||
| }; | ||
|
|
||
| var dataView = ComponentCreation.CreateDataView(Env, data); | ||
| var pipe = new NAIndicatorEstimator(Env, | ||
| new (string input, string output)[] { ("A", "NAA"), ("B", "NAB"), ("C", "NAC"), ("D", "NAD") }); | ||
| TestEstimatorCore(pipe, dataView); | ||
| Done(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void TestCommandLine() | ||
| { | ||
| Assert.Equal(Maml.Main(new[] { @"showschema loader=Text{col=A:R4:0} xf=NAIndicator{col=B:A} in=f:\2.txt" }), (int)0); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void TestOldSavingAndLoading() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add small metadata test? #Closed |
||
| { | ||
| var data = new[] { | ||
| new TestClass() { A = 1, B = 3, C = new float[2]{ 1, 2 } , D = new double[2]{ 3,4} }, | ||
| new TestClass() { A = float.NaN, B = double.NaN, C = new float[2]{ float.NaN, float.NaN } , D = new double[2]{ double.NaN,double.NaN}}, | ||
| new TestClass() { A = float.NegativeInfinity, B = double.NegativeInfinity, C = new float[2]{ float.NegativeInfinity, float.NegativeInfinity } , D = new double[2]{ double.NegativeInfinity, double.NegativeInfinity}}, | ||
| new TestClass() { A = float.PositiveInfinity, B = double.PositiveInfinity, C = new float[2]{ float.PositiveInfinity, float.PositiveInfinity, } , D = new double[2]{ double.PositiveInfinity, double.PositiveInfinity}}, | ||
| new TestClass() { A = 2, B = 1 , C = new float[2]{ 3, 4 } , D = new double[2]{ 5,6}}, | ||
| }; | ||
|
|
||
| var dataView = ComponentCreation.CreateDataView(Env, data); | ||
| var pipe = new NAIndicatorEstimator(Env, | ||
| new (string input, string output)[] { ("A", "NAA"), ("B", "NAB"), ("C", "NAC"), ("D", "NAD") }); | ||
| var result = pipe.Fit(dataView).Transform(dataView); | ||
| var resultRoles = new RoleMappedData(result); | ||
| using (var ms = new MemoryStream()) | ||
| { | ||
| TrainUtils.SaveModel(Env, Env.Start("saving"), ms, null, resultRoles); | ||
| ms.Position = 0; | ||
| var loadedView = ModelFileUtils.LoadTransforms(Env, dataView, ms); | ||
| } | ||
| } | ||
|
|
||
| [Fact] | ||
| public void NAIndicatorFileOutput() | ||
| { | ||
| string dataPath = GetDataPath("breast-cancer.txt"); | ||
| var reader = TextLoader.CreateReader(Env, ctx => ( | ||
| ScalarFloat: ctx.LoadFloat(1), | ||
| ScalarDouble: ctx.LoadDouble(1), | ||
| VectorFloat: ctx.LoadFloat(1, 4), | ||
| VectorDoulbe: ctx.LoadDouble(1, 4) | ||
| )); | ||
|
|
||
| var data = reader.Read(new MultiFileSource(dataPath)).AsDynamic; | ||
| var wrongCollection = new[] { new TestClass() { A = 1, B = 3, C = new float[2] { 1, 2 }, D = new double[2] { 3, 4 } } }; | ||
| var invalidData = ComponentCreation.CreateDataView(Env, wrongCollection); | ||
| var est = new NAIndicatorEstimator(Env, | ||
| new (string input, string output)[] { ("ScalarFloat", "A"), ("ScalarDouble", "B"), ("VectorFloat", "C"), ("VectorDoulbe", "D") }); | ||
|
|
||
| TestEstimatorCore(est, data, invalidInput: invalidData); | ||
| var outputPath = GetOutputPath("NAIndicator", "featurized.tsv"); | ||
| using (var ch = Env.Start("save")) | ||
| { | ||
| var saver = new TextSaver(Env, new TextSaver.Arguments { Silent = true }); | ||
| IDataView savedData = TakeFilter.Create(Env, est.Fit(data).Transform(data), 4); | ||
| using (var fs = File.Create(outputPath)) | ||
| DataSaverUtils.SaveDataView(ch, saver, savedData, fs, keepHidden: true); | ||
| } | ||
|
|
||
| CheckEquality("NAIndicator", "featurized.tsv"); | ||
| Done(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void NAIndicatorMetadataTest() | ||
| { | ||
| var data = new[] { | ||
| new TestClass() { A = 1, B = 3, C = new float[2]{ 1, 2 } , D = new double[2]{ 3,4} }, | ||
| new TestClass() { A = float.NaN, B = double.NaN, C = new float[2]{ float.NaN, float.NaN } , D = new double[2]{ double.NaN,double.NaN}}, | ||
| new TestClass() { A = float.NegativeInfinity, B = double.NegativeInfinity, C = new float[2]{ float.NegativeInfinity, float.NegativeInfinity } , D = new double[2]{ double.NegativeInfinity, double.NegativeInfinity}}, | ||
| new TestClass() { A = float.PositiveInfinity, B = double.PositiveInfinity, C = new float[2]{ float.PositiveInfinity, float.PositiveInfinity, } , D = new double[2]{ double.PositiveInfinity, double.PositiveInfinity}}, | ||
| new TestClass() { A = 2, B = 1, C = new float[2]{ 3, 4 } , D = new double[2]{ 5,6}}, | ||
| }; | ||
|
|
||
| var dataView = ComponentCreation.CreateDataView(Env, data); | ||
| var args = new NAIndicatorTransform.Arguments(); | ||
| args.Column = new NAIndicatorTransform.Column[] { new NAIndicatorTransform.Column() { Name = "NAA", Source = "A" } }; | ||
|
||
| var pipe = new CategoricalEstimator(Env, new CategoricalEstimator.ColumnInfo("A", "CatA")); | ||
| var newpipe = pipe.Append(new NAIndicatorEstimator(Env, new (string input, string output)[] { ("CatA", "NAA") })); | ||
| var result = newpipe.Fit(dataView).Transform(dataView); | ||
| Assert.True(result.Schema.TryGetColumnIndex("NAA", out var col)); | ||
| // Check that the column is normalized. | ||
| Assert.True(result.Schema.IsNormalized(col)); | ||
| // Check that slot names metadata was correctly created. | ||
| var value = new VBuffer<ReadOnlyMemory<char>>(); | ||
| var type = result.Schema.GetMetadataTypeOrNull(MetadataUtils.Kinds.SlotNames, col); | ||
| result.Schema.GetMetadata(MetadataUtils.Kinds.SlotNames, col, ref value); | ||
| Assert.True(value.Length == 4); | ||
| var mem = new ReadOnlyMemory<char>(); | ||
| value.GetItemOrDefault(0, ref mem); | ||
| Assert.True(mem.ToString() == "1"); | ||
| value.GetItemOrDefault(1, ref mem); | ||
| Assert.True(mem.ToString() == "-Infinity"); | ||
| value.GetItemOrDefault(2, ref mem); | ||
| Assert.True(mem.ToString() == "Infinity"); | ||
| value.GetItemOrDefault(3, ref mem); | ||
| Assert.True(mem.ToString() == "2"); | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice catch :) #Resolved