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

Fix label binarize for binary class #5900

Merged
merged 7 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
9 changes: 7 additions & 2 deletions python/cuml/preprocessing/label.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2020-2023, NVIDIA CORPORATION.
# Copyright (c) 2020-2024, NVIDIA CORPORATION.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -64,14 +64,19 @@ def label_binarize(

cp.cuda.Stream.null.synchronize()

is_binary = classes.shape[0] == 2

if sparse_output:
sp = sp.tocsr()
if is_binary:
sp = sp.getcol(1) # getcol does not support -1 indexing
return sp
else:

arr = sp.toarray().astype(y.dtype)
arr[arr == 0] = neg_label

if is_binary:
arr = arr[:, -1].reshape((-1, 1))
return arr


Expand Down
34 changes: 33 additions & 1 deletion python/cuml/tests/test_preprocessing.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2020-2023, NVIDIA CORPORATION.
# Copyright (c) 2020-2024, NVIDIA CORPORATION.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -43,6 +43,7 @@
quantile_transform as cu_quantile_transform,
robust_scale as cu_robust_scale,
scale as cu_scale,
label_binarize as cu_label_binarize,
)
from sklearn.preprocessing import (
Binarizer as skBinarizer,
Expand All @@ -68,6 +69,7 @@
quantile_transform as sk_quantile_transform,
robust_scale as sk_robust_scale,
scale as sk_scale,
label_binarize as sk_label_binarize,
)
from sklearn.impute import (
MissingIndicator as skMissingIndicator,
Expand Down Expand Up @@ -1135,6 +1137,36 @@ def test_kernel_centerer():
assert_allclose(sk_t_X, t_X)


def test_label_binarize():
cu_bin = cu_label_binarize(
cp.array([1, 0, 1, 1]), classes=cp.array([0, 1])
)
sk_bin = sk_label_binarize([1, 0, 1, 1], classes=[0, 1])
assert_allclose(cu_bin, sk_bin)

cu_bin_sparse = cu_label_binarize(
cp.array([1, 0, 1, 1]), classes=cp.array([0, 1]), sparse_output=True
)
sk_bin_sparse = sk_label_binarize(
[1, 0, 1, 1], classes=[0, 1], sparse_output=True
)
assert_allclose(cu_bin_sparse, sk_bin_sparse)

cu_multi = cu_label_binarize(
cp.array([1, 6, 3]), classes=cp.array([1, 3, 4, 6])
)
sk_multi = sk_label_binarize([1, 6, 3], classes=[1, 3, 4, 6])
assert_allclose(cu_multi, sk_multi)

cu_multi_sparse = cu_label_binarize(
cp.array([1, 6, 3]), classes=cp.array([1, 3, 4, 6]), sparse_output=True
)
sk_multi_sparse = sk_label_binarize(
[1, 6, 3], classes=[1, 3, 4, 6], sparse_output=True
)
assert_allclose(cu_multi_sparse, sk_multi_sparse)


def test__repr__():
assert cuBinarizer().__repr__() == "Binarizer()"
assert cuFunctionTransformer().__repr__() == "FunctionTransformer()"
Expand Down
Loading