Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Scikit-Learn · Class

GaussianNB

GaussianNB is a Gaussian Naive Bayes classifier that models each feature with a class-specific normal distribution. It supports both batch training with fit and incremental learning with partial_fit.

How it works

GaussianNB classifies observations by assuming each feature follows a Gaussian distribution within each class. It estimates the class-specific feature means and variances with maximum likelihood, combines those likelihoods with class priors, and supports incremental updates through partial_fit.

Syntax

class sklearn.naive_bayes.GaussianNB(*, priors=None, var_smoothing=1e-09)

Parameters

NameTypeDescription
priorsarray-like of shape (n_classes,), default=NonePrior probabilities of the classes. If specified, the priors are not adjusted according to the data.
var_smoothingfloat, default=1e-9Portion of the largest variance of all features that is added to variances for calculation stability.

Returns

An instance of GaussianNB after construction; fit and partial_fit return the fitted estimator.

Key points

  • GaussianNB is a probabilistic classifier for supervised classification tasks.
  • For each class, it estimates feature means and variances with maximum likelihood.
  • The constructor accepts optional class priors and variance smoothing, with smoothing defaulting to 1e-9.
  • fit learns the classifier from feature matrix X and target labels y.
  • partial_fit updates the classifier incrementally from one batch at a time.

Common mistakes

  • The model's Gaussian likelihood assumption may be unsuitable when feature distributions are strongly non-Gaussian.
  • Use partial_fit for incremental batches, but provide the complete class list on the first call.

Example: Train and predict with GaussianNB

from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import GaussianNB

X, y = load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.3, random_state=0
)
model = GaussianNB()
model.fit(X_train, y_train)
print(model.predict(X_test[:3]))

This trains Gaussian Naive Bayes on part of the Iris dataset and predicts labels for three held-out samples.

When to use it

Continuous-feature classification

Classify numeric feature vectors when class-conditional feature distributions are approximately Gaussian.

Incremental learning

Train on data arriving in separate batches or too large to process as one batch.

Custom class priors

Encode domain knowledge about class frequencies before fitting.

Interview questions

QWhat assumptions does GaussianNB make?

GaussianNB assumes that feature values are Gaussian within each class and estimates the class-specific means and variances by maximum likelihood. It then uses these likelihoods with class priors to classify samples.

Follow-up: What must be supplied for the first incremental update? Pass the complete set of possible class labels through classes when starting partial_fit.

QWhat do priors and var_smoothing control?

The constructor accepts optional priors and a variance-smoothing parameter. priors lets you specify class probabilities, while var_smoothing controls numerical stabilization of feature variances.

Follow-up: Why is variance stabilization useful? It prevents extremely small estimated variances from making the likelihood numerically unstable.

QHow can GaussianNB train incrementally?

GaussianNB supports online learning through partial_fit, which updates model parameters from batches instead of requiring all training data at once. The ordinary fit method trains from the supplied complete dataset.

Follow-up: How would you process a stream? Call partial_fit repeatedly on successive batches and identify all classes in the initial call.

  • BernoulliNB
  • CategoricalNB
  • ComplementNB
  • MultinomialNB
  • fit
  • partial_fit
  • predict
  • predict_log_proba

Source: GaussianNB — scikit-learn 1.9.1 documentation. Checked 2026-10-15.