class_docstr: Bernoulli Naive Bayes.
  
  Bernoulli Naive Bayes model learns from occurrences between features such as word
  counts and discrete classes. The input vector must contain positive values, such as
  counts or TF-IDF values.
  
  Parameters
  ----------
  alpha
      Additive (Laplace/Lidstone) smoothing parameter (use 0 for no smoothing).
  true_threshold
      Threshold for binarizing (mapping to booleans) features.
  
  Attributes
  ----------
  class_counts : collections.Counter
      Number of times each class has been seen.
  feature_counts : collections.defaultdict
      Total frequencies per feature and class.
  
  Examples
  --------
  
  >>> import pandas as pd
  >>> from river import compose
  >>> from river import feature_extraction
  >>> from river import naive_bayes
  
  >>> docs = [
  ...     ("Chinese Beijing Chinese", "yes"),
  ...     ("Chinese Chinese Shanghai", "yes"),
  ...     ("Chinese Macao", "yes"),
  ...     ("Tokyo Japan Chinese", "no")
  ... ]
  
  >>> model = compose.Pipeline(
  ...     ("tokenize", feature_extraction.BagOfWords(lowercase=False)),
  ...     ("nb", naive_bayes.BernoulliNB(alpha=1))
  ... )
  
  >>> for sentence, label in docs:
  ...     model.learn_one(sentence, label)
  
  >>> model["nb"].p_class("yes")
  0.75
  >>> model["nb"].p_class("no")
  0.25
  
  >>> model.predict_proba_one("test")
  {'yes': 0.883..., 'no': 0.116...}
  
  >>> model.predict_one("test")
  'yes'
  
  You can train the model and make predictions in mini-batch mode using the class methods
  `learn_many` and `predict_many`.
  
  >>> df_docs = pd.DataFrame(docs, columns = ["docs", "y"])
  
  >>> X = pd.Series([
  ...    "Chinese Beijing Chinese",
  ...    "Chinese Chinese Shanghai",
  ...    "Chinese Macao",
  ...    "Tokyo Japan Chinese"
  ... ])
  
  >>> y = pd.Series(["yes", "yes", "yes", "no"])
  
  >>> model = compose.Pipeline(
  ...     ("tokenize", feature_extraction.BagOfWords(lowercase=False)),
  ...     ("nb", naive_bayes.BernoulliNB(alpha=1))
  ... )
  
  >>> model.learn_many(X, y)
  
  >>> unseen = pd.Series(["Taiwanese Taipei", "Chinese Shanghai"])
  
  >>> model.predict_proba_many(unseen)
           no       yes
  0  0.116846  0.883154
  1  0.047269  0.952731
  
  >>> model.predict_many(unseen)
  0    yes
  1    yes
  dtype: object
  
  References
  ----------
  [^1]: [The Bernoulli model](https://nlp.stanford.edu/IR-book/html/htmledition/the-bernoulli-model-1.html)
class_docstr_tok: ["Bernoulli","Naive","Bayes",".","Bernoulli","Naive","Bayes","model","learns","from","occurrences","between","features","such","as","word","counts","and","discrete","classes",".","The","input","vector","must","contain","positive","values",",","such","as","counts","or","TF-IDF","values",".","Parameters","--","--","--","--","--","alpha","Additive","(","Laplace\/Lidstone",")","smoothing","parameter","(","use","0","for","no","smoothing",")",".","true_threshold","Threshold","for","binarizing","(","mapping","to","booleans",")","features",".","Attributes","--","--","--","--","--","class_counts",":","collections.Counter","Number","of","times","each","class","has","been","seen",".","feature_counts",":","collections.defaultdict","Total","frequencies","per","feature","and","class",".","Examples","--","--","--","--",">",">",">","import","pandas","as","pd",">",">",">","from","river","import","compose",">",">",">","from","river","import","feature_extraction",">",">",">","from","river","import","naive_bayes",">",">",">","docs","=","[","...","(","``","Chinese","Beijing","Chinese","''",",","``","yes","''",")",",","...","(","``","Chinese","Chinese","Shanghai","''",",","``","yes","''",")",",","...","(","``","Chinese","Macao","''",",","``","yes","''",")",",","...","(","``","Tokyo","Japan","Chinese","''",",","``","no","''",")","...","]",">",">",">","model","=","compose.Pipeline","(","...","(","``","tokenize","''",",","feature_extraction.BagOfWords","(","lowercase=False",")",")",",","...","(","``","nb","''",",","naive_bayes.BernoulliNB","(","alpha=1",")",")","...",")",">",">",">","for","sentence",",","label","in","docs",":","...","model.learn_one","(","sentence",",","label",")",">",">",">","model","[","``","nb","''","]",".p_class","(","``","yes","''",")","0.75",">",">",">","model","[","``","nb","''","]",".p_class","(","``","no","''",")","0.25",">",">",">","model.predict_proba_one","(","``","test","''",")","{","'yes","'",":","0.883","...",",","'no","'",":","0.116","...","}",">",">",">","model.predict_one","(","``","test","''",")","'yes'","You","can","train","the","model","and","make","predictions","in","mini-batch","mode","using","the","class","methods","`","learn_many","`","and","`","predict_many","`",".",">",">",">","df_docs","=","pd.DataFrame","(","docs",",","columns","=","[","``","docs","''",",","``","y","''","]",")",">",">",">","X","=","pd.Series","(","[","...","``","Chinese","Beijing","Chinese","''",",","...","``","Chinese","Chinese","Shanghai","''",",","...","``","Chinese","Macao","''",",","...","``","Tokyo","Japan","Chinese","''","...","]",")",">",">",">","y","=","pd.Series","(","[","``","yes","''",",","``","yes","''",",","``","yes","''",",","``","no","''","]",")",">",">",">","model","=","compose.Pipeline","(","...","(","``","tokenize","''",",","feature_extraction.BagOfWords","(","lowercase=False",")",")",",","...","(","``","nb","''",",","naive_bayes.BernoulliNB","(","alpha=1",")",")","...",")",">",">",">","model.learn_many","(","X",",","y",")",">",">",">","unseen","=","pd.Series","(","[","``","Taiwanese","Taipei","''",",","``","Chinese","Shanghai","''","]",")",">",">",">","model.predict_proba_many","(","unseen",")","no","yes","0","0.116846","0.883154","1","0.047269","0.952731",">",">",">","model.predict_many","(","unseen",")","0","yes","1","yes","dtype",":","object","References","--","--","--","--","--","[","^1","]",":","[","The","Bernoulli","model","]","(","https",":","\/\/nlp.stanford.edu\/IR-book\/html\/htmledition\/the-bernoulli-model-1.html",")"]
docstr: Learn from a batch of count vectors.
  
  Parameters
  ----------
  X
      Count vectors.
  y
      Target classes.
docstr_tok: ["Learn","from","a","batch","of","count","vectors",".","Parameters","--","--","--","--","--","X","Count","vectors",".","y","Target","classes","."]
code: def learn_many(self, X: pd.DataFrame, y: pd.Series):
      """Learn from a batch of count vectors.
  
      Parameters
      ----------
      X
          Count vectors.
      y
          Target classes.
  
      """
      # One hot encode y and convert it into sparse matrix
      y = base.one_hot_encode(y)
      columns, classes = X.columns, y.columns
      y = sparse.csc_matrix(y.sparse.to_coo()).T
  
      self.class_counts.update(
          {c: count.item() for c, count in zip(classes, y.sum(axis=1))}
      )
  
      if hasattr(X, "sparse"):
          X = sparse.csr_matrix(X.sparse.to_coo())
          X.data = X.data > self.true_threshold
      else:
          X = X > self.true_threshold
  
      fc = y @ X
  
      # Update the defaultdict feature counts by slicing the sparse matrix per row.
      # Each row of y @ X correspond to a class and each column to a token.
      for c, i in zip(classes, range(fc.shape[0])):
          if sparse.issparse(fc):
              counts = {
                  c: {
                      columns[f]: count
                      for f, count in zip(fc[i].indices, fc[i].data)
                  }
              }
  
          else:
              counts = {
                  c: {f: count for f, count in zip(columns, fc[i])}
              }
  
          # Transform {classe_i: {token_1: f_1, ... token_n: f_n}} into:
          # [{token_1: {classe_i: f_1}},.. {token_n: {class_i: f_n}}]
          for dict_count in [
              {token: {c: f} for token, f in frequencies.items()}
              for c, frequencies in counts.items()
          ]:
              for f, count in dict_count.items():
                  self.feature_counts[f].update(count)
  
file_path: river/river/naive_bayes/bernoulli.py
imports: from __future__ import annotations
  import collections
  import math
  import numpy
  import pandas
  from scipy import sparse
  from .None import base
label_desc: Use image node_id 7 for calling the BernoulliNB obj's underlying member method code with example usage: obj.learn_many(X, y) without return types