# BernoulliNB

### *class* cuml.naive_bayes.BernoulliNB(, alpha=1.0, binarize=0.0, fit_prior=True, class_prior=None, output_type=None, verbose=False)[[source]](https://github.com/NVIDIA/cuml/blob/93418baf5/python/cuml/cuml/naive_bayes/naive_bayes.py#L911)

Naive Bayes classifier for multivariate Bernoulli models.

Like MultinomialNB, this classifier is suitable for discrete data. The
difference is that while MultinomialNB works with occurrence counts,
BernoulliNB is designed for binary/boolean features.

* **Parameters:**
  **alpha**
  : Additive (Laplace/Lidstone) smoothing parameter
    (0 for no smoothing).

  **binarize**
  : Threshold for binarizing (mapping to booleans) of sample features.
    If None, input is presumed to already consist of binary vectors.

  **fit_prior**
  : Whether to learn class prior probabilities or not.
    If false, a uniform prior will be used.

  **class_prior**
  : Prior probabilities of the classes. If specified the priors are not
    adjusted according to the data.

  **output_type**
  : Return results and set estimator attributes to the indicated output
    type. If None, the output type set at the module level
    (`cuml.global_settings.output_type`) will be used. See
    [Output Data Type Configuration](../../cuml/index.html.md#output-data-type-configuration) for more info.

  **verbose**
  : Sets logging level. It must be one of `cuml.common.logger.level_*`.
    See [Verbosity Levels](../../cuml/index.html.md#verbosity-levels) for more info.
* **Attributes:**
  **class_count_**
  : Number of samples encountered for each class during fitting.

  **class_log_prior_**
  : Log probability of each class (smoothed).

  **classes_**
  : Class labels known to the classifier

  **feature_count_**
  : Number of samples encountered for each (class, feature)
    during fitting.

  **feature_log_prob_**
  : Empirical log probability of features given a class, P(x_i|y).

  **n_features_**
  : Number of features of each sample.

### References

C.D. Manning, P. Raghavan and H. Schuetze (2008). Introduction to
Information Retrieval. Cambridge University Press, pp. 234-265.
[https://nlp.stanford.edu/IR-book/html/htmledition/the-bernoulli-model-1.html](https://nlp.stanford.edu/IR-book/html/htmledition/the-bernoulli-model-1.html)
A. McCallum and K. Nigam (1998). A comparison of event models for naive
Bayes text classification. Proc. AAAI/ICML-98 Workshop on Learning for
Text Categorization, pp. 41-48.
V. Metsis, I. Androutsopoulos and G. Paliouras (2006). Spam filtering with
naive Bayes – Which naive Bayes? 3rd Conf. on Email and Anti-Spam (CEAS).

### Examples

```pycon
>>> import cupy as cp
>>> from cuml.naive_bayes import BernoulliNB
>>> rng = cp.random.RandomState(1)
>>> X = rng.randint(5, size=(6, 100), dtype=cp.int32)
>>> y = cp.array([1, 2, 3, 4, 4, 5])
>>> clf = BernoulliNB()
>>> clf.fit(X, y)
BernoulliNB()
>>> print(clf.predict(X[2:3]))
[3]
```

<!-- !! processed by numpydoc !! -->

#### fit(X, y) → \_BaseDiscreteNB[[source]](https://github.com/NVIDIA/cuml/blob/93418baf5/python/cuml/cuml/naive_bayes/naive_bayes.py#L699)

Fit Naive Bayes classifier according to X, y

* **Parameters:**
  **X**
  : Training vectors, where n_samples is the number of samples and
    n_features is the number of features.

  **y**
  : Target values.

<!-- !! processed by numpydoc !! -->

#### get_params(deep=True)[[source]](https://github.com/NVIDIA/cuml/blob/93418baf5/python/cuml/cuml/internals/base.py#L191)

Returns a dict of all params owned by this class. If the child class
has appropriately overridden the `_get_param_names` method and does not
need anything other than what is there in this method, then it doesn’t
have to override this method

<!-- !! processed by numpydoc !! -->

#### partial_fit(X, y, classes=None) → \_BaseDiscreteNB[[source]](https://github.com/NVIDIA/cuml/blob/93418baf5/python/cuml/cuml/naive_bayes/naive_bayes.py#L618)

Incremental fit on a batch of samples.

This method is expected to be called several times consecutively on
different chunks of a dataset so as to implement out-of-core or online
learning.

This is especially useful when the whole dataset is too big to fit in
memory at once.

This method has some performance overhead hence it is better to call
partial_fit on chunks of data that are as large as possible (as long
as fitting in the memory budget) to hide the overhead.

* **Parameters:**
  **X**
  : Training vectors, where n_samples is the number of samples and
    n_features is the number of features

  **y**
  : Target values.

  **classes**
  : List of all the classes that can possibly appear in the y vector.
    Must be provided at the first call to partial_fit, can be omitted
    in subsequent calls.
* **Returns:**
  **self**

<!-- !! processed by numpydoc !! -->

#### predict(X)[[source]](https://github.com/NVIDIA/cuml/blob/93418baf5/python/cuml/cuml/naive_bayes/naive_bayes.py#L144)

Perform classification on an array of test vectors X.

* **Parameters:**
  **X**
  : Dense or sparse matrix with dtype float32 or float64.
    Acceptable dense formats: CUDA array interface compliant objects like
    CuPy, cuDF DataFrame/Series, NumPy ndarray and Pandas
    DataFrame/Series.
* **Returns:**
  **y_hat**
  : Predicted values
    <br/>
    For more information on how to configure cuML’s output type,
    refer to: [Output Data Type Configuration](../../cuml/index.html.md#output-data-type-configuration).

<!-- !! processed by numpydoc !! -->

#### predict_log_proba(X)[[source]](https://github.com/NVIDIA/cuml/blob/93418baf5/python/cuml/cuml/naive_bayes/naive_bayes.py#L166)

Return log-probability estimates for the test vector X.

* **Parameters:**
  **X**
  : Dense or sparse matrix with dtype float32 or float64.
    Acceptable dense formats: CUDA array interface compliant objects like
    CuPy, cuDF DataFrame/Series, NumPy ndarray and Pandas
    DataFrame/Series.
* **Returns:**
  **C**
  : Returns the log-probability of the samples for each class in the model. The columns correspond to the classes in sorted order, as they appear in the attribute `classes_`.
    <br/>
    For more information on how to configure cuML’s output type,
    refer to: [Output Data Type Configuration](../../cuml/index.html.md#output-data-type-configuration).

<!-- !! processed by numpydoc !! -->

#### predict_proba(X)[[source]](https://github.com/NVIDIA/cuml/blob/93418baf5/python/cuml/cuml/naive_bayes/naive_bayes.py#L200)

Return probability estimates for the test vector X.

* **Parameters:**
  **X**
  : Dense or sparse matrix with dtype float32 or float64.
    Acceptable dense formats: CUDA array interface compliant objects like
    CuPy, cuDF DataFrame/Series, NumPy ndarray and Pandas
    DataFrame/Series.
* **Returns:**
  **C**
  : Returns the probability of the samples for each class in the model. The columns correspond to the classes in sorted order, as they appear in the attribute `classes_`.
    <br/>
    For more information on how to configure cuML’s output type,
    refer to: [Output Data Type Configuration](../../cuml/index.html.md#output-data-type-configuration).

<!-- !! processed by numpydoc !! -->

#### score(X, y, sample_weight=None, \*\*kwargs)[[source]](https://github.com/NVIDIA/cuml/blob/93418baf5/python/cuml/cuml/internals/mixins.py#L101)

Scoring function for classifier estimators based on mean accuracy.

* **Parameters:**
  **X**
  : Dense matrix with dtype float32 or float64.
    Acceptable formats: CUDA array interface compliant objects like
    CuPy, cuDF DataFrame/Series, NumPy ndarray and Pandas
    DataFrame/Series.

  **y**
  : Dense matrix with dtype float32 or float64.
    Acceptable formats: CUDA array interface compliant objects like
    CuPy, cuDF DataFrame/Series, NumPy ndarray and Pandas
    DataFrame/Series.

  **sample_weight**
  : The weights for each observation in X. If None, all observations
    are assigned equal weight.
    Acceptable formats: CUDA array interface compliant objects like
    CuPy, cuDF DataFrame/Series, NumPy ndarray and Pandas
    DataFrame/Series.
* **Returns:**
  **score**
  : Accuracy of self.predict(X) wrt. y (fraction where y == pred_y)

<!-- !! processed by numpydoc !! -->

#### set_params(\*\*params)[[source]](https://github.com/NVIDIA/cuml/blob/93418baf5/python/cuml/cuml/internals/base.py#L200)

Accepts a dict of params and updates the corresponding ones owned by
this class. If the child class has appropriately overridden the
`_get_param_names` method and does not need anything other than what is,
there in this method, then it doesn’t have to override this method

<!-- !! processed by numpydoc !! -->

#### update_log_probs()[[source]](https://github.com/NVIDIA/cuml/blob/93418baf5/python/cuml/cuml/naive_bayes/naive_bayes.py#L722)

Updates the log probabilities. This enables lazy update for
applications like distributed Naive Bayes, so that the model
can be updated incrementally without incurring this cost each
time.

<!-- !! processed by numpydoc !! -->
