COMP4318 · Machine Learning and Data Mining
Naïve Bayes & Evaluating ML Methods
Week 4 builds the naïve Bayes classifier from Bayes' theorem and the conditional-independence assumption, then covers how to evaluate any classifier: the confusion matrix, accuracy/precision/recall/F1, and cross-validation. Both are heavily hand-computed, so both are heavily examined — expect a naïve Bayes posterior (often with a Gaussian density for a numeric attribute) and a confusion-matrix metrics question. The homework quiz (h4) drills the same.
What this chapter covers
- 01Bayes' theorem: P(H|E) = P(E|H)·P(H) / P(E), with H = class and E = evidence
- 02Naïve Bayes assumptions: attributes conditionally independent given the class, and all equally important
- 03Classification: P(H|E) ∝ P(E1|H)·…·P(Ek|H)·P(H); pick the class with the largest product
- 04Zero-frequency problem and the Laplace / m-estimate smoothing fix
- 05Gaussian naïve Bayes for numeric attributes via the normal PDF f(x) = 1/(σ√(2π))·e^(−(x−μ)²/(2σ²))
- 06Confusion matrix; accuracy, precision = tp/(tp+fp), recall = tp/(tp+fn), F1 = 2PR/(P+R)
- 07Holdout, stratification, a validation set for hyperparameters, k-fold and leave-one-out cross-validation
- 08Grid search with cross-validation for tuning (note: ROC/AUC is not spelled out in the available slides — confirm examinability on Canvas)
A naïve Bayes posterior with two attributes
- +1Priors from the class counts: P(spam) = 6/10 = 0.6, P(ham) = 4/10 = 0.4.
- +1Conditional (likelihood) probabilities: P(free|spam) = 4/6 = 0.667, P(link|spam) = 5/6 = 0.833; P(free|ham) = 1/4 = 0.25, P(link|ham) = 1/4 = 0.25.
- +1Naïve Bayes score for spam (attributes assumed conditionally independent given the class): P(spam|E) ∝ 0.667·0.833·0.6 = 0.333.
- +1Score for ham: P(ham|E) ∝ 0.25·0.25·0.4 = 0.025.
- +1Normalise by the sum: P(spam|E) = 0.333/(0.333 + 0.025) = 0.93 > 0.5 → classify as spam.
Key terms
- Bayes' theorem
- P(H|E) = P(E|H)·P(H)/P(E); it inverts a likelihood P(E|H) and a prior P(H) into a posterior P(H|E) over classes.
- Naïve Bayes
- A classifier that assumes attributes are conditionally independent given the class, so P(H|E) ∝ P(H)·ΠP(Eᵢ|H); assign the class with the largest product.
- Zero-frequency / Laplace smoothing
- If an attribute value never co-occurs with a class its conditional probability is 0 and zeroes the product; the fix adds 1 to the numerator (and the count of attribute values to the denominator).
- Confusion matrix
- A 2×2 (or larger) table of true/false positives and negatives that all evaluation metrics are read from.
- Precision & recall
- Precision = tp/(tp+fp) (of the predicted positives, how many are right); recall = tp/(tp+fn) (of the actual positives, how many were found); F1 = 2PR/(P+R) balances them.
- k-fold cross-validation
- Split the data into k folds, train on k−1 and test on the held-out fold in turn, and average the k accuracies; stratified 10-fold is the ML standard.
Naïve Bayes & Evaluating ML Methods FAQ
Why is naïve Bayes called 'naïve'?
Because it makes the simplifying (often unrealistic) assumption that the attributes are conditionally independent given the class, so their probabilities can be multiplied. It also treats all attributes as equally important. Despite the assumption it works surprisingly well, is fast (one scan of the data) and is robust to isolated noise; strongly correlated attributes are what hurt it.
Precision vs recall — which is which?
Precision = tp/(tp+fp) asks: of everything I predicted positive, what fraction was actually positive (few false alarms). Recall = tp/(tp+fn) asks: of everything that is actually positive, what fraction did I catch (few misses). Do not swap the denominators — that is the single most common lost mark. F1 = 2PR/(P+R) is their harmonic balance.
What is k-fold cross-validation and why not just tune on the test set?
k-fold CV splits the training data into k folds, trains on k−1 and validates on the remaining fold in turn, then averages — giving a more stable estimate than a single split. You never tune hyperparameters on the test set, because doing so leaks information and inflates your reported accuracy; use a validation set or CV for tuning and keep the test set for the final estimate.
Is ROC/AUC examinable in COMP4318?
The lecture materials teach the confusion matrix and accuracy/precision/recall/F1 explicitly, but ROC curves and AUC are not spelled out in them, so treat ROC/AUC as unconfirmed for the exam. Master the confusion-matrix metrics first, and confirm on Canvas whether ROC/AUC is in scope for your offering before relying on it.
Exam move
Split your practice in two. For naïve Bayes, rehearse the routine: priors from class counts, conditional probabilities per attribute, multiply within each class (with the prior), then normalise and pick the larger — and always keep the Laplace-smoothing fix ready for a zero. Practise one Gaussian-NB numeric attribute using the normal PDF with per-class μ and σ. For evaluation, build a confusion matrix from stated counts and compute all four metrics, stating the positive class first. Rehearse explaining k-fold CV and why the test set is off-limits for tuning. When precision and recall blur, ask Sia to rebuild the matrix with fresh counts and check each metric.
Working through Naïve Bayes & Evaluating ML Methods in COMP4318? Sia is AskSia’s AI Machine Learning tutor — ask any COMP4318 Naïve Bayes & Evaluating ML Methods question and get a clear, step-by-step explanation grounded in how COMP4318 is taught and assessed. Read this chapter free, then take your hardest questions to Sia.