Skip to content
Prev Previous commit
Next Next commit
Accept any Real zero_division value equal to 0 or 1
sklearn validates zero_division against numbers.Real, so numpy scalar
integers and floats such as np.int64(0) and np.float32(1.0) are valid
inputs. The isinstance((int, float)) check rejected them. Extend the
check to numbers.Real and parametrize the literal test with numpy
scalars.
  • Loading branch information
VaggelisGian committed Aug 27, 2026
commit 2b3881d1d7045968e2969664ace3657f2f221edd
3 changes: 2 additions & 1 deletion python/cuml/cuml/metrics/_classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# SPDX-FileCopyrightText: Copyright (c) 2020-2026, NVIDIA CORPORATION.
# SPDX-License-Identifier: Apache-2.0
#
import numbers
import warnings

import cudf
Expand Down Expand Up @@ -217,7 +218,7 @@ def precision_score(

if isinstance(zero_division, str) and zero_division == "warn":
zero_division_value = 0.0
elif isinstance(zero_division, (int, float)) and zero_division in (0, 1):
elif isinstance(zero_division, numbers.Real) and zero_division in (0, 1):
zero_division_value = float(zero_division)
else:
raise ValueError(
Expand Down
4 changes: 3 additions & 1 deletion python/cuml/tests/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,9 @@ def test_precision_score_zero_division_warn():
np.testing.assert_allclose(res, sol)


@pytest.mark.parametrize("zero_division", [0, 1])
@pytest.mark.parametrize(
"zero_division", [0, 1, np.int64(0), np.int64(1), np.float32(1.0)]
)
def test_precision_score_zero_division_literal(zero_division):
# classes 1 and 2 are never predicted
y_true = np.array([0, 1, 2, 0, 1, 2])
Expand Down
Loading