Scikit-Learn : Gaussian Naïve Bayes

Scikit-Learn : Gaussian Naïve Bayes

In this guide, we will discuss Gaussian Naïve Bayes in Scikit-Learn. As the name suggest, Gaussian Naïve Bayes classifier assumes that the data from each label is drawn from a simple Gaussian distribution. The Scikit-learn provides sklearn.naive_bayes.GaussianNB to implement the Gaussian Naïve Bayes algorithm for classification.

Parameters

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

Sr.NoParameter & Description
1priors − arrray-like, shape(n_classes)It represents the prior probabilities of the classes. If we specify this parameter while fitting the data, then the prior probabilities will not be justified according to the data.
2Var_smoothing − float, optional, default = 1e-9This parameter gives the portion of the largest variance of the features that is added to variance in order to stabilize calculation.

Attributes

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

Sr.NoAttributes & Description
1class_prior_ − array, shape(n_classes,)It provides the probability of every class.
2class_count_ − array, shape(n_classes,)It provides the actual number of training samples observed in every class.
3theta_ − array, shape (n_classes, n_features)It gives the mean of each feature per class.
4sigma_ − array, shape (n_classes, n_features)It gives the variance of each feature per class.
5epsilon_ − floatThese are the absolute additive value to variance.

Methods

Following table consist the methods used by sklearn.naive_bayes.GaussianNB method −

Sr.NoMethod & Description
1fit(self, X, y[, sample_weight])This method will Fit Gaussian Naive Bayes classifier according to X and y.
2get_params(self[, deep])With the help of this method we can get the parameters for this estimator.
3partial_fit(self, X, y[,classes, sample_weight])This method allows the incremental fit on a batch of samples.
4predict(self, X)This method will perform classification on an array of test vectors X.
5predict_log_proba(self, X)This method will return the log-probability estimates for the test vector X.
6predict_proba(self, X)This method will return the probability estimates for the test vector X.
7score(self, X, y[, sample_weight])With this method we can get the mean accuracy on the given test data and labels.
9set_params(self, \*\*params)This method allows us to set the parameters of this estimator.

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.array([[-1, -1], [-2, -4], [-4, -6], [1, 2]])
Y = np.array([1, 1, 2, 2])
from sklearn.naive_bayes import GaussianNB
GNBclf = GaussianNB()
GNBclf.fit(X, Y)

Output

GaussianNB(priors = None, var_smoothing = 1e-09)

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

Example

print((GNBclf.predict([[-0.5, 2]]))

Output

[2]

Next Topic : Click Here

This Post Has One Comment

Leave a Reply