How to Train a Naive Bayes Classifier in Sklearn

03.13.2021

Intro

The Naive Bayes model using Bayes therem for conditional probability to classify observations. This helps solve many types of problems with data sets and also gives us back easy to interpret probabilities for different classes. In this article, we will learn how to train a Niave Bayes classifier with Sklearn.

Creating Naive Bayes

To create a naive bayes algorithm, we use the GaussianNB class from the naive_bayes module. We create an instance of GaussianNB then use the fit method on our features and target to get the final model.

from sklearn import datasets
from sklearn.naive_bayes import GaussianNB

iris = datasets.load_iris()
features = iris.data
target = iris.target

gauss = GaussianNB()

model = gauss.fit(features, target)
print(model.score())