Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Machine Learning Foundations

Overview

Machine Learning (ML) is a subset of artificial intelligence that enables systems to learn patterns from data and make predictions or decisions without being explicitly programmed. Before diving into specific algorithms, you must master the foundational concepts that underpin every ML system.

Why Foundations Matter

graph TD
    A[ML Foundations] --> B[Linear Algebra]
    A --> C[Probability & Statistics]
    A --> D[Optimization]
    A --> E[Loss Functions]
    A --> F[Regularization]
    A --> G[Model Evaluation]
    B --> H[Understanding Data Representation]
    C --> I[Understanding Uncertainty]
    D --> J[Training Models]
    E --> K[Defining Objectives]
    F --> L[Preventing Overfitting]
    G --> M[Measuring Performance]

In ML interviews, foundations are the most frequently tested area. Interviewers expect you to explain not just what a technique does, but why it works mathematically and when to apply it.

Topics in This Section

TopicKey ConceptsInterview Frequency
Linear AlgebraVectors, matrices, eigenvalues, SVD⭐⭐⭐⭐⭐
ProbabilityBayes’ theorem, distributions, MLE/MAP⭐⭐⭐⭐⭐
OptimizationGradient descent, SGD, Adam⭐⭐⭐⭐
Loss FunctionsMSE, cross-entropy, hinge loss⭐⭐⭐⭐
RegularizationL1, L2, dropout, early stopping⭐⭐⭐⭐
Bias-VarianceTradeoff, underfitting, overfitting⭐⭐⭐⭐⭐
Cross-ValidationK-fold, stratified, time series⭐⭐⭐
Feature EngineeringScaling, encoding, selection⭐⭐⭐
EvaluationAccuracy, precision, recall, AUC-ROC⭐⭐⭐⭐⭐

The ML Pipeline

graph LR
    A[Raw Data] --> B[Feature Engineering]
    B --> C[Model Selection]
    C --> D[Training]
    D --> E[Evaluation]
    E --> F{Good Enough?}
    F -->|No| B
    F -->|Yes| G[Deployment]

Prerequisites: The Math You Need

Linear Algebra (Must-Know)

ConceptWhy It Matters in MLExample Use
VectorsRepresent data points and featuresA single training sample is a vector
MatricesRepresent datasets and transformationsDataset X is an n×d matrix
Dot ProductMeasure similarityCosine similarity, neural network layers
Eigenvalues/EigenvectorsDimensionality reductionPCA finds eigenvectors of covariance matrix
SVDMatrix factorizationRecommendation systems, latent factors
Matrix CalculusGradient computationBackpropagation uses Jacobians

Key formula — Matrix multiplication in a neural network layer:

\[ z = Wx + b \]

Where W is the weight matrix, x is the input vector, and b is the bias vector.

Probability & Statistics (Must-Know)

ConceptWhy It MattersExample Use
Bayes’ TheoremUpdating beliefs with evidenceNaive Bayes, Bayesian inference
Conditional ProbabilityDependencies between variablesP(y|x) is the core prediction
Expectation & VarianceSummary statisticsMean squared error, variance of predictions
Common DistributionsModeling dataGaussian (continuous), Bernoulli (binary)
MLE / MAPParameter estimationTraining logistic regression
Central Limit TheoremWhy averages workConfidence intervals for A/B tests

Key formula — Bayes’ Theorem:

\[ P(\theta | D) = \frac{P(D | \theta) \cdot P(\theta)}{P(D)} \]

Posterior = Likelihood × Prior / Evidence

Calculus & Optimization

ConceptWhy It MattersExample Use
DerivativesFinding gradientsGradient descent updates
Chain RuleBackpropagationComputing gradients through layers
Partial DerivativesMulti-variable optimizationLoss function w.r.t. each parameter
ConvexityGuaranteeing global minimumLinear regression, logistic regression

Core ML Concepts Every Interview Tests

1. Supervised vs Unsupervised vs Self-Supervised

graph TD
    A[ML Paradigms] --> B[Supervised]
    A --> C[Unsupervised]
    A --> D[Self-Supervised]
    A --> E[Reinforcement Learning]
    B --> B1[Classification: labels are categories]
    B --> B2[Regression: labels are continuous]
    C --> C1[Clustering: group similar items]
    C --> C2[Dimensionality Reduction: compress features]
    D --> D1[Contrastive Learning: learn from data structure]
    D --> D2[Masked Prediction: fill in the blanks]
    E --> E1[Agent learns from environment rewards]

2. The Bias-Variance Tradeoff

Model ComplexityBiasVarianceRisk
Too simple (linear)HighLowUnderfitting
Just rightLow-MediumLow-MediumGood generalization
Too complex (deep tree)LowHighOverfitting

Interview tip: Always explain that increasing model complexity reduces bias but increases variance. Regularization techniques (L1, L2, dropout) help control this tradeoff.

3. Gradient Descent Variants

VariantDescriptionProsCons
Batch GDUse all data per updateStable convergenceSlow, memory-heavy
SGDUse one sample per updateFast, can escape local minimaNoisy updates
Mini-batch GDUse a batch of samplesBalance of speed and stabilityRequires batch size tuning
AdamAdaptive learning ratesFast, handles sparse gradientsCan generalize poorly

Key formula — SGD update rule:

\[ \theta_{t+1} = \theta_t - \eta \cdot \nabla_\theta J(\theta_t) \]

Where η is the learning rate and J is the loss function.

4. Regularization Techniques

TechniqueWhat It DoesWhen to Use
L1 (Lasso)Adds |w| penalty, encourages sparsityFeature selection
L2 (Ridge)Adds w² penalty, shrinks weightsGeneral overfitting
Elastic NetCombines L1 + L2Many correlated features
DropoutRandomly zero activationsDeep neural networks
Early StoppingStop training when val loss increasesAny iterative model
Data AugmentationExpand training dataImage, text, audio tasks

5. Feature Engineering Essentials

TechniqueDescriptionExample
NormalizationScale to [0,1]Min-max scaling
StandardizationZero mean, unit varianceZ-score
One-Hot EncodingBinary columns for categoriesColor → [1,0,0], [0,1,0]
Log TransformReduce skewnessIncome, population data
BinningConvert continuous to categoricalAge groups
Feature InteractionCombine featuresa × b, a / b
Polynomial FeaturesAdd powers of featuresx², x³

Common Interview Questions

Theory Questions

  1. What is the bias-variance tradeoff? Bias is error from overly simple models (underfitting). Variance is error from overly complex models (overfitting). The goal is to find the sweet spot that minimizes total error = bias² + variance + irreducible noise.

  2. Explain the difference between L1 and L2 regularization. L1 (Lasso) adds the absolute value of weights to the loss, creating sparse solutions (some weights become exactly 0) — useful for feature selection. L2 (Ridge) adds the squared magnitude, shrinking weights toward zero but never exactly zero — better for preventing overfitting when all features matter.

  3. When would you use precision vs recall? Precision: when false positives are costly (spam detection — don’t block good emails). Recall: when false negatives are costly (cancer detection — don’t miss cases). F1: when you need a balance.

  4. How does cross-validation help? It gives a more reliable estimate of model performance by training and evaluating on multiple data splits. K-fold CV reduces the variance of the performance estimate compared to a single train/test split.

Practical Questions

  1. You have a dataset with 100 features and 1000 samples. What do you do? This is a high-dimensional, low-sample scenario (curse of dimensionality). Apply feature selection (mutual information, L1 regularization), dimensionality reduction (PCA), or use simpler models. Collect more data if possible.

  2. Your model has 99% accuracy on an imbalanced dataset (99% negative). Is it good? No — this is the accuracy paradox. A model predicting all negative achieves 99%. Use precision, recall, F1, or AUC-ROC instead. Consider oversampling (SMOTE), undersampling, or class-weighted loss.

  3. How do you handle missing data? Options: (1) Delete rows/columns if missing is random and small. (2) Impute with mean/median/mode. (3) Use models that handle missingness (XGBoost). (4) Create a “missing” indicator feature. (5) Use multiple imputation for statistical rigor.

The ML Pipeline (Detailed)

graph TD
    A[Problem Definition] --> B[Data Collection]
    B --> C[Exploratory Data Analysis]
    C --> D[Data Cleaning]
    D --> E[Feature Engineering]
    E --> F[Train/Val/Test Split]
    F --> G[Model Selection]
    G --> H[Hyperparameter Tuning]
    H --> I[Final Evaluation]
    I --> J{Meets Requirements?}
    J -->|No| E
    J -->|Yes| K[Model Deployment]
    K --> L[Monitoring & Maintenance]

Study Roadmap

graph LR
    A[Week 1-2: Math Basics] --> B[Week 3-4: Core Concepts]
    B --> C[Week 5-6: Classical ML]
    C --> D[Week 7-8: Deep Learning]
    D --> E[Week 9-10: System Design]
    E --> F[Week 11-12: Mock Interviews]

Key Takeaway

“The quality of your ML system is determined by the quality of your understanding of the fundamentals, not by the complexity of your model.”

Every advanced topic — from transformers to reinforcement learning — builds on these foundations. Master them first.

References

  • Goodfellow, I., Bengio, Y., & Courville, A. (2016). Deep Learning — Chapter 4-5 (Numerical Computation, Machine Learning Basics)
  • Bishop, C. (2024). Deep Learning: Foundations and Concepts — Modern take on fundamentals
  • Hastie, T., Tibshirani, R., & Friedman, J. (2009). The Elements of Statistical Learning — Comprehensive ML theory
  • Ng, A. (2024). Machine Learning Specialization (Coursera) — Best intro course
  • Shalev-Shwartz, S. & Ben-David, S. (2014). Understanding Machine Learning — Free PDF, rigorous treatment

Cross-References