~/blog

Instance-Based vs Model-Based Learning

Jun 25, 20266 min readBy Mohammed Vasim
Machine LearningAIData Science

You're building a real-time loan approval system. Every application needs a credit decision in under 100 milliseconds. You train a logistic regression model — a few hundred coefficients, sub-millisecond inference. Now a colleague suggests K-Nearest Neighbors instead: no training, just store all past applications. Inference now takes hundreds of milliseconds, and you discover a key difference: logistic regression compressed the training data into a compact formula; KNN kept every row.

That choice — throw the data away vs keep it — separates model-based learning from instance-based learning. Getting it wrong means deploying a system that's either too slow to serve or too rigid to capture the real pattern.

What "Learning" Means Here

Mitchell's definition: a program is said to learn from experience E with respect to task T and performance measure P, if its performance at T, as measured by P, improves with experience E. For house price prediction: E = 6 training examples, T = predict price, P = MSE.

The key question isn't whether it learns — it's what it keeps from that experience. Does it compress the data into a compact model, or does it keep the raw examples?

Anchor dataset:

python
X = [650, 850, 1100, 1400, 1600, 1900]  # sq_ft
y = [180, 220, 280,  340,  370,  430]   # price in $k
# Query: predict price for sq_ft = 1000

Model-Based Learning

Model-based (eager) learning fits a parametric function to the training data and extracts a compact summary: the parameters. Once training ends, the training data can be discarded.

Linear regression on our 6-point anchor yields , . At inference:

The six training rows are gone. Only two numbers remain — and . For a second query:

Inference time is — a single dot product. Memory is — just the weight vector. For features, that's 1000 numbers regardless of whether the training set had 100 or 100 million samples.

Algorithms in this class: Linear Regression, Logistic Regression, SVM, Neural Networks, Naive Bayes, Decision Trees (once built).

Instance-Based Learning (Lazy Learning)

Instance-based (lazy) learning memorizes the entire training set. There is no fitting phase — the "training" step is just storing the data. All computation is deferred to inference.

KNN () on the same anchor for query sq_ft = 1000:

Distances to each training point:

Training sq_ftDistance from 1000Price
650|1000 − 650| = 350180
850|1000 − 850| = 150 ✓220
1100|1000 − 1100| = 100 ✓280
1400|1000 − 1400| = 400340
1600|1000 − 1600| = 600370
1900|1000 − 1900| = 900430

Two nearest: sq_ft = 850 (price 220) and sq_ft = 1100 (price 280).

For a second query, sq_ft = 800:

Nearest neighbors: 650 (price 180) and 850 (price 220).

Linear regression gave $213.3k for this same query. These are different predictions — not by coincidence, but structurally. KNN interpolates locally from the two closest neighbors. Linear regression fits a single global line. On data that isn't perfectly linear, they will consistently disagree in regions far from training points.

Inference time is — compute distance to every stored training point. Memory is — all training data must be kept. At million samples, that's expensive.

Eager (Model-Based) Lazy (Instance-Based) Train: Fit w₀, w₁ (slow) Train: store instant O(1) Infer: ŷ = w·x fast O(p) Infer: search all n points slow O(n) Memory: O(p) Memory: O(n) training rows

When the Difference Matters: Four Scenarios

1. Large dataset, real-time inference (loan approval at a bank, ): KNN must compute distances per query — hundreds of milliseconds per decision. Use model-based (logistic regression). Inference is one dot product — sub-millisecond.

2. Streaming data that changes over time (user preference prediction): Instance-based wins — append new examples without retraining. Model-based requires periodic full retrains, which may take hours for large models.

3. Non-linear local patterns (housing prices by neighborhood): KNN captures the local cluster around each query point. A single global linear model may underfit neighborhoods that don't follow the citywide trend.

4. Interpretability required (medical diagnosis): Model-based (logistic regression, decision tree) — the physician can inspect the coefficients or rules. KNN offers no such explanation: "your nearest neighbors voted default" isn't useful.

Generalization: The Core Tradeoff

Model-based generalizes via the parametric assumption. If the true relationship is linear and you have very little data, a linear model generalizes from 3 points to any . The downside: if the assumption is wrong, it's wrong everywhere — a systematic global error.

Instance-based generalizes by similarity — a new point inherits the labels of its nearest training points. No assumption about the global shape. The downside: in high dimensions, "nearest" stops being meaningful. When , two training points can be the "closest" while still being geometrically far away — a problem called the curse of dimensionality.

Comparison Table

AspectModel-BasedInstance-Based
Training phaseFits parameters Stores data (no fitting)
Inference cost — constant — grows with data
Memory cost — compact — grows with data
AssumptionsGlobal: data follows a parametric formLocal: nearby points are similar
Adapts to new dataRequires retrainingJust add new row to store
Interpretable?Yes — inspect weightsNo — result depends on neighbors
Handles local patterns?Poorly (single global fit)Yes — local shape captured

Decision Guide

ConditionPrefer
Fast inference neededModel-based
Training data changes frequentlyInstance-based
Data has global linear/polynomial structureModel-based
Data has local clusters or non-linear patternsInstance-based
High dimensionality ()Model-based
Small dataset, low dimensionalityEither

The model-vs-instance distinction feeds directly into the hyperparameter tradeoffs in this series: polynomial degree (post 13) controls model complexity for linear regression just as controls KNN's local vs global behavior. Both are forms of the bias-variance tradeoff, explored in post 10.

A common mistake is assuming instance-based methods beat the curse of dimensionality. They don't — they just fail differently. A linear model with produces infinite possible weight vectors; KNN with produces nearest neighbors that are still geometrically far. The high-dimension problem is structural, not algorithmic — which is why feature engineering and dimensionality reduction are prerequisites, not optional improvements.

Test Your Understanding

  1. For the anchor dataset, compute KNN () predictions for sq_ft = 1250. Then compute the linear regression prediction for the same query. Which is higher, and why does the difference arise?

  2. You add a new training sample (sq_ft = 1050, price = 265) to the dataset. How does each approach handle this? Which requires more work?

  3. An instance-based model "memorizes" training data exactly. Can it overfit? What would overfitting look like for ?

  4. For a 100-feature dataset with samples, you're choosing between logistic regression (model-based) and KNN (instance-based). What factors push you toward logistic regression?

  5. Why does the KNN inference cost not depend on the number of features , while a linear model's inference cost does? Which grows faster with scale, and when does the crossover matter?

Comments (0)

No comments yet. Be the first to comment!

Leave a comment