Skip to content

[BUG] ComplementNB fails with an ambiguous truth-value error for per-feature alpha #8642

Description

@apiqwe

Describe the bug

cuml.naive_bayes.ComplementNB accepts a NumPy array as its alpha constructor argument but fails during fit() with an internal scalar comparison:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

The equivalent scikit-learn estimator supports an alpha array of shape (n_features,), fits successfully, and predicts [1].

This is a scikit-learn compatibility gap, and the current failure does not clearly report that cuML expects a scalar smoothing value.

Steps/Code to reproduce bug

cuML reproducer:

import numpy as np
from cuml.naive_bayes import ComplementNB

X = np.array([[1, 2], [3, 4], [5, 6]])
y = np.array([0, 1, 0])

alpha = np.array([1.0, 2.0])

print(ComplementNB(alpha=alpha).fit(X, y).predict([[1, 2]]))

Output:

Traceback (most recent call last):
  File "/workspace/apibughub/cuml/ComplementNB/error_bug1/error_bug_cuml.py", line 9, in <module>
    print(ComplementNB(alpha=alpha).fit(X, y).predict([[1, 2]]))
          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^
  File "/opt/conda/envs/rapids-26.08/lib/python3.14/site-packages/cuml/internals/outputs.py", line 874, in inner
    res = func(*args, **kwargs)
  File "/opt/conda/envs/rapids-26.08/lib/python3.14/site-packages/cuml/naive_bayes/naive_bayes.py", line 702, in fit
    return self._partial_fit(X, y, reset=True)
           ~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^
  File "/opt/conda/envs/rapids-26.08/lib/python3.14/site-packages/cuml/naive_bayes/naive_bayes.py", line 661, in _partial_fit
    if self.alpha < 0:
       ^^^^^^^^^^^^^^
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

For comparison, the equivalent scikit-learn code:

import numpy as np
from sklearn.naive_bayes import ComplementNB

X = np.array([[1, 2], [3, 4], [5, 6]])
y = np.array([0, 1, 0])

alpha = np.array([1.0, 2.0])

print(ComplementNB(alpha=alpha).fit(X, y).predict([[1, 2]]))

Output:

[1]

Expected behavior

Preferably, cuml.naive_bayes.ComplementNB should support a per-feature smoothing array with shape (n_features,), matching scikit-learn. In this reproducer, alpha=np.array([1.0, 2.0]) matches the two input features, so fitting should complete and prediction should return:

[1]

If array-valued alpha is intentionally unsupported, cuML should validate the argument and raise a clear TypeError or ValueError stating that alpha must be a scalar. It should not evaluate an array in a scalar Boolean context and expose NumPy's ambiguous truth-value error.

Environment details (please complete the following information):

  • Environment location: Docker
  • Linux Distro/Architecture: Ubuntu 24.04 / x86_64
  • GPU Model/Driver: NVIDIA GeForce RTX 4090 / 595.71.05
  • CUDA: 13.2
  • Method of cuDF & cuML install: conda

conda list:

conda list
# packages in environment at /opt/conda/envs/rapids-26.08:
#
# Name                              Version          Build                                         Channel
# Name              Version       Build                                      Channel
python              3.14.6        h242f9ac_102_cp314                         conda-forge
numpy               2.4.6         py314h2b28147_0                            conda-forge
scipy               1.16.3        py314hf07bd8e_2                            conda-forge
scikit-learn        1.9.0         np2py314hf09ca88_0                         conda-forge
rapids              26.08.00      cuda13_260806_c2656556                     rapidsai
cuml                26.08.00      cuda13_cp311_abi3_260805_265b9da6          rapidsai
libcuml             26.08.00      cuda13_260805_265b9da6                     rapidsai
cudf                26.08.00      cuda13_cp311_abi3_260805_ff5b362d          rapidsai
libraft             26.08.00      cuda13_260805_ebf92684                     rapidsai
libraft-headers     26.08.00      cuda13_260805_ebf92684                     rapidsai
pylibraft           26.08.00      cuda13_cp311_abi3_260805_ebf92684          rapidsai
cuvs                26.08.01      cuda13_cp311_abi3_260806_25b1be43          rapidsai
libcuvs             26.08.01      cuda13_260806_25b1be43                     rapidsai
cupy                14.1.1        py314hdea9c46_0                            conda-forge
cupy-core           14.1.1        py314hcd3b49b_0                            conda-forge
numba               0.64.0        py314h8169c2f_0                            conda-forge
numba-cuda          0.30.4        py314h42812f9_0                            conda-forge
rmm                 26.08.00      cuda13_cp311_abi3_260805_42d059f1          rapidsai
librmm              26.08.00      cuda13_260805_42d059f1                     rapidsai
cuda-version        13.3           hcbadf70_3                                 conda-forge
cuda-bindings       13.3.1        py314h42812f9_1                            conda-forge
cuda-cudart         13.3.29       hecca717_0                                 conda-forge
cuda-nvrtc          13.3.33       hecca717_0                                 conda-forge
libcublas           13.6.0.2      h676940d_0                                 conda-forge
libcusolver         12.2.6.9      h676940d_0                                 conda-forge
libcusparse         12.8.2.51     hecca717_0                                 conda-forge
libcurand           10.4.3.29     h676940d_0                                 conda-forge

Additional context

The failure comes from this scalar-only validation in ComplementNB._partial_fit():

if self.alpha < 0:

For an array-valued alpha, the comparison produces a Boolean array, which cannot be used directly as an if condition.

The input has two features and the smoothing array has exactly two elements:

X.shape = (3, 2)
alpha.shape = (2,)

All smoothing values are finite and strictly positive, so a per-element non-negativity check would pass.

The scikit-learn ComplementNB documentation defines alpha as a float or an array-like of shape (n_features,). The cuML ComplementNB documentation currently documents only a scalar float. The report therefore requests either compatible per-feature smoothing support or explicit validation of the narrower cuML contract.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions