v Bernoulli Naive Bayes Classifier - Machine Learning

Bernoulli Naive Bayes Classifier

The Bernoulli naive Bayes classifier assumes that all our features are binary such that they take only two values (e.g. a nominal categorical feature that has been one-hot encoded).

Preliminaries

# Load libraries
import numpy as np
from sklearn.naive_bayes import BernoulliNB

Create Binary Feature And Target Data

# Create three binary features
X = np.random.randint(2, size=(100, 3))

# Create a binary target vector
y = np.random.randint(2, size=(100, 1)).ravel()

View Feature Data

# View first ten observations
X[0:10]
array([[1, 1, 1],
       [0, 1, 0],
       [1, 1, 1],
       [0, 0, 0],
       [1, 0, 1],
       [1, 1, 1],
       [0, 1, 1],
       [1, 1, 1],
       [1, 1, 1],
       [1, 1, 0]])

Train Bernoulli Naive Bayes Classifier

# Create Bernoulli Naive Bayes object with prior probabilities of each class
clf = BernoulliNB(class_prior=[0.25, 0.5])

# Train model
model = clf.fit(X, y)