Continuous-feature classification
Classify numeric feature vectors when class-conditional feature distributions are approximately Gaussian.
Scikit-Learn · Class
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.
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.
class sklearn.naive_bayes.GaussianNB(*, priors=None, var_smoothing=1e-09)| Name | Type | Description |
|---|---|---|
priors | array-like of shape (n_classes,), default=None | Prior probabilities of the classes. If specified, the priors are not adjusted according to the data. |
var_smoothing | float, default=1e-9 | Portion of the largest variance of all features that is added to variances for calculation stability. |
An instance of GaussianNB after construction; fit and partial_fit return the fitted estimator.
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.
Classify numeric feature vectors when class-conditional feature distributions are approximately Gaussian.
Train on data arriving in separate batches or too large to process as one batch.
Encode domain knowledge about class frequencies before fitting.
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.
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.
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.
BernoulliNBCategoricalNBComplementNBMultinomialNBfitpartial_fitpredictpredict_log_proba