diff --git a/docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/Conversion/ConvertType.cs b/docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/Conversion/ConvertType.cs new file mode 100644 index 0000000000..c223450405 --- /dev/null +++ b/docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/Conversion/ConvertType.cs @@ -0,0 +1,55 @@ +using System; +using Microsoft.ML.Data; + +namespace Microsoft.ML.Samples.Dynamic +{ + public static class ConvertType + { + private sealed class InputData + { + public bool Survived; + } + + private sealed class TransformedData + { + public bool Survived { get; set; } + + public Int32 SurvivedInt32 { get; set; } + } + + public static void Example() + { + var mlContext = new MLContext(seed: 1, conc: 1); + var rawData = new[] { + new InputData() { Survived = true }, + new InputData() { Survived = false }, + new InputData() { Survived = true }, + new InputData() { Survived = false }, + new InputData() { Survived = false }, + }; + + var data = mlContext.Data.LoadFromEnumerable(rawData); + + // Construct the pipeline. + var pipeline = mlContext.Transforms.Conversion.ConvertType("SurvivedInt32", "Survived", DataKind.Int32); + + // Let's train our pipeline, and then apply it to the same data. + var transformer = pipeline.Fit(data); + var transformedData = transformer.Transform(data); + + // Display original column 'Survived' (boolean) and converted column 'SurvivedInt32' (Int32) + var convertedData = mlContext.Data.CreateEnumerable(transformedData, true); + foreach (var item in convertedData) + { + Console.WriteLine("A:{0,-10} Aconv:{1}", item.Survived, item.SurvivedInt32); + } + + // Output + // A: True Aconv:1 + // A: False Aconv:0 + // A: True Aconv:1 + // A: False Aconv:0 + // A: False Aconv:0 + } + } +} \ No newline at end of file diff --git a/src/Microsoft.ML.Data/Data/Conversion.cs b/src/Microsoft.ML.Data/Data/Conversion.cs index 2ea1717cb4..c8c847214f 100644 --- a/src/Microsoft.ML.Data/Data/Conversion.cs +++ b/src/Microsoft.ML.Data/Data/Conversion.cs @@ -1642,10 +1642,10 @@ public void Convert(in TX src, ref SB dst) #endregion FromTX #region FromBL - public void Convert(in BL src, ref I1 dst) => dst = (I1)(object)src; - public void Convert(in BL src, ref I2 dst) => dst = (I2)(object)src; - public void Convert(in BL src, ref I4 dst) => dst = (I4)(object)src; - public void Convert(in BL src, ref I8 dst) => dst = (I8)(object)src; + public void Convert(in BL src, ref I1 dst) => dst = System.Convert.ToSByte(src); + public void Convert(in BL src, ref I2 dst) => dst = System.Convert.ToInt16(src); + public void Convert(in BL src, ref I4 dst) => dst = System.Convert.ToInt32(src); + public void Convert(in BL src, ref I8 dst) => dst = System.Convert.ToInt64(src); public void Convert(in BL src, ref R4 dst) => dst = System.Convert.ToSingle(src); public void Convert(in BL src, ref R8 dst) => dst = System.Convert.ToDouble(src); public void Convert(in BL src, ref BL dst) => dst = src; diff --git a/src/Microsoft.ML.Data/Transforms/ConversionsExtensionsCatalog.cs b/src/Microsoft.ML.Data/Transforms/ConversionsExtensionsCatalog.cs index 7e960394ff..e09e5c7588 100644 --- a/src/Microsoft.ML.Data/Transforms/ConversionsExtensionsCatalog.cs +++ b/src/Microsoft.ML.Data/Transforms/ConversionsExtensionsCatalog.cs @@ -47,6 +47,12 @@ public static HashingEstimator Hash(this TransformsCatalog.ConversionTransforms /// Name of the column resulting from the transformation of . /// Name of the column to transform. If set to , the value of the will be used as source. /// The expected kind of the output column. + /// + /// + /// + /// public static TypeConvertingEstimator ConvertType(this TransformsCatalog.ConversionTransforms catalog, string outputColumnName, string inputColumnName = null, DataKind outputKind = ConvertDefaults.DefaultOutputKind) => new TypeConvertingEstimator(CatalogUtils.GetEnvironment(catalog), outputColumnName, inputColumnName, outputKind); diff --git a/test/BaselineOutput/Common/Convert/Types.tsv b/test/BaselineOutput/Common/Convert/Types.tsv index 059f4b4762..b010b12948 100644 --- a/test/BaselineOutput/Common/Convert/Types.tsv +++ b/test/BaselineOutput/Common/Convert/Types.tsv @@ -24,6 +24,7 @@ #@ col=ConvL:R4:42-43 #@ col=ConvM:R4:44-45 #@ col=ConvN:R4:46-47 +#@ col=ConvBI:I4:48-49 #@ } -a b 0 1 -1 1 0 1 0 1 -1 1 -1 1 0 1 -1 1 0 1 1 1 1 1 ? ? 0 1 -1 1 0 1 0 1 -1 1 -1 1 0 1 -1 1 0 1 1 1 1 1 -0 1 0 1 -2147483648 2147483647 0 4294967295 0 255 -128 127 -32768 32767 0 65535 -9223372036854775808 9223372036854775807 0 18446744073709551615 -3.40282347E+38 3.40282347E+38 -1.7976931348623157E+308 1.7976931348623157E+308 0 1 0 1 -2.14748365E+09 2.14748365E+09 0 4.2949673E+09 0 255 -128 127 -32768 32767 0 65535 -9.223372E+18 9.223372E+18 0 1.84467441E+19 -3.40282347E+38 3.40282347E+38 -Infinity Infinity +a b 0 1 -1 1 0 1 0 1 -1 1 -1 1 0 1 -1 1 0 1 1 1 1 1 ? ? 0 1 -1 1 0 1 0 1 -1 1 -1 1 0 1 -1 1 0 1 1 1 1 1 0 1 +0 1 1 0 -2147483648 2147483647 0 4294967295 0 255 -128 127 -32768 32767 0 65535 -9223372036854775808 9223372036854775807 0 18446744073709551615 -3.40282347E+38 3.40282347E+38 -1.7976931348623157E+308 1.7976931348623157E+308 0 1 1 0 -2.14748365E+09 2.14748365E+09 0 4.2949673E+09 0 255 -128 127 -32768 32767 0 65535 -9.223372E+18 9.223372E+18 0 1.84467441E+19 -3.40282347E+38 3.40282347E+38 -Infinity Infinity 1 0 diff --git a/test/Microsoft.ML.Tests/Transformers/ConvertTests.cs b/test/Microsoft.ML.Tests/Transformers/ConvertTests.cs index bdc01a7141..a6f07263fc 100644 --- a/test/Microsoft.ML.Tests/Transformers/ConvertTests.cs +++ b/test/Microsoft.ML.Tests/Transformers/ConvertTests.cs @@ -98,7 +98,7 @@ public void TestConvertWorkout() new TestPrimitiveClass() { AA = new []{"0", "1"}, - AB = new []{false, true}, + AB = new []{true, false}, AC = new []{ int.MinValue, int.MaxValue}, AD = new uint[]{ uint.MinValue, uint.MaxValue}, AE = new byte[]{ byte.MinValue, byte.MaxValue}, @@ -125,7 +125,9 @@ public void TestConvertWorkout() new TypeConvertingEstimator.ColumnOptions("ConvK", DataKind.Single, "AK"), new TypeConvertingEstimator.ColumnOptions("ConvL", DataKind.Single, "AL"), new TypeConvertingEstimator.ColumnOptions("ConvM", DataKind.Single, "AM"), - new TypeConvertingEstimator.ColumnOptions("ConvN", DataKind.Single, "AN")} + new TypeConvertingEstimator.ColumnOptions("ConvN", DataKind.Single, "AN"), + new TypeConvertingEstimator.ColumnOptions("ConvBI", DataKind.Int32, "AB") // verify Boolean -> Int32 conversion + } ); TestEstimatorCore(allTypesPipe, allTypesDataView);