Compute mean absolute percentage error (MAPE) as both a fraction and a percent
Quick Overview
Implement mean absolute percentage error in NumPy and return it both as a fraction on the scikit-learn scale and as a percent. It tests the order of dividing and averaging, metric scale conventions, and why MAPE becomes unreliable when actual values are near zero.
Compute mean absolute percentage error (MAPE) as both a fraction and a percent
Role: Data Scientist
Category: Machine Learning
Difficulty: easy
Interview Round: Other
Implement `compute_mape(y_true, y_pred)` returning the mean absolute percentage error as a tuple of floats `(fraction, percent)`.
Requirements:
- `fraction = (1/n) * sum(abs((y_true - y_pred) / y_true))` (`0.02` means 2%).
- `percent = fraction * 100.0`.
- Return `(fraction, percent)` in that exact order.
- `fraction` must match `sklearn.metrics.mean_absolute_percentage_error` to floating-point tolerance.
For example:
```python
y_true = np.array([100.0, 200.0])
y_pred = np.array([110.0, 190.0])
compute_mape(y_true, y_pred)
# (0.075, 7.5) # (|0.1| + |0.05|) / 2 = 0.075
```
The printed values can carry a rounding digit in the last place, so compare with a tolerance.
```hint Which scale
Check whether scikit-learn reports MAPE as a fraction or as a percentage before you decide where the factor of 100 goes.
```
### Constraints & Assumptions
- Inputs are fixed 1-D float arrays with `n = 8`.
- Every `y_true` value is strictly positive, so no division-by-zero handling is required.
### What a Strong Answer Covers
- Computing the elementwise relative error against the actual value and averaging it.
- Returning the fraction on scikit-learn's scale and the percent as the fraction times 100, in that order.
- A vectorized implementation with its time and space cost.
- What goes wrong when actual values are zero or close to zero, and why MAPE can mislead.
### Follow-up Questions
- How would you handle zeros in `y_true`?
- Why can MAPE be a misleading metric?
Overview: Implement mean absolute percentage error in NumPy and return it both as a fraction on the scikit-learn scale and as a percent. It tests the order of dividing and averaging, metric scale conventions, and why MAPE becomes unreliable when actual values are near zero.