Tag: Safety

  • ML0031 Linear Regression

    This is an IntvML Pro interview question. Upgrade to Pro to unlock the full prompt, diagrams, and solution.

  • ML0021 L1 Loss L2 Loss

    What are the key differences between L1 loss and L2 loss?

    A three-panel question card about L1 and L2 loss: house-price data with one extreme point, a residual table with blank absolute and squared columns, and one training setup splitting into an L1 run and an L2 run, each asking what will differ.

    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 \pm 1 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 |e|; L2 averages squared errors e^2, amplifying large deviations.
    (2) Gradient Behavior: L1’s gradient is a constant \pm 1 (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.

    FeatureL1 Loss (MAE)L2 Loss (MSE)
    Error CalculationAbsolute differenceSquared difference
    Outlier SensitivityLess sensitiveMore sensitive
    GradientConstant (+1 or -1)Proportional to the error
    SparsityInduces sparsity (feature selection)Does not inherently induce sparsity
    Optimization near minimumCan be unstableMore stable

    Table 1: The five practical differences between L1 and L2 loss; every row follows from the choice of |e| versus e^2 as the per-sample penalty.

    L1 absolute loss V-shape versus L2 squared loss parabola as functions of the residual

    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:
    \mathcal{L}_{L1} = \frac{1}{n} \sum_{i=1}^{n} \left| \hat{y}_i - y_i \right|
    \frac{\partial \mathcal{L}_{L1}}{\partial \hat{y}_i} = \frac{1}{n}\,\mathrm{sign}(\hat{y}_i - y_i)
    \mathcal{L}_{L2} = \frac{1}{n} \sum_{i=1}^{n} \left( \hat{y}_i - y_i \right)^2
    \frac{\partial \mathcal{L}_{L2}}{\partial \hat{y}_i} = \frac{2}{n}\left(\hat{y}_i - y_i\right)

    Where:

    • y_i is the true target and \hat{y}_i the model prediction for sample i.
    • n is the number of samples; both losses average over the dataset.
    • \mathrm{sign}(\cdot) is the sign function: +1 for positive residuals, -1 for negative, undefined at 0 (subgradient [-1, 1] in practice).
    Gradient of L1 loss constant step function versus gradient of L2 loss linear in the residual

    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 content
  • ML0019 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?

    Whiteboard-style card showing an imbalanced dataset of 990 legitimate and 10 fraudulent transactions, plus two fraud-detector confusion matrices with their accuracies: a do-nothing model at 99.0 percent and a model that catches 8 of 10 frauds at 98.6 percent.

    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.

    F1 score contour lines over the recall-precision plane

    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:
    \text{F1} = \frac{2 \times \text{Precision} \times \text{Recall}}{\text{Precision} + \text{Recall}}
    \text{F1} = \frac{2TP}{2TP + FP + FN}

    Where:

    • \text{Precision} is TP/(TP+FP) and \text{Recall} is TP/(TP+FN) (see the precision-and-recall question).
    • TP, FP, and FN are the true positive, false positive, and false negative counts.
    • The second line is the equivalent counts-only form; TN does not appear, which is why F1 stays meaningful under class imbalance.

    Login to view more content