Naive Bayes
scikit-learn
prior probability
machine learning
data science

How to specify the prior probability for scikit-learn's Naive Bayes

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

Naive Bayes combines feature likelihoods with class priors, so changing the prior can change the final prediction even when the observed features stay the same. In scikit-learn, the usual way to specify priors is with the class_prior parameter on Naive Bayes estimators that support it. This is useful when the training sample does not reflect the real class distribution or when domain knowledge gives you a better baseline.

What the Prior Means

The prior is the probability of each class before looking at the features. In Bayes' rule, it is the baseline weight assigned to each label before the likelihood terms are applied.

For a binary classifier, priors such as [0.9, 0.1] mean class 0 is assumed much more common than class 1 before any evidence from the input is considered.

That matters most when the evidence is weak or ambiguous. If the features strongly support one class, the likelihood terms can still outweigh the prior.

Pass class_prior Explicitly

For classifiers such as MultinomialNB and BernoulliNB, specify the priors at construction time:

python
1import numpy as np
2from sklearn.naive_bayes import MultinomialNB
3
4X = np.array([
5    [3, 0],
6    [2, 1],
7    [0, 3],
8    [1, 2],
9])
10y = np.array([0, 0, 1, 1])
11
12model = MultinomialNB(class_prior=[0.7, 0.3])
13model.fit(X, y)
14
15print(model.class_log_prior_)
16print(model.predict([[1, 1]]))

The model still learns feature likelihoods from the data. The only thing you are overriding is the class baseline.

fit_prior and Uniform Priors

If you do not pass class_prior, many Naive Bayes estimators infer the priors from the label frequencies in the training data. That behavior is controlled by fit_prior.

python
1from sklearn.naive_bayes import BernoulliNB
2
3model = BernoulliNB(fit_prior=True)
4model.fit(X > 0, y)
5print(np.exp(model.class_log_prior_))

If you set fit_prior=False, the estimator typically uses uniform priors instead of learning them from class counts.

python
model = BernoulliNB(fit_prior=False)
model.fit(X > 0, y)
print(np.exp(model.class_log_prior_))

That is a useful comparison when you want to see how much of the prediction is coming from the label frequency baseline versus the feature evidence.

Check Which Priors the Model Is Actually Using

After fitting, inspect class_log_prior_:

python
print(np.exp(model.class_log_prior_))

That gives you a direct sanity check. It is especially helpful if:

  • you supplied custom priors
  • you changed fit_prior
  • your class labels are not ordered the way you expect

Always verify the class order through model.classes_ if there is any ambiguity.

When Manual Priors Make Sense

Manual priors are useful when the training data is intentionally resampled or otherwise distorted. For example, you might oversample a rare class to help the classifier learn its feature distribution, while still wanting production-time class prevalence to remain low.

They are also useful when domain knowledge is stronger than sample frequency. A diagnostic classifier trained on a curated dataset may not reflect the real-world rate of positive cases, so a manual prior can move the model back toward deployment reality.

Common Pitfalls

The biggest mistake is passing prior values in the wrong class order. Scikit-learn aligns the prior vector with the estimator's internal class ordering, not with your assumptions.

Another issue is expecting priors to override strong feature evidence completely. They do not. Naive Bayes still multiplies prior and likelihood, so informative features can dominate.

People also sometimes use manual priors to patch deeper data problems. If the features, labels, or evaluation setup are wrong, changing the prior is not a real fix.

Finally, do not confuse class priors with class weights or sample weights. Those affect training differently and solve different imbalance problems.

Summary

  • Use class_prior to specify explicit priors in supported scikit-learn Naive Bayes estimators.
  • Use fit_prior=False when you want uniform priors instead of learned class frequencies.
  • Inspect class_log_prior_ and classes_ after fitting to verify what the model used.
  • Manual priors are most helpful when training data does not reflect deployment prevalence.
  • Priors influence predictions, but they do not replace feature evidence.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.