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.
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:
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.
If you set fit_prior=False, the estimator typically uses uniform priors instead of learning them from class counts.
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_:
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_priorto specify explicit priors in supported scikit-learn Naive Bayes estimators. - Use
fit_prior=Falsewhen you want uniform priors instead of learned class frequencies. - Inspect
class_log_prior_andclasses_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
- How to speed-up k-means from Scikit learn?
- How to speedup rnn training speed of tensorflow?
- how to split a dataset into training and validation set keeping ratio between classes?
- How to split data based on a column value in sklearn
- How to split a dataframe string column into two columns?
- How to Split the Input into different channels in Keras
- How to subsample a 2D polygon?
- How to tell if an array is a permutation in On?

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 courseTrack 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.