ML0092 Naive Bayes for Continuous Features

How does Naive Bayes handle continuous numeric features, and what goes wrong when the Gaussian assumption is violated?

Answer

The default approach is Gaussian Naive Bayes: for each continuous feature, the model estimates a per-class mean and variance from the training data and plugs the feature value into the Gaussian PDF to get the likelihood. This is fast (one pass to compute mean and variance, O(nd)) and works well when features are approximately normal within each class, as in the Iris dataset or standardized medical lab values. The problem is that the Gaussian assumption is often violated: income is right-skewed, reaction times are log-normal, and sensor readings can be multi-modal. When the true distribution is skewed or has heavy tails, the Gaussian likelihood misestimates the probability mass, pushing the posterior toward 0 or 1 incorrectly. The fixes are discretization (bin the feature and use Categorical or Multinomial NB), kernel density estimation (replace the parametric Gaussian with a non-parametric KDE), or transformation (log or Box-Cox transform to normalize the feature before applying Gaussian NB).

(1) Gaussian NB (Default): estimates \mu_{yj} and \sigma_{yj}^2 per class per feature, then uses the Gaussian PDF in the likelihood product; O(nd) training, O(d) prediction, but assumes normality.
(2) Discretization: bin the continuous feature into k bins (equal-width or quantile), then treat it as categorical with k values; the 2024 Max-Relevance-Min-Divergence (MRmD) method beats prior discretization schemes on most of 45 benchmark datasets by maximizing discriminant information and generalization simultaneously.
(3) Kernel Density Estimation: replace the Gaussian with a non-parametric KDE \hat{f}(x \mid y) = \frac{1}{n_y h}\sum K(\frac{x - x_i}{h}); the R naivebayes package and MATLAB’s ClassificationNaiveBayes both support it, and a 2025 paper used optimized robust KDE with Welsch M-estimation to handle outliers in Bayesian classification.

Three panels: left shows a skewed income distribution with a Gaussian fit that misses the long tail; middle shows discretization into bins with per-class bin probabilities; right shows a KDE curve that follows the true skewed shape closely

Figure 1: Three approaches to continuous features in Naive Bayes: Gaussian NB fits a normal curve that misses skewness, discretization bins the feature into categorical probabilities, and kernel density estimation follows the true non-parametric shape.

The practical decision flow is: start with Gaussian NB and check the per-class histograms. If the feature is approximately normal within each class, Gaussian is fine and is the cheapest option. If the feature is skewed but unimodal, apply a log or Box-Cox transform and re-check normality. If the feature is multi-modal or has heavy tails that resist transformation, use discretization (simpler, faster, but loses information) or KDE (more accurate, but slower at prediction because it evaluates the kernel against all training points in that class). Scikit-learn’s GaussianNB also tends to produce overconfident probabilities because the independence assumption compounds with the Gaussian misfit, so CalibratedClassifierCV with isotonic regression is recommended when the predicted probabilities need to be reliable, as in a medical risk score where the probability itself (not just the classification) drives the decision.

Mathematical Formulation:
P(x_j \mid y) = \frac{1}{\sqrt{2\pi\sigma_{yj}^2}} \exp\!\left(-\frac{(x_j - \mu_{yj})^2}{2\sigma_{yj}^2}\right)
\hat{f}_h(x \mid y) = \frac{1}{n_y h} \sum_{i=1}^{n_y} K\!\left(\frac{x - x_i^{(y)}}{h}\right)

Where:

  • The first equation is Gaussian NB: \mu_{yj} and \sigma_{yj}^2 are the class-conditional mean and variance of feature j, estimated as the sample mean and variance of all training values of feature j in class y.
  • The second equation is kernel density estimation: n_y is the number of training points in class y, h is the bandwidth, K is the kernel function (typically Gaussian), and x_i^{(y)} are the training values of the feature in class y.
  • For discretization, the continuous range is split into k bins, and P(\text{bin}_b \mid y) = (N_{yb} + \alpha) / (N_y + \alpha k) with Laplace smoothing, reducing the continuous feature to a categorical one.
MethodAssumptionTraining CostWhen to Use
Gaussian NBNormal per classO(nd) one passApproximately normal features (Iris, standardized labs)
DiscretizationNo distributional assumptionO(nd) binning + countingSkewed, multi-modal, or heavy-tailed features
KDENon-parametricO(nd) storage; O(n_y) per predictionArbitrary distributions where bandwidth can be tuned
Transform + GaussianNormal after transformO(nd) transform + estimateLog-normal, power-law features (income, reaction time)

Login to view more content


Log in to track your progress

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *