MAE, MSE and baselines
MAE, MSE and RMSE summarize the size of numerical prediction errors. Mean absolute error averages absolute errors; mean squared error averages squared errors; root mean squared error takes the square root of MSE. Choose the metric according to how mistakes affect the task, compare against a simple baseline, and report the data split and units. Lower error is better on the same metric and same observations, but not necessarily across tasks.
Calculate errors by hand
Use actual packing times [5, 8] minutes and predictions [4, 10]. The residuals are [1, −2]. Absolute errors are [1, 2]; MAE = (1 + 2)/2 = 1.5 minutes. Squared errors are [1, 4]; MSE = (1 + 4)/2 = 2.5 minutes². RMSE = sqrt(2.5) ≈ 1.58 minutes. RMSE is at least MAE for the same nonempty error set. Neither is a signed measure, and neither reveals which observations were overpredicted. Do not confuse MSE with the square of MAE.
from math import sqrt
def error_metrics(actual, predicted): if not actual or len(actual) != len(predicted): raise ValueError('Matching nonempty data required') errors = [y - p for y, p in zip(actual, predicted)] mae = sum(abs(e) for e in errors) / len(errors) mse = sum(e ** 2 for e in errors) / len(errors) return mae, mse, sqrt(mse)
mae, mse, rmse = error_metrics([5, 8], [4, 10])print(f'{mae:.2f} {mse:.2f} {rmse:.2f}')print(error_metrics([4, 4], [4, 4]))What large errors do to the score
Compare errors [1, 1, 1] with [0, 0, 3]. Both have MAE 1 minute, but the second has MSE 3 minutes² rather than 1. Squaring gives large errors greater influence, which may be useful when large failures have greater cost. Neither metric is automatically best; asymmetric penalties for late estimates may require a different measure. An empty evaluation set has undefined average error. Equal-length validation prevents accidental omission of an observation. Inspect error distribution, important groups and worst cases; an average alone cannot guarantee dependable performance.
Establish a baseline before model selection
A constant baseline ignores the features and predicts a statistic computed on training targets. The mean minimizes training squared error among constant predictions; a median minimizes training absolute error (for even length, any value between the middle two minimizes the sum). With training targets [2, 2, 8], mean is 4 and median is 2. On held-out targets [2, 8], mean predicts 4 twice: MAE 3 and MSE 10. Median predicts 2 twice: MAE 3 and MSE 18. A training-optimal baseline need not win on new data. Compute both on the same held-out observations without refitting on them.
from statistics import median
def constant_baselines(training, held_out): if not training or not held_out: raise ValueError('Both sets must contain values') mean = sum(training) / len(training) middle = median(training) results = [] for value in (mean, middle): errors = [y - value for y in held_out] results.append((value, sum(abs(e) for e in errors) / len(errors), sum(e ** 2 for e in errors) / len(errors))) return results
for value, mae, mse in constant_baselines([2, 2, 8], [2, 8]): print(f'{value:.1f} {mae:.1f} {mse:.1f}')Regularization, evaluation and uncertainty
A linear model with many coefficients may fit training noise. Ridge regression adds a penalty proportional to the sum of squared coefficients to the fitting objective, usually excluding the intercept in standard implementations. This can reduce sensitivity to noisy data, but too much shrinkage can underfit. Feature scales affect coefficient penalties, so fit any scaling on training data and select the penalty using validation. Do not add the penalty to a reported held-out MAE or MSE. The ML track covers estimators, uncertainty and regularization in detail. For further reading, scikit-learn documents LinearRegression, Ridge and DummyRegressor; this chapter does not require them or claim native package support.
Practice and decision check
Compare the mean and median baseline for training [1, 1, 10] and held-out [1, 1, 4]. The training mean is 4; the median is 1. Mean baseline MAE is 2 and MSE is 6; median MAE is 1 and MSE is 3. Neither implies a causal relationship or guarantees future accuracy. Before deployment decide what error magnitude is acceptable, whether extreme misses matter, and what to do with out-of-range inputs. Report a separate test score only after selection is frozen. The chapter project compares a fitted line with constant predictions and documents when to abstain.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
MAE, MSE and baselines quiz
Apply regression concepts to original numerical scenarios.
Errors 1 and −2 minutes give what MAE?
What are the units of MSE when targets are minutes?
Which constant minimizes training squared error for [2,2,8]?
Which targets should be used to fit a constant median baseline before final test evaluation?
What penalty does ridge regression add to the training objective, usually excluding the intercept?