Scikit Learn - Multinomial Naïve Bayes



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.No Parameter & Description
1

alpha − float, optional, default = 1.0

It represents the additive smoothing parameter. If you choose 0 as its value, then there will be no smoothing.

2

fit_prior − Boolean, optional, default = true

It 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.

3

class_prior − array-like, size(n_classes,), optional, Default = None

This parameter represents the prior probabilities of each class.

Attributes

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

Sr.No Attributes & Description
1

class_log_prior_ − array, shape(n_classes,)

It provides the smoothed log probability for every class.

2

class_count_ − array, shape(n_classes,)

It provides the actual number of training samples encountered for each class.

3

intercept_ − array, shape (n_classes,)

These are the Mirrors class_log_prior_ for interpreting MultinomilaNB model as a linear model.

4

feature_log_prob_ − array, shape (n_classes, n_features)

It gives the empirical log probability of features given a class $P\left(\begin{array}{c} features\arrowvert Y\end{array}\right)$.

5

coef_ − array, shape (n_classes, n_features)

These are the Mirrors feature_log_prior_ for interpreting MultinomilaNB model as a linear model.

6

feature_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]
Advertisements