Text classification
Document classification with bag-of-words or other nonnegative features.
Scikit-Learn · Class
ComplementNB is a Complement Naive Bayes classifier designed for imbalanced datasets. It uses complement-class statistics and is especially useful for text classification with nonnegative features.
ComplementNB is a Complement Naive Bayes classifier that adapts multinomial Naive Bayes by estimating each class from examples belonging to other classes. It is useful for imbalanced classification, especially with text-like count features.
class sklearn.naive_bayes.ComplementNB(*, alpha=1.0, force_alpha=True, fit_prior=True, class_prior=None, norm=False)| Name | Type | Description |
|---|---|---|
alpha | float or array-like of shape (n_features,), default=1.0 | Additive Laplace/Lidstone smoothing; a scalar or an array-like value with shape (n_features,). Default: 1.0. |
force_alpha | bool, default=True | Whether to force alpha to be used even when it is zero. Default: True. |
fit_prior | bool, default=True | Whether to learn class priors; used only for the single-class training edge case. Default: True. |
class_prior | array-like of shape (n_classes,), default=None | Prior probabilities for the classes. Default: None. |
norm | bool, default=False | Whether to apply a second normalization to the weights. Default: False. |
An instance of sklearn.naive_bayes.ComplementNB; after fitting, prediction methods return labels, probabilities, or log-probabilities.
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import ComplementNB
X = CountVectorizer().fit_transform([
"team wins match", "team scores goal",
"stock market rises", "market price falls",
])
y = ["sports", "sports", "finance", "finance"]
clf = ComplementNB(alpha=1.0)
clf.fit(X, y)
print(clf.predict(CountVectorizer().fit_transform(["team scores"])))Train ComplementNB on nonnegative count features and predict a document.
Document classification with bag-of-words or other nonnegative features.
Supervised classification where some classes have substantially fewer examples.
Sparse feature matrices produced by vectorizers.
ComplementNB is an adaptation of multinomial Naive Bayes designed for imbalanced datasets. It computes model weights using statistics from the complement of each class.
Follow-up: Why can it be preferable to MultinomialNB for text classification? The documentation describes its parameter estimates as more stable and reports frequent performance advantages on text tasks.
alpha is the additive Laplace/Lidstone smoothing parameter. It can be a scalar or an array with one value per feature, and its default is 1.0.
Follow-up: How would you represent feature-specific smoothing? Pass an array-like alpha of shape (n_features,), matching the feature columns.
The classifier accepts X as an array-like or sparse matrix of nonnegative feature values and y as the target labels. After fitting, predict accepts an array of test vectors and returns class labels.
Follow-up: Which related methods provide confidence-like outputs? predict_proba returns probability estimates, while predict_log_proba returns their logarithms.
MultinomialNBBernoulliNBCategoricalNBGaussianNBBayes