Categorical tabular classification
Classifying records whose predictors are discrete encoded categories.
Scikit-Learn · Class
CategoricalNB is a Naive Bayes classifier for categorically distributed features. Encode each feature’s categories as contiguous integers starting at zero before fitting.
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.
class sklearn.naive_bayes.CategoricalNB(*, alpha=1.0, force_alpha=True, fit_prior=True, class_prior=None, min_categories=None)| Name | Type | Description |
|---|---|---|
alpha | float | Additive Laplace/Lidstone smoothing parameter; default 1.0. |
fit_prior | bool | Whether to learn class prior probabilities; default True. |
class_prior | array-like of shape (n_classes,) | Prior probabilities of the classes; default None. |
min_categories | int or array-like of shape (n_features,) | Minimum number of categories per feature; default None. |
force_alpha | bool | Whether alpha=0 is preserved rather than replaced by a small positive value; default True. |
An instance of sklearn.naive_bayes.CategoricalNB; fitting returns the fitted estimator.
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.
Classifying records whose predictors are discrete encoded categories.
Training on datasets where category vocabularies are known in advance.
Producing class predictions from compact integer-coded features.
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.
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.
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.
BernoulliNBGaussianNBMultinomialNBOrdinalEncoderBaseNB