Docsity

CS 229 — Lecture Notes on Generative Learning Algorithms, Exams of Nursing

Toggle sidebar
bg1
CS 229 — Lecture Notes on
Generative Learning Algorithms
Course: CS 229 — Machine Learning
Topic: Generative Learning Algorithms
Official notes: CS 229 Lecture Notes 2 — Generative Learning Algorithmscs229.stanford
Table of Contents
1. Discriminative versus Generative Models
2. Bayes Rule and Classification
3. Gaussian Discriminant Analysis
4. GDA Parameter Estimation
5. GDA versus Logistic Regression
6. Naive Bayes
7. Laplace Smoothing
8. Worked Example
9. Official Resources
Discriminative versus Generative Models
Discriminative models
A discriminative learning algorithm models the conditional distribution:
p(y∣x;θ)p(y \mid x; \theta)p(y∣x;θ)
or learns a direct mapping from inputs xxx to labels yyy. Examples include logistic regression
and the perceptron algorithm.cs229.stanford+1
For classification, a discriminative model focuses on the decision boundary between classes.
Generative models
A generative learning algorithm models:
p(x∣y)p(x \mid y)p(x∣y)
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download CS 229 — Lecture Notes on Generative Learning Algorithms and more Exams Nursing in PDF only on Docsity!

CS 229 — Lecture Notes on

Generative Learning Algorithms

Course: CS 229 — Machine Learning Topic: Generative Learning Algorithms Official notes: CS 229 Lecture Notes 2 — Generative Learning Algorithmscs229.stanford

Table of Contents

  1. Discriminative versus Generative Models
  2. Bayes Rule and Classification
  3. Gaussian Discriminant Analysis
  4. GDA Parameter Estimation
  5. GDA versus Logistic Regression
  6. Naive Bayes
  7. Laplace Smoothing
  8. Worked Example
  9. Official Resources

Discriminative versus Generative Models

Discriminative models

A discriminative learning algorithm models the conditional distribution:

p(y∣x;θ)p(y \mid x; \theta)p(y∣x;θ)

or learns a direct mapping from inputs xxx to labels yyy. Examples include logistic regression and the perceptron algorithm.cs229.stanford+

For classification, a discriminative model focuses on the decision boundary between classes.

Generative models

A generative learning algorithm models:

p(x∣y)p(x \mid y)p(x∣y)

and:

p(y)p(y)p(y)

It then uses Bayes rule to derive:

p(y∣x)=p(x∣y)p(y)p(x)p(y \mid x) = \frac{p(x \mid y)p(y)}{p(x)}p(y∣x)=p(x)p(x∣y)p(y)

cs229.stanford+

In other words, a generative model learns how each class generates its inputs. For example, if y=0y = 0y=0 means dog and y=1y = 1y=1 means elephant, then p(x∣y=0)p(x \mid y = 0)p(x∣y=0) models the distribution of dog features, while p(x∣y=1)p(x \mid y = 1)p(x∣y=1) models the distribution of elephant features.cs229.stanford

Comparison

Feature Discriminative model Generative model

Models p(y∣x)p(y \mid x)p(y∣x) p(x∣y)p(x \mid y)p(x∣y) and p(y)p(y)p(y)

Learns Decision boundary Class-conditional distributions

Examples Logistic regression, perceptron GDA, Naive Bayes

Can generate samples? Usually no Yes

Uses Bayes rule for prediction? Not necessarily Yes

Stanford’s midterm-review material summarizes this distinction: discriminative models directly estimate p(y∣x)p(y \mid x)p(y∣x), while generative models estimate p(x∣y)p(x \mid y)p(x∣y) and infer p(y∣x)p(y \mid x)p(y∣x) using Bayes rule.cs229.stanford

Bayes Rule and Classification

For binary classification, generative models learn:

p(y=1),p(y=0),p(x∣y=1),p(x∣y=0)p(y = 1), \qquad p(y = 0), \qquad p(x \mid y = 1), \qquad p(x \mid y = 0)p(y=1),p(y=0),p(x∣y=1),p(x∣y=0)

Then, for a new input xxx, compute:

p(y=1∣x)=p(x∣y=1)p(y=1)p(x)p(y = 1 \mid x) = \frac{p(x \mid y = 1)p(y = 1)}{p(x)}p(y=1∣x)=p(x)p(x∣y=1)p(y=1)

p(x∣y)=1(2π)d/2∣Σ∣1/2exp (−12(x−μy)TΣ−1(x−μy))p(x \mid y) = \frac{1}{(2\pi)^{d/2}|\Sigma|^{1/2}} \exp \left( -\frac{1}{2}(x - \mu_y)^T\Sigma^{-1}(x - \mu_y) \right)p(x∣y)=(2π)d/2∣Σ∣1/21exp(−21(x−μy)TΣ−1(x−μy))

cs229.stanford+

GDA Parameter Estimation

GDA parameters are estimated by maximizing the joint likelihood:

ℓ(ϕ,μ0,μ1,Σ)=∑i=1mlogp(x(i),y(i)) \ell(\phi, \mu_0, \mu_1, \Sigma) = \sum_{i=1}^{m} \log p(x^{(i)}, y^{(i)})ℓ(ϕ,μ0,μ1,Σ)=i=1∑mlogp(x(i),y(i))

Equivalently:

ℓ=∑i=1m[logp(x(i) ∣y(i))+log p(y(i))]\ell = \sum_{i=1}^{m} \left[ \log p(x^{(i)} \mid y^{(i)}) + \log p(y^{(i)}) \right]ℓ=i=1∑m[logp(x(i)∣y(i))+logp(y(i))]

cs229.stanford+

Maximum-likelihood estimates

For a training set of mmm examples:

ϕ=1m∑i=1m1{y(i)=1}\phi = \frac{1}{m} \sum_{i=1}^{m} \mathbf{1}{y^{(i)} = 1}ϕ=m i=1∑m1{y(i)=1} μy=∑i=1m1{y(i)=y}x(i)∑i=1m1{y(i)=y}\mu_y = \frac{ \sum_{i=1}^{m} \mathbf{1}{y^{(i)} = y}x^{(i)} }{ \sum_{i=1}^{m} \mathbf{1}{y^{(i)} = y} }μy=∑i=1m1{y(i)=y}∑i=1m 1{y(i)=y}x(i) Σ=1m∑i=1m(x(i)−μy(i))(x(i)−μy(i))T\Sigma = \frac{1}{m} \sum_{i=1}^{m} \left(x^{(i)} - \mu_{y^{(i)}}\right) \left(x^{(i)} - \mu_{y^{(i)}}\right)^TΣ=m1i=1∑m(x(i)−μy(i))(x(i)−μy(i))T

cs229.stanford

Interpretation

 ϕ\phiϕ estimates the prior probability of class 1.  μ0\mu_0μ0 is the sample mean of inputs with label 0.  μ1\mu_1μ1 is the sample mean of inputs with label 1.  Σ\SigmaΣ is the pooled covariance matrix across both classes.

GDA versus Logistic Regression

GDA and logistic regression often produce similar decision boundaries, but they make different modeling assumptions.cs229.stanford+

Feature GDA Logistic regression

Model type Generative Discriminative

Models p(x∣y)p(x \mid y)p(x∣y), p(y)p(y)p(y) p(y∣x)p(y \mid x)p(y∣x)

Feature type Typically continuous Continuous or transformed features

Distributional assumption Gaussian class-conditionals^ No Gaussian assumption

Decision boundary Linear when Σ\SigmaΣ is shared Linear

Asymptotic behavior Can be more efficient when assumptions hold

Often more robust when assumptions fail

Important theoretical result

If p(x∣y)p(x \mid y)p(x∣y) is multivariate Gaussian with a shared covariance matrix Σ\SigmaΣ, then:

p(y=1∣x)p(y = 1 \mid x)p(y=1∣x)

has the logistic form:

p(y=1∣x)=11+e−(θTx)p(y = 1 \mid x) = \frac{1}{1 + e^{-(\theta^T x)}}p(y=1∣x)=1+e−(θTx)

Thus, GDA implies logistic regression, but logistic regression does not imply GDA.cs229.stanford+

Practical guidance

 Use GDA when the Gaussian assumption is plausible and the dataset is small.  Use logistic regression when the Gaussian assumption may be violated or when robustness matters more.

Stanford notes that GDA makes stronger assumptions and can be more data-efficient when those assumptions are correct.cs229.stanford+

Naive Bayes

Laplace Smoothing

Naive Bayes can assign zero probability to an unseen feature-class combination. This is problematic because multiplying by zero makes the entire posterior probability zero, regardless of other evidence.

Problem

Suppose a word never appears in the training spam emails. Then maximum likelihood gives:

p(xj=1∣y=1)=0p(x_j = 1 \mid y = 1) = 0p(xj=1∣y=1)=

If that word appears in a test email predicted to be spam, the product becomes zero.

Laplace-smoothed estimate

For a binary feature xj∈{0,1}x_j \in {0, 1}xj∈{0,1}, replace the maximum-likelihood estimate with:

p(xj=1∣y)=∑i=1m1{xj(i)=1,y(i)=y}+1∑i=1m1{y(i)=y}+2p(x_j = 1 \mid y) = \frac{ \sum_{i=1}^{m} \mathbf{1}{x_j^{(i)} = 1, y^{(i)} = y} + 1 }{ \sum_{i=1}^{m} \mathbf{1}{y^{(i)} = y} + 2 }p(xj=1∣y)=∑i=1m1{y(i)=y}+2∑i=1m1{xj(i)=1,y(i)=y}+

cs229.stanford+

The +1+1+1 in the numerator and +2+2+2 in the denominator correspond to one pseudo-count for each possible binary value.

General form

For a feature taking kkk possible values:

p(xj=s∣y)=∑i=1m1{xj(i)=s,y(i)=y}+1∑i=1m1{y(i)=y}+kp(x_j = s \mid y) = \frac{ \sum_{i=1}^{m} \mathbf{1}{x_j^{(i)} = s, y^{(i)} = y} + 1 }{ \sum_{i=1}^{m} \mathbf{1}{y^{(i)} = y} + k }p(xj=s∣y)=∑i=1m1{y(i)=y}+k∑i=1m1{xj(i)=s,y(i)=y}+

Laplace smoothing prevents zero probabilities and improves generalization when some feature values are rare.cs229.stanford+

Worked Example

Suppose a tiny spam dataset has 4 spam emails and 6 non-spam emails.

Then:

p(y=1)=410=0.4p(y = 1) = \frac{4}{10} = 0.4p(y=1)=104=0. p(y=0)=610=0.6p(y = 0) = \frac{6}{10} = 0.6p(y=0)=106=0.

Suppose the word “free” appears in 3 of the 4 spam emails and in 1 of the 6 non-spam emails.

Without smoothing:

p(free=1∣y=1)=34p(\text{free} = 1 \mid y = 1) = \frac{3}{4}p(free=1∣y=1)= p(free=1∣y=0)=16p(\text{free} = 1 \mid y = 0) = \frac{1}{6}p(free=1∣y=0)=

With Laplace smoothing:

p(free=1∣y=1)=3+14+2=46p(\text{free} = 1 \mid y = 1) = \frac{3 + 1}{4 + 2} = \frac{4}{6}p(free=1∣y=1)=4+23+1= p(free=1∣y=0)=1+16+2=28p(\text{free} = 1 \mid y = 0) = \frac{1 + 1}{6 + 2} = \frac{2}{8}p(free=1∣y=0)=6+21+1=

Laplace smoothing prevents any feature-class count of zero from forcing the entire posterior probability to zero.cs229.stanford+

Official Resources

Resource Contents

Resource Contents

CS 229 Lecture Notes 2

Generative learning algorithms, GDA, Naive Bayes, Laplace smoothing. cs229.stanford

CS 229 main notes, Spring 2026

Chapter 4: Generative learning algorithms. cs229.stanford

CS 229

materials page

Index to official lecture notes, including Generative Algorithms. cs229.stanford

CS 229

lecture page

Topics: GDA, GDA versus logistic regression, Naive Bayes, Laplace smoothing. see.stanford

CS 229 midterm-

Compact review of discriminative versus generative models, GDA, Naive Bayes, and smoothing. cs229.stanford