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

Scikit-Learn · Class

CategoricalNB

CategoricalNB is a Naive Bayes classifier for categorically distributed features. Encode each feature’s categories as contiguous integers starting at zero before fitting.

How it works

CategoricalNB learns a separate categorical distribution for every feature conditioned on each class. It combines these distributions with class priors to classify encoded category values, using additive smoothing controlled by alpha; features must use contiguous integer codes beginning at zero.

Syntax

class sklearn.naive_bayes.CategoricalNB(*, alpha=1.0, force_alpha=True, fit_prior=True, class_prior=None, min_categories=None)

Parameters

NameTypeDescription
alphafloatAdditive Laplace/Lidstone smoothing parameter; default 1.0.
fit_priorboolWhether to learn class prior probabilities; default True.
class_priorarray-like of shape (n_classes,)Prior probabilities of the classes; default None.
min_categoriesint or array-like of shape (n_features,)Minimum number of categories per feature; default None.
force_alphaboolWhether alpha=0 is preserved rather than replaced by a small positive value; default True.

Returns

An instance of sklearn.naive_bayes.CategoricalNB; fitting returns the fitted estimator.

Key points

  • Use this classifier when predictors represent discrete categorical variables rather than continuous measurements.
  • The model treats each feature as having its own categorical probability distribution.
  • The alpha parameter applies additive smoothing to conditional category probabilities.
  • fit_prior determines whether class priors are estimated from the training labels.
  • class_prior lets you provide explicit class probabilities instead of estimating them from the data.
  • min_categories controls the minimum category-space size maintained for each feature.

Common mistakes

  • Input categories for each feature must be encoded as contiguous integers starting at 0; arbitrary or string values are not the expected representation.
  • Disabling smoothing requires both alpha=0 and force_alpha=True in the current API.
  • With fit_prior=False, learned class frequencies are ignored and classes receive a uniform prior.

Example: Classify encoded categorical features

from sklearn.naive_bayes import CategoricalNB
from sklearn.preprocessing import OrdinalEncoder

X_text = [["red", "small"], ["blue", "large"], ["red", "large"], ["blue", "small"]]
y = [0, 1, 0, 1]
encoder = OrdinalEncoder()
X = encoder.fit_transform(X_text)

model = CategoricalNB()
model.fit(X, y)
print(model.predict(encoder.transform([["red", "small"]])))

Encode each categorical feature as integer category codes, fit the classifier, then predict encoded samples.

When to use it

Categorical tabular classification

Classifying records whose predictors are discrete encoded categories.

Fixed category spaces

Training on datasets where category vocabularies are known in advance.

Encoded feature pipelines

Producing class predictions from compact integer-coded features.

Interview questions

QWhen is CategoricalNB appropriate?

CategoricalNB is intended for classification when features are categorically distributed. It estimates a feature-specific categorical distribution conditioned on the target class and applies the Naive Bayes independence assumption.

Follow-up: How must raw categories be prepared? Encode each feature independently so its values are integer codes from 0 through one less than that feature’s category count.

QWhat does alpha control?

alpha is an additive Laplace/Lidstone smoothing parameter, with a default of 1.0. Smoothing changes the estimated conditional probabilities so observed counts do not produce unsupported zero probabilities.

Follow-up: How can smoothing be disabled? Set alpha=0 and force_alpha=True.

QWhat is the purpose of min_categories?

min_categories specifies the minimum category count for each feature. An integer applies the same minimum to every feature, while an array-like value supplies one minimum per feature; None infers counts from training data.

Follow-up: Why might a minimum be useful? It can preserve a declared category space even when some categories are absent from the training subset.

  • BernoulliNB
  • GaussianNB
  • MultinomialNB
  • OrdinalEncoder
  • BaseNB

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