Smoothing and log likelihood
Quick answer
Additive smoothing assigns nonzero likelihood to known categories or tokens not observed in a particular class. Log-space scoring prevents products of tiny probabilities from underflowing. Examples run locally with Python 3.13.5, scikit-learn 1.8.0, NumPy 2.3.5 and SciPy 1.17.0 when imported. ByteMind library execution is unverified.
Smooth a zero count
counts=[3,0]; alpha=1print([(c+alpha)/(sum(counts)+alpha*len(counts)) for c in counts])Expected output:
[0.8, 0.2]The unseen known token receives probability 0.2 and the probabilities sum to one.
How it works
For a count vector of V known categories, add alpha>0 to each count and divide by total count plus alpha*V. With counts [3,0] and alpha=1, probabilities are [4/5,1/5]. This makes an unseen-within-class token possible, not a guarantee that an arbitrary new token is in the fitted vocabulary. Laplace smoothing is alpha=1; other positive values are tunable on validation data. For CategoricalNB each feature has its own category distribution; predefine a nonnegative integer code for unknown values and reserve that category using min_categories, with the same encoder at inference. Do not treat -1 as a valid CategoricalNB code. For log joint scores, add log prior to log likelihood terms instead of multiplying. For probability normalization, subtract the maximum score m, exponentiate and divide by the sum; this is equivalent to softmax for finite scores. If all scores are negative infinity, the normalization is undefined, so detect invalid model/input rather than returning arbitrary probabilities. Log likelihood does not imply a good probability calibration. For multinomial features, all-zero input produces the prior; for Bernoulli it also includes absence terms. Smoothing changes estimated parameters and can reduce variance at the expense of bias. Choose alpha using validation, not final test labels.
Reserve an unknown category
from sklearn.naive_bayes import CategoricalNBm=CategoricalNB(alpha=1,min_categories=3).fit([[0],[0],[1],[1]],[0,0,1,1])print(m.predict_proba([[2]]).round(6).tolist())Expected output:
[[0.5, 0.5]]The reserved code 2 is valid but unseen in both classes. Equal priors and equal smoothed likelihoods give equal scores.
Common mistakes
Do not add alpha only to the numerator without changing the denominator. Never multiply hundreds of small probabilities directly. Reserve unknown categories before fitting rather than expanding the model at inference.
Practice and next steps
Calculate smoothed probabilities for [0,0,4] with alpha=1. Explain why an unseen category needs an explicit representation. Complete the knowledge check and coding exercise. Next: Recursive partitioning.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Smoothing and log likelihood — knowledge check
Apply smoothing and log likelihood to new examples.
What is the denominator for additive smoothing?
What is Laplace smoothing?
Why use log scores?
How should an unknown categorical code be handled?
What if every log score is negative infinity?