Sub topic 4
The naive assumption: features independent given the class
This chapter assumes you know joint and conditional probability, Bayes' theorem, and the idea of modeling P(x | y) and P(y). Here you will see the one assumption that makes naive Bayes practical.
The problem with the joint likelihood
Bayes' theorem says the posterior for class y is proportional to P(y) · P(x1, x2, ..., xn | y). The hard part is the joint likelihood P(x1, ..., xn | y). With n binary features, a full table needs 2^n - 1 numbers per class. You would also need enough training data to see most feature combinations, and you won't.
The assumption
Naive Bayes assumes that, once you know the class, the features do not carry information about each other:
P(x1, ..., xn | y) = P(x1 | y) · P(x2 | y) · ... · P(xn | y)
This is conditional independence. It is weaker than plain independence. In a spam filter, "free" and "prize" appear together far more often than chance across all emails, so they are dependent overall. The assumption says that inside the spam class, seeing "free" tells you nothing extra about whether "prize" appears.
What it buys you
Each factor P(xi | y) is a one-dimensional distribution. You estimate it from counts or a mean and variance for that feature alone. The parameter count drops from exponential to linear in n.
def full_joint_params(n_features, n_classes):
return n_classes * (2 ** n_features - 1)
def naive_params(n_features, n_classes):
return n_classes * n_features
for n in (5, 20, 50):
print(n, full_joint_params(n, 2), naive_params(n, 2))For 50 binary features and 2 classes, the full table needs about 2.25 quadrillion likelihood parameters. The naive model needs 100.
Resulting decision rule
Put the factored likelihood into Bayes' theorem. The evidence term P(x) is the same for every class, so drop it:
y_hat = argmax over y of P(y) · P(x1 | y) · ... · P(xn | y)
The next chapter covers this rule in detail.
Common errors
- Confusing "independent given the class" with "independent." The model never claims features are unrelated overall.
- Assuming the assumption holds. Duplicated or highly correlated features, such as "New" and "York," count the same evidence twice and push the score too far.
- Expecting a bad assumption to always hurt accuracy. The ranking of classes often survives. The probability values themselves suffer more, which a later chapter explains.
Exercise
A model has 10 binary features and 3 classes. How many likelihood parameters does a full joint model need, and how many does naive Bayes need? Ignore the class priors.
Show answer
The full joint model needs 3 × (2^10 − 1) = 3 × 1023 = 3069 parameters. Naive Bayes needs 3 × 10 = 30 parameters.