Module 0 — Foundations of Machine Learning

Source: Lec 2 — Introduction to Machine Learning. · Next: Module 1 — Decision Trees · Index: 00_INDEX_and_Coverage

Notation used throughout the course

  • Data point: ( features).
  • = number of training instances. = class label (or target). = test point.
  • Labels: logistic regression uses ; SVM and perceptron use .

How each concept is laid out

Intuition → definition → maths → insights → 5 worked examples (very easy → very hard) → mistakes → exam traps → connections → checklist. Each hard example has a collapsible Python check so you can reproduce the answer yourself.


C1. What Is Machine Learning? (Samuel, Mitchell, the three paradigms)

Intuition

A normal program = rules written by a human. A learning program = rules extracted from examples. A child learns “dog” by meeting many dogs, not by reading a definition. ML is that, but with a score you can measure.

flowchart LR
    D[Data] --> Q{What feedback<br/>does the learner get?}
    Q -->|correct answer<br/>per example| S[Supervised<br/>classification · regression]
    Q -->|no answers| U[Unsupervised<br/>clustering · density estimation]
    Q -->|delayed reward| R[Reinforcement<br/>mentioned only]

Formal definitions

  • Samuel (1959): ML is the field of study that gives computers the ability to learn without being explicitly programmed.
  • Mitchell (1997): A program learns from experience with respect to a task and performance measure if its performance on , measured by , improves with experience .
  • Paradigms: Supervised (labelled data), Unsupervised (no labels), Reinforcement (reward signal; mentioned, not studied in this course).

Mathematical formulation

Let denote performance on task after experience . Learning with respect to means: for experiences ,

Derivation / reasoning

Why must be measured on unseen data? If is measured on the training set, a lookup table gets a perfect score with zero “understanding”. Mitchell’s definition is meaningful only when reflects the task in general, i.e. on new instances. This is the seed of the whole generalisation / overfitting theme (C7, C13).

Key insights

  1. Always identify the triple before choosing an algorithm.
  2. determines the paradigm: labelled → supervised; unlabelled → unsupervised.
  3. “Improves with experience” is a testable, quantitative statement.

Worked examples

Example 1 — Very Easy · The spam filter

  • Problem: An email app learns to flag spam by watching which emails the user drags to the spam folder. Identify , , .
  • Asked: The three components.
  • Concept: Mitchell’s definition.
  • Approach: Task = what the program does; experience = what it watches; performance = how success is scored.
  • Solution: = classify incoming emails as spam / not spam. = a database of emails with the user’s spam / not-spam labels. = fraction of new emails classified correctly.
  • Final answer: : spam classification; : labelled emails; : accuracy on new emails.

Key lesson

The three parts are three different objects; don’t merge them.

Example 2 — Easy · Name the paradigm

  • Problem: Label each as supervised, unsupervised or reinforcement: (a) predicting house prices from past sales with sale prices; (b) a shop grouping customers by purchase behaviour, no labels; (c) a robot learning to walk, receiving when it moves forward and when it falls; (d) tagging photos using 10 000 photos already tagged by humans.
  • Asked: Paradigm for each.
  • Concept: Paradigms are defined by the type of feedback.
  • Approach: Ask “what feedback does the learner receive?”
  • Solution: (a) Each training house has its price (the target) → supervised (regression). (b) No target → unsupervised (clustering). (c) Feedback is a delayed scalar reward, not a correct answer → reinforcement. (d) Human tags are the answer key → supervised (classification).
  • Final answer: (a) supervised, (b) unsupervised, (c) reinforcement, (d) supervised.

Key lesson

Supervision = a correct answer per example. A reward only says “good/bad”, not “what you should have done”.

Example 3 — Moderate · The digit recogniser’s learning curve

  • Problem: A handwritten-digit program is evaluated on a fixed test set of 1000 images. After training on images its test accuracy is 71 %; after images, 88 %; after images, 87.5 %. Does it “learn” per Mitchell? Over which range?
  • Asked: Whether improves with .
  • Concept: Mitchell’s definition (improvement, not necessarily monotone forever).
  • Approach: Compare at successive experience levels, and compare each difference with sampling noise.
  • Solution: , , . : clear improvement. : a tiny decline, smaller than one standard error of a 1000-image test set, .
  • Final answer: Yes, it learns from 500 → 2000 images; from 2000 → 8000 the change is statistically indistinguishable from zero (performance has plateaued).
accuracy
 90% |            ●────────●   ← plateau (difference < 1 s.e.)
 80% |
 70% |   ●
     +---+--------+--------+---→ training images
        500     2000     8000

Key lesson

Learning curves saturate. Compare differences with sampling noise before claiming “worse”.

Example 4 — Hard · Memoriser vs generaliser

  • Problem: Two programs are trained on the same 1000 labelled digit images. Program A stores every training image with its label; on a test image it returns the stored label if an identical image exists, otherwise it guesses “0”. Program B fits a smooth classifier. Results — A: train 100 %, test 10 %. B: train 94 %, test 92 %. Which has learned in Mitchell’s sense, and what should be?
  • Asked: Which program learned; what must measure.
  • Concept: must be measured on unseen instances of .
  • Approach: Evaluate the two programs under both choices of .
  • Solution: (i) If = training accuracy: A scores 100 %, B 94 % → A “wins” — absurd, A does not generalise. (ii) If = test accuracy: A = 10 % (a random guess among 10 digits), B = 92 %. A improves with experience only if a test image happens to equal a stored one; for new handwriting, no. B improves with experience.
  • Final answer: B has learned; the correct is accuracy on unseen instances.

Key lesson

Memorisation is not learning; generalisation is the point.

Example 5 — Very Hard · The leaking test set

  • Problem: A team reports accuracy on a fixed 200-point test set for training sizes . Program X: . Program Y: . Program Z: . Z’s test set accidentally contains 20 points that also appear in the training set once . (a) Which programs learn? (b) What is Z’s accuracy on the non-leaked 180 test points at , assuming Z gets all 20 leaked points right?
  • Asked: Learning judgement and corrected accuracy.
  • Concept: Mitchell’s definition + leakage invalidates .
  • Approach: (a) look at the trend; (b) remove leaked points from numerator and denominator.
  • Solution: (a) X: strictly increasing → learns. Y: constant → does not improve → not learning (a fixed rule). Z: noisy and non-monotone, . (b) Correct answers at : . Remove the 20 leaked (all correct): correct among clean points . At there is no leakage: . So clean accuracy goes : practically no improvement; Z’s apparent gain was leakage.
  • Final answer: X learns; Y and Z do not (Z’s clean accuracy vs ).

Key lesson

A test set contaminated by training data silently inflates . Always check that train and test are disjoint.

Common mistakes

  • Treating training accuracy as performance.
  • Saying “learning requires labels” (unsupervised learning has none).

Exam traps

  • “Define ML” questions: give both Samuel and Mitchell; Mitchell is the formal one.
  • The definition says improves; a constant program does not learn.

Connections: Evaluation (C11–C13), overfitting (C7), supervised / unsupervised split (C2–C3 below).

Mastery checklist

  • State Samuel’s and Mitchell’s definitions.
  • Identify for any story.
  • Explain why must be on unseen data.

C2. Supervised Learning: Classification vs Regression

Intuition

Supervised learning = learning with an answer key. You get pairs and must predict for new . If the answer is a category (spam / not spam) it is classification; if it is a number (price) it is regression.

flowchart LR
    T[(Training pairs<br/>X_i, y_i)] --> L[Learner]
    L --> H["hypothesis h(X)"]
    N[New X_0] --> H
    H --> P["prediction ŷ"]
    P -.compare on held-out data.-> E[estimate of true risk]

Formal definition

Given training data , , learn a function (the inferred function) so that estimates the target of a new .

  • Classification: (discrete).
  • Regression: (continuous).

Mathematical formulation

Empirical risk for loss : with for classification and for regression.

Derivation

Why not just minimise to zero? A lookup table does it. We want small true risk ; is only an estimate, so we evaluate on held-out data.

Key insights

  • Same input, different target type → different algorithm family.
  • Every classifier in this course (trees, Bayes, kNN, logistic, SVM, perceptron, NN) fits this template.

Worked examples

Example 1 — Very Easy · Classification or regression?

  • Problem: Classify as classification or regression: (a) predict a patient’s blood-sugar level; (b) predict whether a tumour is malignant; (c) predict tomorrow’s rainfall in mm; (d) predict whether it will rain tomorrow.
  • Asked: Task type.
  • Concept: Target type.
  • Approach: Discrete vs continuous target.
  • Solution: (a) blood sugar is a real number → regression. (b) malignant / benign → classification. (c) mm of rain is a real number → regression. (d) rain / no rain → classification.
  • Final answer: (a) regression; (b) classification; (c) regression; (d) classification.

Key lesson

Look at , not . The same weather features can feed either task.

Example 2 — Easy · Reading a data table

  • Problem: A shop records (age, income in ₹ thousand) and whether the customer buys: no, yes, yes. State , , , .
  • Asked: Notation.
  • Concept: Training-set notation.
  • Approach: Count rows and feature columns.
  • Solution: Rows = 3 → . Feature columns = 2 → . , .
  • Final answer: .

Key lesson

counts features only; the target is not a feature.

Example 3 — Moderate · Scoring a study-time model

  • Problem: Hours studied vs marks out of 10 : . A tutor’s rule is . Compute the empirical squared loss and the number of points a rounding classifier (” equals ”) gets right.
  • Asked: and the match count.
  • Concept: Empirical risk.
  • Approach: Compute predictions and residuals.
  • Solution: . Residuals : . Squared: . . Matches: first three equal, fourth → 3 correct of 4.
  • Final answer: ; 3 / 4 correct.

Key lesson

The same predictions can be scored by different losses (0.25 squared loss vs 25 % 0-1 error).

Example 4 — Hard · A regressor that acts as a classifier

  • Problem: House prices (₹ lakh): sizes (m²) ; prices . A manager wants “cheap” () vs “expensive”. Option 1: regress price, then threshold at 40. Option 2: train a classifier directly. The regressor is . Compute the threshold classifier’s accuracy and find the decision boundary .
  • Asked: Accuracy and boundary.
  • Concept: A regressor induces a classifier.
  • Approach: Predict, threshold, compare with true labels.
  • Solution: Predictions: ; ; ; ; . True labels (price → cheap): C, C, E, E, E. Predicted labels with rule “predicted is cheap”: C, C, E (not ), E, E → all five match → accuracy . Boundary: (ties at 70 go to expensive).
  • Final answer: Accuracy 100 %, boundary .

Key lesson

A regression model contains classification information, but the tie-breaking rule at the boundary must be fixed in advance.

Example 5 — Very Hard · The flashcard memoriser

  • Problem: A “lookup” learner stores training pairs; for a test already stored it returns the stored , otherwise it returns the majority training label. Training set: . Test set: . Compute training 0-1 risk, test 0-1 risk, and compare with the constant predictor “A”.
  • Asked: Three error rates.
  • Concept: Generalisation gap.
  • Approach: Find the majority label first, then evaluate each point.
  • Solution: Majority label = A (3 vs 1). Training: every stored point is returned exactly → 0 errors → risk 0. Test: stored → A, correct. : unseen → A, true B → error. : A, true B → error. : A, true A → correct. Test risk . Constant “A”: test predictions all A → correct on and → risk 0.5. Training risk of constant A: 1 error of 4 → 0.25.
  • Final answer: Lookup: train 0, test 0.5. Constant A: train 0.25, test 0.5. The lookup learner has a gap of 0.5 and no test advantage.
LearnerTrain riskTest riskGap
Lookup00.50.5
Constant “A”0.250.50.25

Key lesson

Zero training error can coexist with no generalisation. The gap is what matters.

Common mistakes

  • Using the wrong loss (squared error on categorical labels).
  • Counting the target as a feature.

Exam traps

  • Labels coded vs change formulas (logistic uses ; SVM and perceptron use ).
  • “Logistic regression” is a classifier.

Connections: Every later module. Regression → C26; classification → C4–C10, C14–C22, C23–C25, C27, C30–C34, C49+.

Mastery checklist

  • Define classification and regression.
  • Write the empirical risk for 0-1 and squared loss.
  • Explain train vs test risk.

C3. Unsupervised Learning & Feature Engineering

Intuition

No answer key. Two things you can still do: group similar items (clustering) or describe how the data is spread (density estimation). Separately, you can clean up the features: keep the useful ones (selection) or build new, better ones (extraction).

flowchart TD
    F[Raw features] --> A{Use labels?}
    A -->|yes| S["Feature selection<br/>keep original columns<br/>χ² · information gain"]
    A -->|no| X["Feature extraction<br/>build new columns<br/>PCA · entropy-based"]

Formal definitions

  • Clustering: partition data into groups; high similarity within, low across.
  • Density estimation: estimate the distribution .
  • Feature selection (supervised): score each feature using class labels (e.g. statistic, information gain), keep the top-scoring ones.
  • Feature extraction (unsupervised): build new features from old ones without labels (e.g. PCA, entropy-based methods).

Mathematical formulation

for a feature-vs-class contingency table with observed counts and expected counts : Information gain of a feature is defined in C4.

Derivation

Expected counts are what we would see if feature and class were independent: . A large deviation from independence means the feature carries information about the class, so it gets a high score.

Key insights

  • Selection keeps original features (interpretable); extraction creates new ones.
  • Supervised ⇒ selection (uses labels); unsupervised ⇒ extraction (does not).
  • Variance is not relevance (see Examples 4 and 5).

Worked examples

Example 1 — Very Easy · Which bucket is PCA?

  • Problem: Is PCA feature selection or feature extraction? Is it supervised?
  • Asked: Category.
  • Concept: Taxonomy.
  • Approach: PCA builds new axes and ignores labels.
  • Solution: New features → extraction; no labels → unsupervised.
  • Final answer: Unsupervised feature extraction.

Key lesson

Selection = keep; extraction = build.

Example 2 — Easy · Pick the best weather features

  • Problem: For the play-tennis data, the information-gain scores are outlook 0.247, temperature 0.029, humidity 0.152, wind 0.048. Select the top 2 features.
  • Asked: Top-2.
  • Concept: Score-based selection.
  • Approach: Sort descending.
  • Solution: . Top two: outlook, humidity.
  • Final answer: {outlook, humidity}.

Key lesson

Selection is just ranking by a score and cutting at a budget.

Example 3 — Moderate · Does the word “free” signal spam?

  • Problem: Among 100 emails: contains “free” & spam = 30, “free” & ham = 10, no “free” & spam = 20, no “free” & ham = 40. Compute .
  • Asked: .
  • Concept: feature score.
  • Approach: Row / column totals → expected counts → sum.
  • Solution: Row totals: present , absent . Column totals: spam , ham ; . , , , . Deviations: . .
spamhamrow total
”free” present30 (E=20)10 (E=20)40
”free” absent20 (E=30)40 (E=30)60
col total5050100
  • Final answer: (strongly dependent on class; high score).

Key lesson

Expected counts come from independence: row total × column total ÷ N.

Example 4 — Hard · Duplicate sensors and a noisy one

  • Problem: Features and are exact duplicate sensors and both perfectly predict the class; is pure noise. A selector picks the top-2 by information gain. A PCA extractor keeps 1 component. Compare what each keeps. Four points : class 0; class 0; class 1; class 1.
  • Asked: Which representation is better and why.
  • Concept: Redundancy, and variance ≠ relevance.
  • Approach: Compute IG per feature; compute covariance.
  • Solution: Class entropy bit. Split on : both class 0 (entropy 0); both class 1 (entropy 0); IG; same for . For : one of each class (entropy 1); same for ; IG. The selector keeps — relevant but redundant. PCA: centred with variance each and covariance ; has variance and zero covariance with the others. The largest variance direction is — pure noise. One-component PCA keeps noise and discards the class information.
  • Final answer: Selection keeps two redundant but relevant features; one-component PCA keeps only the noise axis. Here selection wins.

Key lesson

Unsupervised extraction maximises variance, not relevance.

Example 5 — Very Hard · Vibration vs temperature on a machine

  • Problem: A factory logs two readings per machine: = vibration (swings widely) and = temperature offset (tiny). Points (state): healthy, healthy, faulty, faulty. (a) Compute PCA’s first direction and the 1-D projected class separation. (b) Compute the information gain of splitting on the sign of and on the sign of . (c) Which is better, and what is the lesson?
  • Asked: PCA direction, IGs, conclusion.
  • Concept: Variance ≠ discriminability.
  • Approach: Covariance matrix → eigenvectors → project; then entropy.
  • Solution: (a) Means: . ; ; . , eigenvalues ; first PC . Projection onto it gives coordinates for the classes in the order healthy, healthy, faulty, faulty → both classes occupy both values → total overlap. (b) Splitting on : groups and , each with one of each class → entropy 1 each → IG. Splitting on : groups all healthy and all faulty → entropy 0 → IG. (c) PCA’s direction has IG 0; the low-variance axis has IG 1.
 x2
  1 |  F(-10,1)            F(10,1)
    |
  0 +-----------------------------→ x1   ← PCA axis (var 100): classes overlap
    |
 -1 |  H(-10,-1)           H(10,-1)
  • Final answer: PCA’s direction is (no class information); separates perfectly.

Key lesson

When classification is the goal, use supervised selection — or a supervised projection such as Fisher’s discriminant (C48).

Common mistakes

  • Believing the highest-variance feature is the most useful.
  • Mixing up “selection” and “extraction”.

Exam traps

  • and IG both need labels → supervised.
  • Feature extraction may be applied before a supervised learner, but is itself unsupervised.

Connections: IG (C4), PCA (C43–C48), clustering (C35–C42).

Mastery checklist

  • Distinguish selection vs extraction and supervised vs unsupervised.
  • Compute from a table.
  • Explain why PCA can discard class-relevant directions.