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.
Following table consist the parameters used by sklearn.naive_bayes.ComplementNB method −
|Sr.No||Parameter & Description|
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.
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. This parameter is only used in edge case with a single class in the training data set.
class_prior − size(n_classes,), optional, Default = None
This parameter represents the prior probabilities of each class.
norm − Boolean, optional, default = False
It tells the model that whether to perform second normalization of the weights or not.
Following table consist the attributes used by sklearn.naive_bayes.ComplementNB method −
|Sr.No||Attributes & Description|
class_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.
class_count_ − array, shape(n_classes,)
It provides the actual number of training samples encountered for each class.
feature_log_prob_ − array, shape (n_classes, n_features)
It gives the empirical weights for class components.
feature_count_ − array, shape (n_classes, n_features)
It provides the actual number of training samples encountered for each (class,feature).
feature_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..
The Python script below will use sklearn.naive_bayes.BernoulliNB method to construct Bernoulli Naïve Bayes Classifier from our data set −
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)
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 −
[11 12 13 14 15]