Scikit-Learn : Complement Naïve Bayes

Scikit-Learn : Complement Naïve Bayes

In this guide, we will discuss Complement Naïve Bayes in Scikit-Learn. Another useful naïve Bayes model which was designed to correct the severe assumptions made by Multinomial Bayes classifier. This kind of NB classifier is suitable for imbalanced data sets. The Scikit-learn provides sklearn.naive_bayes.ComplementNB to implement the Gaussian Naïve Bayes algorithm for classification.

Parameters

Following table consist the parameters used by sklearn.naive_bayes.ComplementNB 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. This parameter is only used in edge case with a single class in the training data set.
3class_prior − size(n_classes,), optional, Default = NoneThis parameter represents the prior probabilities of each class.
4norm − Boolean, optional, default = FalseIt tells the model that whether to perform second normalization of the weights or not.

Attributes

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

Sr.NoAttributes & Description
1class_log_prior_ − array, shape(n_classes,)It provides the smoothed empirical log probability for every class. This attribute is only used in edge case with a single class in the training data set.
2class_count_ − array, shape(n_classes,)It provides the actual number of training samples encountered for each class.
3feature_log_prob_ − array, shape (n_classes, n_features)It gives the empirical weights for class components.
4feature_count_ − array, shape (n_classes, n_features)It provides the actual number of training samples encountered for each (class,feature).
5feature_all_ − array, shape(n_features,)It provides the actual number of training samples encountered for each feature.

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

Implementation Example

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

Example

import numpy as np
X = np.random.randint(15, size = (15, 1000))
y = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
from sklearn.naive_bayes import ComplementNB
CNBclf = ComplementNB()
CNBclf.fit(X, y)

Output

ComplementNB(alpha = 1.0, class_prior = None, fit_prior = True, norm = False)

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

Example

print((CNBclf.predict(X[10:15]))

Output

[11 12 13 14 15]

Next Topic : Click Here

This Post Has 2 Comments

Leave a Reply