Compute a zero-safe MAPE that excludes zero actuals and counts them

Quick Overview

Implement a mean absolute percentage error function in NumPy that skips zero actuals, returns the metric as a fraction, and reports how many rows were excluded. It tests masking before division, the all-zero edge case, and how percentage error metrics behave near zero.

Compute a zero-safe MAPE that excludes zero actuals and counts them

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `safe_mape(y_true, y_pred)` to compute MAPE over only the non-zero actuals. Return the tuple `(mape_fraction, n_excluded)`. - `mape_fraction` is the mean of `abs((y_true - y_pred) / y_true)` over entries with `y_true != 0` only. - Return a fraction (sklearn convention), not a percentage. - `n_excluded` is a Python `int` counting entries where `y_true == 0`. - If every `y_true` is zero, return `(float('nan'), n)`. For example: ```python y_true = np.array([0., 100., 0., 50., 200., 40.]) y_pred = np.array([1., 110., 5., 45., 180., 44.]) safe_mape(y_true, y_pred) # (0.1, 2) ``` ```hint Order of operations Decide which entries are allowed to act as denominators before any division happens, and think about what the mean of an empty selection should be. ``` ### Constraints & Assumptions - Inputs are 1-D `np.ndarray` of equal length. - The grader uses `n = 6` with exactly two zeros in `y_true`, so `n_excluded == 2`. ### What a Strong Answer Covers - Exclusion of zero actuals before any division, so no infinities or division warnings appear. - A mean over the remaining entries only, returned as a fraction. - `n_excluded` returned as a plain Python `int`. - The all-zero case returning NaN together with the full count. ### Follow-up Questions - Why is MAPE asymmetric, and when would you prefer sMAPE? - How would you handle near-zero (not exactly zero) actuals that still blow up the metric? - How does sklearn's `mean_absolute_percentage_error` treat a zero actual, and why can its output mislead?

Overview: Implement a mean absolute percentage error function in NumPy that skips zero actuals, returns the metric as a fraction, and reports how many rows were excluded. It tests masking before division, the all-zero edge case, and how percentage error metrics behave near zero.

PracHub logo
General
Nov 25, 2026
easyData ScientistOtherMachine Learning
0
0

Implement safe_mape(y_true, y_pred) to compute MAPE over only the non-zero actuals. Return the tuple (mape_fraction, n_excluded).

  • mape_fraction is the mean of abs((y_true - y_pred) / y_true) over entries with y_true != 0 only.
  • Return a fraction (sklearn convention), not a percentage.
  • n_excluded is a Python int counting entries where y_true == 0 .
  • If every y_true is zero, return (float('nan'), n) .

For example:

y_true = np.array([0., 100., 0., 50., 200., 40.])
y_pred = np.array([1., 110., 5., 45., 180., 44.])
safe_mape(y_true, y_pred)  # (0.1, 2)

Constraints & Assumptions

  • Inputs are 1-D np.ndarray of equal length.
  • The grader uses n = 6 with exactly two zeros in y_true , so n_excluded == 2 .

What a Strong Answer Covers Guidance

  • Exclusion of zero actuals before any division, so no infinities or division warnings appear.
  • A mean over the remaining entries only, returned as a fraction.
  • n_excluded returned as a plain Python int .
  • The all-zero case returning NaN together with the full count.

Follow-up Questions Guidance

  • Why is MAPE asymmetric, and when would you prefer sMAPE?
  • How would you handle near-zero (not exactly zero) actuals that still blow up the metric?
  • How does sklearn's mean_absolute_percentage_error treat a zero actual, and why can its output mislead?