Unit 7 of 8 · about 3 hours · derive, implement

Naive Bayes: event models, smoothing and log-space scoring

Naive Bayes is the smallest complete probabilistic classifier: counting, smoothing and log-space scoring in one place. Its failure mode, confident scores from false independence, previews every calibration problem later.

You'll be able to

  • Derive the naive Bayes decision rule from conditional independence and count its free parameters per event model.
  • Implement Bernoulli naive Bayes with Laplace smoothing and log-space scoring, handling unseen values and single-class training data.
  • Select the event model (Bernoulli, multinomial, Gaussian) from the feature type and state what each independence assumption ignores.

Covers

Bernoulli, multinomial and Gaussian naive BayesPriors, smoothing, conditional independence and log-space scoring
1

Read

In this order, only the parts listed

0 of 5 done

  1. Naive Bayes: independence, smoothing and parameter countsTom Mitchell - Generative and Discriminative Classifiers: Naive Bayes and Logistic Regression

    Sections 2.1-2.3 (conditional independence, the naive Bayes derivation, discrete inputs with the smoothed estimate (7) and Laplace at , prior smoothing (9), plus free parameters) and 2.4 (Gaussian naive Bayes with mean and variance parameters)

  2. Naive Bayes in the generative familyAn Introduction to Statistical Learning with Python

    Chapter 4, Section 4.4.4 (p. 158): the naive Bayes model beside LDA and QDA

  3. Naive Bayes on binary MNIST pixelsDive into Deep Learning - linear models slice

    Section 22.9: The Probabilistic Model for Classification, The Naive Bayes Classifier, Training on pixels binarized at 128, then Exercises 1 (XOR failure) and 2 (unseen values without smoothing)

  4. Event models, smoothing and estimator limitsscikit-learn - linear models, discriminant analysis and naive Bayes

    Section 1.9 (MAP rule, the bad-estimator warning on predict_proba), 1.9.1 (GaussianNB), 1.9.2 (MultinomialNB smoothing formula, Laplace at versus Lidstone below 1) and 1.9.4 (BernoulliNB decision rule, explicit penalty on non-occurrence, binarize behavior)

  5. Gaussian naive Bayes lab on SmarketAn Introduction to Statistical Learning with Python

    Naive Bayes section: GaussianNB fit on Smarket with classes_ and class_prior_ readouts

2

Focus on

  • Smoothing is a MAP estimate under a Dirichlet prior, not a hack. Mitchell's at is Laplace, and BernoulliNB's is the same add-one per value; without it one unseen value zeroes the whole product.
  • Bernoulli penalizes absent indicator features while multinomial ignores absent counts. That is why occurrence vectors can beat count vectors on short documents, and why the two models are not interchangeable.
  • Score in log space always. A product of 1100 halves is exactly 0.0 in float64 while the log sum stays finite at -762.46; needs only the sums.
Common mistakes · 3
  • Feeding counts to BernoulliNB or magnitudes to a binary model without thought. BernoulliNB binarizes continuous input at binarize = 0.0 by default, silently turning magnitudes into presence flags.
  • Multiplying raw probabilities and comparing. Beyond underflow, one unsmoothed zero vetoes the class; sum logs and smooth first.
  • Reading predict_proba as calibrated confidence. The guide calls naive Bayes a bad estimator: use the scores to rank, then threshold or calibrate downstream.
3

Try it yourself

Closed-book, before checking anything

Derive the MAP rule from conditional independence. From a blank file implement Bernoulli naive Bayes with : on X = [[1,0],[1,1],[0,0],[0,1]] with y = [1,1,0,0] match sklearn feature_log_prob_ rows [-1.3863, -0.6931] for class 0 and [-0.2877, -0.6931] for class 1, predict [1] for [[1,0]], and return the majority class when trained on one class only.
4

Quiz yourself

Answer first, then mark it. Missed ones go to Review.

0 got · 0 missed

  1. 1.A test word never appeared in training. What happens with and without smoothing?Answer

    Without smoothing its likelihood is 0 and the whole class product is 0 regardless of other evidence. With Laplace it contributes a small nonzero factor that rarely decides the alone.

  2. 2.When does Bernoulli beat multinomial on text, and why?Answer

    With occurrence vectors on short documents: Bernoulli explicitly penalizes the non-occurrence of indicator words while multinomial ignores absent counts. sklearn advises evaluating both when time permits.

  3. 3.Write the smoothed discrete likelihood and name its prior.Answer

    : the MAP estimate under a symmetric Dirichlet prior with parameters , equivalently the posterior mean under parameters ; is Laplace. Priors smooth the same way with in the denominator.

  4. 4.XOR data with labels [0,1,1,0]. Does naive Bayes succeed, and which assumption fails?Answer

    No. Each feature alone is uninformative about the label, so conditional independence destroys the interaction. Allowing explicit dependence between the inputs fixes it.

5

Practice

  • Deep-ML 140: Bernoulli Naive Bayes Classifier

    NaiveBayes class with forward (Laplace smoothing at 1.0) and predict in log space; tests check a 5-row fit and a symmetric single-feature case, and the spec requires single-class training data to work

    Match sklearn BernoulliNB on the same inputs before submitting.

  • ISLP Lab 4.7.5: Naive Bayes on the Smarket data

    Naive Bayes section: GaussianNB fit with classes_ and class_prior_ readouts beside the LDA and QDA fits

    Compare its confusion matrix with the LDA one and attribute the differences to the diagonal-covariance assumption.

You're done when

Your Bernoulli implementation matches sklearn feature_log_prob_ to 1e-8, survives unseen values and single-class fits, and you can choose the event model from the feature type alone.

Only when you can do this without notes.

Leads to

Notes

Resources · 4 for this unit, 3 more in the section
  • An Introduction to Statistical Learning with PythonJames, Witten, Hastie, Tibshirani and Taylor · primary

    Use Chapters 3, 4 and 6 with their Python labs as the first pass: least squares and its diagnostics, logistic regression, LDA, QDA and naive Bayes, basis expansion and regularization paths.

  • Dive into Deep Learning - linear models sliceD2L authors · required

    Use 3.1, 3.4, 3.6, 3.7, 4.1, 4.4, 22.7 and 22.9 for linear regression, generalization, weight decay, softmax, maximum likelihood and naive Bayes. Implement with NumPy first and use the PyTorch versions only as a differential reference.

  • scikit-learn - linear models, discriminant analysis and naive Bayesscikit-learn · reference

    Use the official Linear Models, LDA and QDA, and Naive Bayes guides to verify estimator semantics and selected edge cases after the independent implementation works.

  • Tom Mitchell - Generative and Discriminative Classifiers: Naive Bayes and Logistic RegressionTom Mitchell, Carnegie Mellon University · required

    Read Sections 2 (naive Bayes, the smoothed estimates (7) and (9), parameter counts), 3 (Gaussian naive Bayes implies the logistic form, gradient ascent, the penalty, the -class rules) and 4 (generative versus discriminative, Ng and Jordan rates). This is the CS446 reading for Lectures 6-8; rework each derivation on a hand-sized dataset.

Also in this section

  • Hands-On Machine Learning with Scikit-Learn and PyTorch - classification and linear modelsAurelien Geron (O'Reilly, 2025) · support

    Read the Chapter 3 and 4 sections each unit names, then run notebooks 03 and 04 for classification diagnostics, linear and polynomial regression, regularization and logistic regression. Recreate the target mechanism independently before inspecting worked exercise code.

  • NeetCode ML - linear-model closing drillNeetCode · support

    After the section gate, solve Gradient Descent, Sigmoid and ReLU, Softmax, Cross-Entropy Loss, Linear Regression Forward and Linear Regression Training cold. Repeat only failed items from a blank file with changed values.

  • Deep-ML - linear and probabilistic model problemsDeep-ML · support

    Use 15 Linear Regression Using Gradient Descent, 106 Train Logistic Regression with Gradient Descent and 140 Bernoulli Naive Bayes Classifier as unit checks after the local implementation passes. Their tests were read in review; each card states what they pin. The local implementation remains the evidence.