Scikit Learn - Gaussian Naïve Bayes



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

priors − 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.

2

Var_smoothing − float, optional, default = 1e-9

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

class_prior_ − array, shape(n_classes,)

It provides the probability of every class.

2

class_count_ − array, shape(n_classes,)

It provides the actual number of training samples observed in every class.

3

theta_ − array, shape (n_classes, n_features)

It gives the mean of each feature per class.

4

sigma_ − array, shape (n_classes, n_features)

It gives the variance of each feature per class.

5

epsilon_ − float

These are the absolute additive value to variance.

Methods

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

Sr.No Method & Description
1

fit(self, X, y[, sample_weight])

This method will Fit Gaussian Naive Bayes classifier according to X and y.

2

get_params(self[, deep])

With the help of this method we can get the parameters for this estimator.

3

partial_fit(self, X, y[,classes, sample_weight])

This method allows the incremental fit on a batch of samples.

4

predict(self, X)

This method will perform classification on an array of test vectors X.

5

predict_log_proba(self, X)

This method will return the log-probability estimates for the test vector X.

6

predict_proba(self, X)

This method will return the probability estimates for the test vector X.

7

score(self, X, y[, sample_weight])

With this method we can get the mean accuracy on the given test data and labels.

9

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