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

Checks if categories indices to drop are not None for OneHotEncoder #1028

Merged
merged 3 commits into from
Oct 3, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion skl2onnx/operator_converters/one_hot_encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,11 @@ def convert_sklearn_one_hot_encoder(
container.add_node(
"OneHotEncoder", name, ohe_output, op_domain="ai.onnx.ml", **attrs
)
if hasattr(ohe_op, "drop_idx_") and ohe_op.drop_idx_ is not None:
if (
hasattr(ohe_op, "drop_idx_")
and ohe_op.drop_idx_ is not None
and ohe_op.drop_idx_[index] is not None
):
extracted_outputs_name = scope.get_unique_variable_name("extracted_outputs")
indices_to_keep_name = scope.get_unique_variable_name("indices_to_keep")
indices_to_keep = np.delete(
Expand Down
36 changes: 34 additions & 2 deletions tests/test_sklearn_one_hot_encoder_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,12 +455,44 @@
self.assertEqual(expected.shape, res[0].shape)
assert_almost_equal(expected, res[0])

@unittest.skipIf(
not one_hot_encoder_supports_drop(),
reason="OneHotEncoder does not support drop in scikit versions < 0.21",
)
def test_one_hot_encoder_drop_if_binary(self):
data = [
["c0.4", "c0.2", 0],
["c1.4", "c1.2", 0],
["c0.2", "c2.2", 1],
["c0.2", "c2.2", 1],
["c0.2", "c2.2", 1],
["c0.2", "c2.2", 1],
]
test = [["c0.2", "c2.2", 1]]
model = OneHotEncoder(categories="auto", drop="if_binary")
model.fit(data)
inputs = [
("input1", StringTensorType([None, 2])),
("input2", Int64TensorType([None, 1])),
]
model_onnx = convert_sklearn(
model, "one-hot encoder", inputs, target_opset=TARGET_OPSET
)
self.assertTrue(model_onnx is not None)

Check notice

Code scanning / CodeQL

Imprecise assert Note

assertTrue(a is not b) cannot provide an informative message. Using assertIsNot(a, b) instead will give more informative messages.
dump_data_and_model(
test,
model,
model_onnx,
verbose=False,
basename="SklearnOneHotEncoderMixedStringIntDrop",
)


if __name__ == "__main__":
import logging

for name in ["skl2onnx", "onnx-extended"]:
for name in ["skl2onnx"]:
log = logging.getLogger(name)
log.setLevel(logging.ERROR)
TestSklearnOneHotEncoderConverter().test_model_one_hot_encoder()
TestSklearnOneHotEncoderConverter().test_one_hot_encoder_drop_if_binary()
unittest.main(verbosity=2)