Key points, notes and past questions
Naive Bayes and generative models
Bayes classifier, parameters, smoothing, Gaussian NB
- Orientation: what you will learn and why
- Big picture: concept map, comparison table and the recipe
- Start here: the probability you need, from zero
- $P(A|B)=P(A,B)/P(B)$.
- Product rule: $P(A,B)=P(B)P(A|B)=P(A)P(B|A)$.
- Bayes: $P(y|x)=P(x|y)P(y)/P(x)$.
- Independence: $P(A,B)=P(A)P(B)$. Conditional independence is the same, given a class.
- $X$ is $(d,n)$: columns are data points.
- Generative versus discriminative models
- Generative: learns $P(y)$ and $P(x|y)$ (the joint), then classifies with Bayes' rule.
- Discriminative: learns $P(y|x)$ or the boundary directly.
- Naive Bayes is generative.
- The Bayes classifier and the posterior
- $P(c|x)=P(x,c)/\sum_{c'}P(x,c')$.
- Binary rule: predict 1 iff $P(y{=}1)P(x|1)\ge P(y{=}0)P(x|0)$. The tie rule is given in the question.
- $P(x)$ cancels when comparing classes.
- With uniform or Gaussian class densities, compare densities times priors.
- Bayes error is how often even the best rule is wrong.
- Counting the parameters of a model
- Full joint: $K\cdot2^d-1$ ($K$ classes, $d$ binary features).
- Naive Bayes, binary features: $(K-1)+Kd$.
- A Gaussian feature in a class has two numbers: mean and variance.
- If the question says the variances are equal, count that variance once.
- Probabilities that must sum to 1 lose one free number.
- The Naive Bayes model and the naive assumption
- $P(x|y)=\prod_{j=1}^dP(x_j|y)$.
- Independence is <b>given the class</b>, not unconditional.
- Match the model to the feature type: Bernoulli for binary, categorical for categories, Gaussian for real numbers.
- Estimating the parameters by counting (MLE)
- Prior: $\hat p=n_1/n$, the class proportion.
- Feature given class: $\hat p_j^{\,y}=k_{jy}/n_y$, where $k_{jy}$ counts class-$y$ points with feature $j$ equal to 1.
- Count within the class, so the denominator is the class size $n_y$, not $n$.
- If points carry weights, add the weights instead of counting.
- Predicting with Naive Bayes: scores, joint and posterior
- Score of class $y$: $P(y)\prod_jP(x_j|y)$.
- For a binary feature use $\hat p$ if the value is 1 and $1-\hat p$ if it is 0.
- Predict the class with the largest score.
- Posterior = score ÷ sum of all class scores.
- Laplace smoothing: removing impossible zeros
- Binary feature: $\hat p=(k+1)/(n_y+2)$.
- $m$ possible values: $(k+1)/(n_y+m)$. With strength $\alpha$: $(k+\alpha)/(n_y+m\alpha)$.
- Smoothing removes impossible zeros and pulls estimates toward uniform.
- Read whether the question wants the plain or the smoothed estimate.
- Gaussian Naive Bayes and mixed features
- $\hat\mu=\frac1{n_c}\sum x$ and $\hat\sigma^2=\frac1{n_c}\sum(x-\hat\mu)^2$, per class and per feature.
- Divide by $n_c$, not $n_c-1$.
- $N(\mu,\sigma^2)$: the second number is the variance, not the standard deviation.
- A density can be larger than 1 because it is a height.
- Mixed features: multiply Bernoulli terms for binary features and Gaussian densities for real ones.