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

Scikit-Learn · Class

ComplementNB

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.

How it works

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.

Syntax

class sklearn.naive_bayes.ComplementNB(*, alpha=1.0, force_alpha=True, fit_prior=True, class_prior=None, norm=False)

Parameters

NameTypeDescription
alphafloat or array-like of shape (n_features,), default=1.0Additive Laplace/Lidstone smoothing; a scalar or an array-like value with shape (n_features,). Default: 1.0.
force_alphabool, default=TrueWhether to force alpha to be used even when it is zero. Default: True.
fit_priorbool, default=TrueWhether to learn class priors; used only for the single-class training edge case. Default: True.
class_priorarray-like of shape (n_classes,), default=NonePrior probabilities for the classes. Default: None.
normbool, default=FalseWhether to apply a second normalization to the weights. Default: False.

Returns

An instance of sklearn.naive_bayes.ComplementNB; after fitting, prediction methods return labels, probabilities, or log-probabilities.

Key points

  • ComplementNB implements the Complement Naive Bayes classifier associated with Rennie et al. (2003).
  • Its motivation is to address strong assumptions made by standard MultinomialNB.
  • Use it when class frequencies are uneven, particularly in classification problems with imbalance.
  • alpha controls additive smoothing and accepts either one scalar or feature-specific values.
  • norm selects whether the learned weights receive a second normalization step.
  • fit learns the classifier from feature matrix X and target vector y, optionally using sample weights.

Common mistakes

  • alpha must be chosen carefully: setting it to zero disables smoothing only when force_alpha is true, which can permit zero-valued probabilities.
  • fit_prior affects only the single-class training edge case; it does not generally control class-prior estimation.

Example: Classify text with ComplementNB

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.

When to use it

Text classification

Document classification with bag-of-words or other nonnegative features.

Imbalanced classes

Supervised classification where some classes have substantially fewer examples.

Sparse high-dimensional inputs

Sparse feature matrices produced by vectorizers.

Interview questions

QWhat is ComplementNB and when is it useful?

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.

QWhat does alpha control?

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.

QWhat inputs and outputs does the estimator use?

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.

  • MultinomialNB
  • BernoulliNB
  • CategoricalNB
  • GaussianNB
  • Bayes

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