Scikit-Learn : Multinomial Naïve Bayes

Scikit-Learn : Multinomial Naïve Bayes

In this guide, we will discuss Multinomial Naïve Bayes in Scikit-Learn. It is another useful Naïve Bayes classifier. It assumes that the features are drawn from a simple Multinomial distribution. The Scikit-learn provides sklearn.naive_bayes.MultinomialNB to implement the Multinomial Naïve Bayes algorithm for classification.

Parameters

Following table consist the parameters used by sklearn.naive_bayes.MultinomialNB method −

Sr.NoParameter & Description
1alpha − float, optional, default = 1.0It represents the additive smoothing parameter. If you choose 0 as its value, then there will be no smoothing.
2fit_prior − Boolean, optional, default = trueIt tells the model that whether to learn class prior probabilities or not. The default value is True but if set to False, the algorithms will use a uniform prior.
3class_prior − array-like, size(n_classes,), optional, Default = NoneThis parameter represents the prior probabilities of each class.

Attributes

Following table consist the attributes used by sklearn.naive_bayes.MultinomialNB method −

Sr.NoAttributes & Description
1class_log_prior_ − array, shape(n_classes,)It provides the smoothed log probability for every class.
2class_count_ − array, shape(n_classes,)It provides the actual number of training samples encountered for each class.
3intercept_ − array, shape (n_classes,)These are the Mirrors class_log_prior_ for interpreting MultinomilaNB model as a linear model.
4feature_log_prob_ − array, shape (n_classes, n_features)It gives the empirical log probability of features given a class P(features⏐Y).
5coef_ − array, shape (n_classes, n_features)These are the Mirrors feature_log_prior_ for interpreting MultinomilaNB model as a linear model.
6feature_count_ − array, shape (n_classes, n_features)It provides the actual number of training samples encountered for each (class,feature).

The methods of sklearn.naive_bayes. MultinomialNB are same as we have used in sklearn.naive_bayes.GaussianNB.

Implementation Example

The Python script below will use sklearn.naive_bayes.GaussianNB method to construct Gaussian Naïve Bayes Classifier from our data set −

Example

import numpy as np
X = np.random.randint(8, size = (8, 100))
y = np.array([1, 2, 3, 4, 5, 6, 7, 8])

from sklearn.naive_bayes import MultinomialNB
MNBclf = MultinomialNB()
MNBclf.fit(X, y)

Output

MultinomialNB(alpha = 1.0, class_prior = None, fit_prior = True)

Now, once fitted we can predict the new value aby using predict() method as follows −

Example

print((MNBclf.predict(X[4:5]))

Output

[5]

Next Topic : Click Here

This Post Has 2 Comments

Leave a Reply