Building Naive Bayes with Sklearn

Naive Bayes is a supervised learning algorithm that is used for classification tasks. It is based on the Bayes theorem, which states that the probability of event A occurring, given that event B has already occurred, is equal to the probability of event A occurring times the probability of event B occurring given that event A has already occurred, divided by the probability of event B occurring.

In scikit-learn, there are three different implementations of the Naive Bayes classifier:

  • GaussianNBĀ : This classifier is used for data that is distributed normally.
  • MultinomialNBĀ : This classifier is used for data that is counts of occurrences.
  • BernoulliNBĀ : This classifier is used for data that is binary (0 or 1).

Here are some of the parameters that can be tuned for the Naive Bayes classifier:

  • alpha: The smoothing parameter. This parameter controls how much smoothing is applied to the estimated probabilities.
  • fit_prior: Whether to fit the prior probabilities for the classes. If this parameter is set to False, then the prior probabilities will be assumed to be equal.
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
iris = load_iris()
X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.25)
clf = GaussianNB()
clf.fit(X_train, y_train)
y_pred = clf.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
print('Accuracy:', accuracy)

Last modified: Tuesday, 12 September 2023, 2:11 PM