BLOGLLM Logo
Go Pro
Guest
Not signed in
All BlogsCoursesPracticeInterview prepSeriesTagsBookmarksPricing
Log in
All BlogsCoursesPracticeTagsBookmarksPricing
My PrepProblemsStudy PlansResearch PapersRoadmapInterviewContestsLeaderboardGames
Articles
Practice / Evaluation metrics30 of 33 in topic

811. Report RMSE, MAE and MAPE for a forecast

EasyEvaluation metrics

Why it matters Demand forecasts, latency predictors and LLM cost models are all judged with these metrics.

Three numbers summarise most regression and forecasting dashboards. MAE is the average absolute miss, RMSE punishes big misses more, and MAPE expresses the miss as a percentage of the true value.

Write regression_errors(y_true, y_pred) returning a dict with:
- "mae": mean |y - p|
- "rmse": sqrt(mean (y - p) ** 2)
- "mape": 100 * mean |y - p| / |y| over the rows where y != 0 only (nan if every y is 0)

Functiondef regression_errors(y_true, y_pred)

Example 1

Input: regression_errors([100.0, 200.0], [110.0, 180.0])

Output: {'mae': 15.0, 'rmse': 15.8114, 'mape': 10.0}

Example 2

Input: y_true = [0.0, 4.0], y_pred = [1.0, 2.0]

Output: {'mae': 1.5, 'rmse': 1.581139, 'mape': 50.0}

Example 3

Input: y_true = [0.0], y_pred = [3.0]

Output: {'mae': 3.0, 'rmse': 3.0, 'mape': float('nan')}

Constraints

  • Python 3 with NumPy. Code runs in your browser.
  • Your code must pass all 4 tests to be accepted.
Hint 1

Compute the error vector e = y - p once.

Hint 2

For MAPE, build a mask of rows where y is not zero before dividing.

Common mistakes
  • Dividing by zero in MAPE when a true value is 0.
  • Taking the square root before averaging in RMSE.
Understand the idea

MAE and RMSE are in the units of y; RMSE >= MAE, with a bigger gap when a few errors are large. MAPE is unit free but undefined where y = 0, so those rows are skipped.

Worked example

y = [100, 200], p = [110, 180]. Errors 10 and 20: MAE 15, RMSE sqrt(250) = 15.81, MAPE = (10% + 10%) / 2 = 10%.

mae = mean|e|, rmse = sqrt(mean e^2), mape = 100 mean(|e| / |y|) where y != 0
Topics
Evaluation metrics
Similar questions
  • Compute precision, recall and F1 from predictionsEasy
  • Measure how overconfident a classifier is with expected calibration errorEasy
  • Build a confusion matrixEasy
  • Compute mean reciprocal rankEasy
All Evaluation metrics problems
Report
811 / 903
Python 3solution.pyPython idle
1
2
3
4
5
6
import numpy as np
​
def regression_errors(y_true, y_pred):
# your code here
pass
​

Run checks these cases in your browser, no sign-in needed. Submit checks all 4 tests.

Ctrl/Cmd + Enter to run
© BlogLLM, Bengaluru, IndiaAboutTermsPrivacyRefundsDMCAContactRSS