Scikit Learn - Complement Nave Bayes
Another useful nave 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 Nave Bayes algorithm for classification.
Parameters
Following table consist the parameters used by sklearn.naive_bayes.ComplementNB 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. This parameter is only used in edge case with a single class in the training data set. |
| 3 | class_prior − size(n_classes,), optional, Default = None This parameter represents the prior probabilities of each class. |
| 4 | norm − Boolean, optional, default = False It 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.No | Attributes & Description |
|---|---|
| 1 | 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. |
| 2 | class_count_ − array, shape(n_classes,) It provides the actual number of training samples encountered for each class. |
| 3 | feature_log_prob_ − array, shape (n_classes, n_features) It gives the empirical weights for class components. |
| 4 | feature_count_ − array, shape (n_classes, n_features) It provides the actual number of training samples encountered for each (class,feature). |
| 5 | 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..
Implementation Example
The Python script below will use sklearn.naive_bayes.BernoulliNB method to construct Bernoulli Nave 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]