Skip to content

handle boolean type in construction utils. #183

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

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 18 additions & 0 deletions src/Microsoft.ML.Api/DataViewConstructionUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,12 @@ private Delegate CreateGetter(int index)
Ch.Assert(colType.IsText);
return CreateStringToTextGetter(index);
}
else if (outputType == typeof(bool))
{
Ch.Assert(colType.IsBool);
return CreateBooleanToDvBoolGetter(index);
}

// T -> T
Ch.Assert(colType.RawType == outputType);
del = CreateDirectGetter<int>;
Expand Down Expand Up @@ -197,6 +203,18 @@ private Delegate CreateStringToTextGetter(int index)
});
}

private Delegate CreateBooleanToDvBoolGetter(int index)
{
var peek = DataView._peeks[index] as Peek<TRow, bool>;
Ch.AssertValue(peek);
bool buf = false;
return (ValueGetter<DvBool>)((ref DvBool dst) =>
{
peek(GetCurrentRowObject(), Position, ref buf);
dst = buf ? DvBool.True : DvBool.False;
Copy link
Contributor

@TomFinley TomFinley May 18, 2018

Choose a reason for hiding this comment

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

dst = buf ? DvBool.True : DvBool.False; [](start = 24, length = 39)

The shorter cast dst = (DvBool)buf; also works.

https://github.com/dotnet/machinelearning/blob/master/src/Microsoft.ML.Core/Data/DvBool.cs#L80

Copy link
Contributor

Choose a reason for hiding this comment

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

Given that DvBool is a bool with a missing value, might be nice to also handle the bool? since it's basically the same concept (just inefficient).


In reply to: 189183653 [](ancestors = 189183653)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added support for bool? but I don't think we have support for any nullable type right now, should I do same for R4, R8, DT?


In reply to: 189184119 [](ancestors = 189184119,189183653)

Copy link
Contributor

@TomFinley TomFinley May 21, 2018

Choose a reason for hiding this comment

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

Good question Ivan.... I honestly don't know. Maybe? I'd like to leave it as an open question. If it comes up, it comes up. But my suspicion is, if it ever does, it will take some years to do so. Thank you though, for addressing the immediate concern.


In reply to: 189339751 [](ancestors = 189339751,189184119,189183653)

});
}

private Delegate CreateArrayToVBufferGetter<TDst>(int index)
{
var peek = DataView._peeks[index] as Peek<TRow, TDst[]>;
Expand Down
23 changes: 23 additions & 0 deletions test/Microsoft.ML.Tests/LearningPipelineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,5 +110,28 @@ public void NoTransformPipeline()
pipeline.Add(new FastForestBinaryClassifier());
var model = pipeline.Train<Data, Prediction>();
}

public class BooleanLabelData
{
[ColumnName("Features")]
[VectorType(2)]
public float[] Features;

[ColumnName("Label")]
public bool Label;
}

[Fact]
public void BooleanLabelPipeline()
{
var data = new BooleanLabelData[1];
data[0] = new BooleanLabelData();
data[0].Features = new float[] { 0.0f, 1.0f };
data[0].Label = false;
var pipeline = new LearningPipeline();
pipeline.Add(CollectionDataSource.Create(data));
pipeline.Add(new FastForestBinaryClassifier());
var model = pipeline.Train<Data, Prediction>();
}
}
}