Understand the idea
Naive Bayes starts with a simple idea from Bayes' theorem: to guess which class an observation belongs to, start from how common each class already is (the prior), then let every predictor 'vote' based on how typical its observed value looks for each class. Multiply all those votes together with the prior, and whichever class comes out on top wins.
The 'naive' part is a deliberate simplification: the method pretends every predictor is completely independent of the others once you already know the class, which is basically never exactly true (bill length and flipper length are genuinely correlated within a species, for instance). Surprisingly, this rarely ruins the classifier: getting the WINNING class right only requires the RANKING of the scores to come out correctly, not their exact values, and that ranking often survives even when the independence assumption is violated. The price you pay is that the predicted PROBABILITIES themselves tend to be over-confident: trust them to rank and classify, not as calibrated risk numbers.
A classic practical trap this method handles by name: if a particular predictor value NEVER appeared alongside a particular class in your training data, a naive calculation would give that combination a probability of exactly zero, and since the whole score is a product, one zero vetoes the entire class regardless of how strongly every other predictor points to it. Laplace (add-one) smoothing is the standard fix: it nudges every count up slightly so nothing is ever exactly zero, at the cost of a small, deliberate bias.
Naive Bayes assumes conditional independence: within each class, the predictors are treated as unrelated to each other, which is the 'naive' part. Gaussian Naive Bayes models each numeric predictor with a bell-shaped curve inside each class. Laplace smoothing adds a small count to every category so that a value never seen in training does not force a probability of exactly zero. The no-information rate is the accuracy you would get by always guessing the most common class, the baseline any model must beat.
Practitioner explanation
KernelStats supports Gaussian (continuous predictors, per-class mean/variance with a small variance-smoothing term added for numerical stability), categorical (Laplace/add-alpha smoothed conditional tables, default alpha=1.0), or MIXED predictor sets; for mixed inputs, both parts are fit and their class-conditional LOG-LIKELIHOODS ADD, with the prior counted only once; this addition is legitimate precisely because of the independence assumption, and the product states this connection directly. The zero-frequency problem is handled by Laplace smoothing: P(x_j=l|c) = (n_lc + alpha)/(n_c + alpha*K_j), so a level never observed with a class in training gets a small nonzero probability instead of vetoing that class outright.
EVERY performance figure is reported in two clearly labeled versions, always both shown together: in-sample (scored on training rows, explicitly stated to be optimistic by construction, since a predictor with only one or two rows per level can reach 1.000 precision in-sample while carrying no real information) and cross-validated (5-fold stratified by default, seed 42, pooled out-of-fold predictions). A 'Train vs cross-validated gap' check explicitly flags a gap above 5 accuracy points; the product states plainly that a large gap does not mean the arithmetic is wrong, it means the in-sample figures describe memorization, and the cross-validated numbers are the ones to quote.
Accuracy is explicitly benchmarked against the no-information rate (always guessing the largest class), because the class priors mechanically pull predictions toward the majority class BY DESIGN: a '90% accurate' model on 90%-majority-class data may have learned nothing at all, and per-class recall plus the confusion matrix are recommended over the single pooled accuracy number for exactly this reason. This function fits and evaluates strictly within the loaded dataset: if new data later contains a predictor level never seen during fitting, the smoothed table still assigns it a small nonzero probability (via alpha), but that probability is pure smoothing artifact and the product recommends treating such predictions with caution.
When to use it
Consider it when
You need a fast, robust baseline classifier for two or more classes from continuous and/or categorical predictors, especially when the number of predictors is large relative to the sample size, where the drastic independence simplification reduces the parameter count enough to avoid overfitting fancier models would suffer from.
Use caution when
Predictors are strongly correlated with each other within classes AND that correlation carries real classification information (LDA or logistic regression handle correlated predictors more gracefully), you need well-CALIBRATED probability estimates rather than just correct rankings (Naive Bayes' probabilities tend to be overconfident), or the outcome is severely imbalanced without checking accuracy against the no-information rate.
Formulas and symbols
Naive Bayes posterior (proportional form)
pi_c the prior probability of class c (its training-data share); the product assumes conditional independence of predictors given the class.
Laplace (add-alpha) smoothing
n_lc the count of level l with class c; K_j the number of levels of predictor j. alpha=1 is classic Laplace smoothing, preventing a zero count from vetoing a whole class.
Cross-validated vs in-sample accuracy gap
Flagged explicitly above 5 accuracy points; a large gap indicates the in-sample number reflects memorization, not genuine predictive skill.
Assumptions and checks
- Naive Bayes assumes predictors are conditionally independent given the class; strongly correlated predictors violate this and can make the model over-confident (double-counting the same evidence), distorting the predicted class probabilities even when the predicted class label is still often correct.
In KernelStats: Checked directly: the card computes the maximum pairwise correlation among the (class-centered) predictors and flags it as a warning above 0.8.
- Normality (of residuals, not of the raw data). Gaussian Naive Bayes models each continuous predictor as normally distributed within each class; strongly skewed predictors violate this, though the classifier is fairly robust to mild non-normality since usually only the class ranking of probabilities, not their exact values, needs to be right.
In KernelStats: Checked directly: the card computes the largest within-class skewness across the continuous predictors (for classes with at least 8 values and non-zero variance) and flags it as a warning above 2.0.
- Without Laplace (additive) smoothing, a categorical predictor level that never co-occurred with a class in the training data forces that class's probability to exactly zero whenever that level appears, however strong the evidence from every other predictor.
In KernelStats: Checked directly: the card reports whether any zero-count cells exist in the categorical part alongside an alpha = 0 (no smoothing) setting, and flags that combination as a warning.
- With a severely imbalanced outcome, a classifier can beat the naive 'always predict the majority class' baseline (the no-information rate) by only a small margin while still looking like it has decent accuracy; comparing accuracy to the base rate is essential to see whether the model is actually learning anything.
In KernelStats: Checked directly: the card compares accuracy to the no-information rate and fails the check when the model cannot beat it, using cross-validated accuracy when available (in-sample accuracy against the training rows' own base rate flatters the model).
Alternatives
- Linear Discriminant Analysis. Predictors are correlated within classes and you want a model that explicitly keeps a shared covariance structure rather than assuming independence.
- Logistic Regression. You have a binary outcome and want interpretable coefficients (odds ratios) with well-behaved probability estimates rather than Naive Bayes' typically overconfident ones.
Worked example
Scenario
Classifying penguin species from bill length (continuous) and island (categorical), with 5-fold cross-validation and default smoothing.
Result
In-sample accuracy = 96.2%, cross-validated accuracy = 95.6% (a 0.6-point gap, far below the 5-point flag threshold; fold accuracies 92.6% to 98.6%); no-information rate = 44.2%.
Interpretation
The small in-sample/CV gap suggests the model is not substantially overfitting, and the large margin over the no-information rate confirms it is genuinely using the predictors' information rather than merely reflecting the class imbalance; 95.6% is the honest number to report.
Dataset: penguins.csv
Common misconceptions
Misconception
Naive Bayes' predicted class probabilities can be read as calibrated risk estimates (e.g. '92% probability of class A').
Correction
Because the conditional-independence assumption is almost never exactly true, correlated predictors 'double-count' evidence and push the computed probabilities toward 0 or 1 more aggressively than is warranted, so the resulting probabilities are typically OVERCONFIDENT. The class rankings (which class wins) are usually still correct even when this happens, so use Naive Bayes to CLASSIFY and RANK, not to quote a calibrated probability.
Misconception
A model with 90% classification accuracy has clearly learned something useful.
Correction
Accuracy must always be read against the NO-INFORMATION RATE: the accuracy achieved by simply guessing the most common class every time. With 90% of rows already in one class, a model achieving 90% accuracy may have learned nothing at all; the priors mechanically pull predictions toward the majority class by design, so per-class recall and the confusion matrix are what actually reveal whether the model discriminates meaningfully.
Read more: Misconception: a model's training-set accuracy is its real-world performance
References
- Idiot's Bayes — Not So Stupid After All?, Hand, D. J. and Yu, K. (2001), International Statistical Review, 69(3), 385-398.
- An Introduction to Statistical Learning with Applications in R (2nd ed.), Section 4.4.4, James, G., Witten, D., Hastie, T. and Tibshirani, R. (2021).
