Difference between generative and discriminative model
Generative models learn the full joint probability distribution P(X, Y) = P(X|Y)P(Y), enabling them to synthesize new examples and classify via Bayes' theorem. Discriminative models learn the conditional distribution P(Y|X) or a direct decision boundary without modeling the input distribution P(X), often achieving higher predictive accuracy when labeled data is abundant.
Study Card: Generative vs. Discriminative Models
Direct Answer
Generative and discriminative models differ in what probability distribution they estimate for prediction tasks. Generative models learn the joint distribution (or in unsupervised generation), modeling how features are generated conditioned on each class , and classify via Bayes’ theorem:
Because they model , generative models can sample synthetic data, handle missing features, and detect out-of-distribution inputs. Discriminative models directly learn the conditional posterior (e.g., Logistic Regression, neural classifiers) or map via a decision boundary (e.g., SVMs). By focusing capacity strictly on separating classes without spending parameters modeling , discriminative models typically achieve lower asymptotic classification error when sufficient labeled data is available and generative assumptions (such as Naive Bayes feature independence) do not hold.
Key Terms
- Generative Model: Models the joint distribution (e.g., Naive Bayes, Gaussian Mixture Models, VAEs, Diffusion Models, autoregressive language models).
- Discriminative Model: Models or a decision function directly (e.g., Logistic Regression, SVMs, Random Forests, discriminative CNNs).
- Joint Probability (): Probability of observing input features and label together.
- Conditional Probability (): Posterior probability of label given observed input features .
Example
Consider classifying 1,000 images (800 dogs, 200 cats). A generative model (such as Gaussian Naive Bayes or a class-conditional Gaussian model) learns separate distributions describing what dog feature vectors look like () and what cat feature vectors look like () alongside class priors , then applies Bayes’ rule—and can sample synthetic feature vectors from those distributions. A discriminative model (such as Logistic Regression) ignores how typical dog or cat images look internally and solely optimizes a hyperplane separating cats from dogs.
Code Implementation
import numpy as np
import matplotlib.pyplot as plt
from sklearn.naive_bayes import GaussianNB
from sklearn.linear_model import LogisticRegression
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
# Generate synthetic data for binary classification
X, y = make_classification(
n_samples=1000, n_features=2, n_informative=2,
n_redundant=0, n_clusters_per_class=1, random_state=42
)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.3, random_state=42, stratify=y
)
# Generative Model: Gaussian Naive Bayes (models P(X|Y) and P(Y))
gnb = GaussianNB()
gnb.fit(X_train, y_train)
gnb_accuracy = np.mean(gnb.predict(X_test) == y_test)
print(f"Generative Model (GaussianNB) Accuracy: {gnb_accuracy:.4f}")
# Discriminative Model: Logistic Regression (models P(Y|X) directly)
lr = LogisticRegression()
lr.fit(X_train, y_train)
lr_accuracy = np.mean(lr.predict(X_test) == y_test)
print(f"Discriminative Model (Logistic Regression) Accuracy: {lr_accuracy:.4f}")
# Plot decision boundaries side by side
h = 0.02
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
grid = np.c_[xx.ravel(), yy.ravel()]
plt.figure(figsize=(12, 5))
plt.subplot(1, 2, 1)
Z_gnb = gnb.predict(grid).reshape(xx.shape)
plt.contourf(xx, yy, Z_gnb, cmap=plt.cm.RdBu, alpha=0.8)
plt.scatter(X_train[:, 0], X_train[:, 1], c=y_train, cmap=plt.cm.RdBu, edgecolors='k')
plt.title('Gaussian Naive Bayes (Generative)')
plt.xlabel('Feature 1')
plt.ylabel('Feature 2')
plt.subplot(1, 2, 2)
Z_lr = lr.predict(grid).reshape(xx.shape)
plt.contourf(xx, yy, Z_lr, cmap=plt.cm.RdBu, alpha=0.8)
plt.scatter(X_train[:, 0], X_train[:, 1], c=y_train, cmap=plt.cm.RdBu, edgecolors='k')
plt.title('Logistic Regression (Discriminative)')
plt.xlabel('Feature 1')
plt.ylabel('Feature 2')
plt.tight_layout()
plt.show()
Related Concepts
- Ng & Jordan (2001) Sample Complexity Tradeoff: When comparing Naive Bayes (generative) and Logistic Regression (discriminative), Naive Bayes converges faster to its asymptotic error ( samples), whereas Logistic Regression typically achieves lower asymptotic error once training data is large enough.
- Semi-Supervised Learning & Unlabeled Data: Generative models can naturally exploit unlabeled inputs via EM or latent-variable objectives to refine and improve class separation when labels are scarce.
- Calibration & Out-of-Distribution Detection: Because generative models explicitly estimate , low likelihood signals out-of-distribution inputs, whereas discriminative classifiers can be overconfident far from the training manifold.
- Modern Deep Generative Models: VAEs, GANs, Diffusion Models, and autoregressive Transformers learn rich data distributions or for text, image, and audio synthesis.