← capítulo

Naive Bayes

Score a patient against a statistical portrait of each class. Least surprising class wins.

Bayes' theorem, 1763. The "naive" shortcut, 1960s. Still the baseline spam filter to beat.

The whole idea

The data

768 Pima patients, eight measurements, a diabetes label. All eight in play.

Below: glucose vs BMI. The clouds overlap, but the diabetic cloud leans up and right.

The whole model

Bayes' rule, drop the constant denominator:

P(yx)    P(y)P(xy)P(y \mid x) \;\propto\; P(y)\, P(x \mid y)

Naive assumption — features independent given the class:

P(yx)    P(y)j=1DP(xjy)P(y \mid x) \;\propto\; P(y) \prod_{j=1}^{D} P(x_j \mid y)

Gaussian likelihood per feature, then argmax in log-space:

y^=argmaxc  [logP(c)+jlogP(xjc)]\hat{y} = \arg\max_{c}\; \left[\log P(c) + \sum_{j} \log P(x_j \mid c)\right]

The model, made visible

The fitted bell curves — one per class per feature. This is all the model stores.

Glucose separates cleanly; BMI overlaps. Naive Bayes weights each by its separation.

Watch the posterior tip

Each test patient scored against both Gaussians. Rings are 1σ and 2σ; the bar is the verdict.

Colored by prediction, red-ringed when wrong. The mistakes cluster in the overlap.

Low glucose: pinned near 0.95 healthy. Climb into the seam and the bar crosses 0.5 — the boundary is where the two bells pull equally.

Scratch vs. library

GaussianNB is the same model. Both score 0.7727 and agree on all 154 test patients.

Not "close to" the library — bit-for-bit the same fitted parameters.

Takeaways