This is an IntvML Pro interview question. Upgrade to Pro to unlock the full prompt, diagrams, and solution.
Tag: Safety
ML0021 L1 Loss L2 Loss
What are the key differences between L1 loss and L2 loss?

The Problem: A line is fit to 10 house sales, one of which looks like a data-entry typo. Each residual’s absolute and squared cost is left blank, and the same training run is about to use either L1 or L2 loss. How do the two choices differ?
Answer
L1 loss (mean absolute error) measures the average absolute difference between predictions and targets, while L2 loss (mean squared error) measures the average squared difference. That single change in the error function drives every practical difference: squaring amplifies large deviations, so L2 is more sensitive to outliers but enjoys a smooth gradient that shrinks to zero at the optimum; L1 treats all errors linearly, making it robust to outliers, but its gradient is a constant
everywhere, so optimization can oscillate near the minimum instead of settling. When used as a regularization penalty rather than a regression loss, L1 additionally induces sparsity: it can drive the weights of uninformative features to exactly zero, performing implicit feature selection, whereas L2 only shrinks weights toward zero.
(1) Error Measure: L1 averages absolute errors
; L2 averages squared errors
, amplifying large deviations.
(2) Gradient Behavior: L1’s gradient is a constant(undefined at 0); L2’s gradient is proportional to the error and vanishes smoothly at the optimum.
(3) Practical Choice: L1 for outlier-robust regression and sparse models; L2 for smooth, stable optimization when Gaussian noise is a reasonable assumption.Feature L1 Loss (MAE) L2 Loss (MSE) Error Calculation Absolute difference Squared difference Outlier Sensitivity Less sensitive More sensitive Gradient Constant (+1 or -1) Proportional to the error Sparsity Induces sparsity (feature selection) Does not inherently induce sparsity Optimization near minimum Can be unstable More stable Table 1: The five practical differences between L1 and L2 loss; every row follows from the choice of
versus
as the per-sample penalty.

Figure 1: Per-sample penalty as a function of the residual: L1 grows linearly (V-shape, robust to large errors), L2 grows quadratically (small errors are nearly free, large errors are heavily punished).
Mathematical Formulation:
Where:
is the true target and
the model prediction for sample
.
is the number of samples; both losses average over the dataset.
is the sign function:
for positive residuals,
for negative, undefined at 0 (subgradient
in practice).

Figure 2: Gradient magnitude versus residual: L1’s constant gradient never decays (unstable near the optimum, undefined at zero), while L2’s gradient shrinks linearly and vanishes exactly at the optimum.
Login to view more contentML0019 Imbalanced Data
This is an IntvML Pro interview question. Upgrade to Pro to unlock the full prompt, diagrams, and solution.
ML0012 F1 Score
What is F1 Score?

The Problem: A fraud dataset has only 10 frauds in 1,000 transactions. A model that never flags anything scores 99.0% accuracy and beats a model that catches 8 of the 10 frauds. What single score would rank these two detectors sensibly?
Answer
The F1 score is a metric for classification models that combines precision and recall into a single number, and it is particularly useful when classes are imbalanced. It is the harmonic mean of the two: precision measures how many positive predictions are correct, recall measures how many actual positives are found, and the harmonic mean punishes imbalance between them: a high F1 requires both to be high at once. A model with 99% precision but 10% recall scores poorly on F1, because the harmonic mean is dominated by the smaller of the two values. This makes F1 a stricter, more informative summary than accuracy or either component alone when false positives and false negatives both matter.
(1) Definition: The harmonic mean of precision and recall, one number summarizing both.
(2) Key Property: It is dominated by the lower component, so it cannot be gamed by maximizing only precision or only recall.
(3) When To Use: Imbalanced classes, or when false positives and false negatives carry comparable cost.
Figure 1: F1 contour lines over the recall–precision plane: to reach a higher F1 band you must improve both metrics: moving along one axis alone quickly flattens out.
Mathematical Formulation:
Where:
is
and
is
(see the precision-and-recall question).
,
, and
are the true positive, false positive, and false negative counts.
- The second line is the equivalent counts-only form;
does not appear, which is why F1 stays meaningful under class imbalance.
Login to view more content




