Machine Learning Fundamentals: Concepts, part 1
The core vocabulary of machine learning. Interviewers use these to check that your foundations are precise, not just familiar.
What is machine learning, and how do supervised, unsupervised, semi-supervised, self-supervised and reinforcement learning differ?
Machine learning is learning a function from data rather than hand-coding rules: you choose a model family, a loss, and an optimization procedure that fits the model's parameters to data.
| Paradigm | Data | Objective | Example |
|---|---|---|---|
| Supervised | Inputs with labels | Predict the label | Spam classification, price prediction |
| Unsupervised | Inputs only | Find structure | Clustering, dimensionality reduction |
| Semi-supervised | Few labels + many unlabeled | Use unlabeled data to improve the supervised model | Pseudo-labeling |
| Self-supervised | Unlabeled; labels created from the data itself | Predict a hidden part of the input | Next-token prediction (LLMs), masked language modeling, contrastive learning |
| Reinforcement learning | Interaction with an environment | Maximize cumulative reward | Game playing, RLHF |
Self-supervised learning is why LLMs scale: the internet provides virtually unlimited "labels" (the next token).
Explain the bias–variance trade-off.
Expected test error for squared loss decomposes into bias² + variance + irreducible noise.
- Bias: error from wrong assumptions, a model too simple to capture the true function (underfitting). E.g., a linear model on a curved relationship.
- Variance: error from sensitivity to the particular training sample, a model that fits noise (overfitting). E.g., a deep unpruned decision tree.
Increasing model complexity typically lowers bias and raises variance; the best test error is at the balance point. Diagnose with learning curves: high train error ≈ val error → high bias (bigger model, better features, less regularization); low train error ≪ val error → high variance (more data, regularization, simpler model, ensembling/bagging).
Modern nuance: very over-parameterized models (deep nets) can show double descent, where test error falls again past the interpolation threshold, so the classic U-curve isn't the whole story.
What is overfitting and how do you prevent it?
Overfitting is when a model learns patterns specific to the training data (including noise) that don't generalize, seen as a gap between training and validation performance.
Prevention, roughly in order of effectiveness:
- More (and more diverse) data, or data augmentation.
- Regularization: L2/L1 penalties, dropout, weight decay.
- Simpler model / fewer features / feature selection.
- Early stopping on validation loss.
- Ensembling (bagging reduces variance).
- Cross-validation to choose hyperparameters without overfitting to one split.
Also check for leakage: suspiciously good validation results often mean the validation data leaked into training.
What are training, validation and test sets for? Why not tune on the test set?
- Training set: fit the model's parameters.
- Validation set: choose hyperparameters, features, model type and early-stopping point.
- Test set: a final, untouched estimate of generalization.
If you tune on the test set, it becomes part of model selection, and the reported score is optimistically biased: you've overfit to that specific sample. With many experiments, even random noise can look like improvement. Keep the test set locked until the end, or use nested cross-validation when data is scarce. In production ML, the best "test set" is often a future time period (temporal holdout).
L1 vs L2 regularization — what's the difference and why does L1 produce sparsity?
Both add a penalty on weights to the loss: L2 adds λ·Σw² (ridge), L1 adds λ·Σ|w| (lasso).
- L2 shrinks all weights smoothly toward zero but rarely makes them exactly zero; it handles correlated features by spreading weight across them. Equivalent to a Gaussian prior on weights.
- L1 drives many weights to exactly zero, which means implicit feature selection. Equivalent to a Laplace prior.
Why sparsity: geometrically, the L1 constraint region is a diamond with corners on the axes, and the loss contours typically first touch it at a corner, where some coordinates are 0. Analytically, L1's gradient has constant magnitude λ regardless of how small w is, so it can push a weight all the way to zero, while L2's gradient 2λw vanishes as w → 0.
Elastic Net combines both to get sparsity while handling correlated features.
How does gradient descent work? Compare batch, stochastic and mini-batch.
Gradient descent iteratively updates parameters in the direction of steepest decrease of the loss: θ ← θ − η·∇L(θ), where η is the learning rate.
| Variant | Gradient computed on | Pros | Cons |
|---|---|---|---|
| Batch | Full dataset | Exact gradient, stable | Slow per step, memory-heavy |
| Stochastic (SGD) | One example | Cheap steps; noise helps escape saddle points | Very noisy, so it needs a decaying LR |
| Mini-batch | 32–4096 examples | GPU-efficient, moderate noise | Batch size is a hyperparameter |
Mini-batch is the standard. The learning rate is the most important hyperparameter: too high diverges or oscillates, too low is slow and gets stuck. Schedules (warmup, cosine decay) and adaptive optimizers (Adam) address this.
What is a loss function vs an evaluation metric? Give examples where they differ.
The loss is what the optimizer minimizes during training; it must be (sub)differentiable and well-behaved. The metric is what you report and care about; it can be anything that reflects business value.
| Task | Typical loss | Typical metric |
|---|---|---|
| Binary classification | Binary cross-entropy | Precision/recall at a threshold, PR-AUC |
| Ranking | Pairwise/listwise losses (or cross-entropy) | nDCG@k, MRR |
| Regression | MSE or Huber | MAE, MAPE, business cost |
| Object detection | Classification + box regression losses | mAP |
They differ because metrics like accuracy, F1 or nDCG are non-differentiable (step functions of thresholds or ranks). You optimize a smooth surrogate and monitor the metric on validation data.
Generative vs discriminative models?
- Discriminative models learn P(y|x) or a decision boundary directly: logistic regression, SVMs, most neural classifiers. Usually better accuracy for classification given enough data.
- Generative models learn P(x, y) or P(x) (and get P(y|x) via Bayes): Naive Bayes, GMMs, HMMs, VAEs, diffusion models, and LLMs as models of P(text). They can generate samples, handle missing data, and often work better with little data.
Classic result (Ng & Jordan): Naive Bayes converges faster with few examples, while logistic regression has lower asymptotic error.
What is the "no free lunch" theorem and what does it mean in practice?
Averaged over all possible problems, no learning algorithm outperforms any other. Every algorithm's advantage comes from inductive biases that match the structure of the problems we actually care about (smoothness, locality, sparsity, hierarchy).
In practice: there is no universally best model. Choose models whose assumptions fit your data (gradient-boosted trees for tabular data, CNNs/ViTs for images, Transformers for sequences), and validate empirically. Always compare against simple baselines.