Skip to content
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

Sample for ConvertType transform estimator #2781

Merged
merged 5 commits into from
Mar 4, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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>(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
}
}
}
8 changes: 4 additions & 4 deletions src/Microsoft.ML.Data/Data/Conversion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ public static HashingEstimator Hash(this TransformsCatalog.ConversionTransforms
/// <param name="outputColumnName">Name of the column resulting from the transformation of <paramref name="inputColumnName"/>.</param>
/// <param name="inputColumnName">Name of the column to transform. If set to <see langword="null"/>, the value of the <paramref name="outputColumnName"/> will be used as source.</param>
/// <param name="outputKind">The expected kind of the output column.</param>
/// <example>
/// <format type="text/markdown">
/// <![CDATA[
/// [!code-csharp[ConvertType](~/../docs/samples/docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/ConvertType.cs)]
/// ]]></format>
/// </example>
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);
Expand Down
5 changes: 3 additions & 2 deletions test/BaselineOutput/Common/Convert/Types.tsv
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 4 additions & 2 deletions test/Microsoft.ML.Tests/Transformers/ConvertTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public void TestConvertWorkout()
new TestPrimitiveClass()
{
AA = new []{"0", "1"},
AB = new []{false, true},
AB = new []{true, false},
Copy link
Member Author

@abgoswam abgoswam Mar 3, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

true, false} [](start = 32, length = 12)

p.s. I modified the order just to ensure that the 2nd row is different from the 1st row above. The output of the 2nd row changes accordingly from "0 1" to "1 0" (which is what i wanted to verify)

AC = new []{ int.MinValue, int.MaxValue},
AD = new uint[]{ uint.MinValue, uint.MaxValue},
AE = new byte[]{ byte.MinValue, byte.MaxValue},
Expand All @@ -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);

Expand Down