Machine Learning & AI Engineering Foundations

Choose a Loss Before a More Complex Model

Compare constant baselines to understand regression losses.

In this chapter

  • Compute mean absolute and mean squared error.
  • Explain why outliers influence losses differently.
  • Separate a training objective from held-out usefulness.
Regression evidence
  1. Training labels
  2. Frozen baseline
  3. Later predictions
  4. Residual review

Compare complexity against a transparent baseline on data not used to fit it.

Establish an honest starting point

A regression model outputs a number, but the loss determines what kinds of mistakes matter during fitting and evaluation. For equipment turnaround, a two-hour miss may be inconvenient while a twelve-hour miss can undermine planning. Mean absolute error averages the magnitude of each miss and retains the target's units. Mean squared error squares the misses before averaging, giving large errors more influence and producing squared units. Neither metric automatically encodes the coordinator's actual decision cost.

Begin with a constant prediction learned only from training labels. The training mean minimizes squared error among constant predictions; a training median minimizes absolute error. These are strong diagnostic baselines because their behavior is transparent. If an elaborate model cannot improve on the appropriate baseline in the later validation period, examine data quality and feature availability before adding complexity. A lower training loss alone is not evidence of a better deployed service.

Work through an outlier by hand

The four synthetic turnaround values include one long case. The mean prediction moves toward it more than the median does. The snippet compares both predictions on the same training values only to illustrate the objective. It prints the absolute and squared errors; do not report these numbers as held-out performance.

python
from statistics import mean, median

hours = [2.0, 3.0, 4.0, 15.0]

def mae(actual, prediction):
    return mean(abs(value - prediction) for value in actual)

def mse(actual, prediction):
    return mean((value - prediction) ** 2 for value in actual)

mean_guess = mean(hours)
median_guess = median(hours)
assert mean_guess == 6.0 and median_guess == 3.5
assert mae(hours, median_guess) == 3.5
assert mae(hours, mean_guess) == 4.5
assert mse(hours, mean_guess) < mse(hours, median_guess)
for name, guess in (("mean baseline", mean_guess), ("median baseline", median_guess)):
    print({"model": name, "prediction_hours": guess, "mae_hours": mae(hours, guess), "mse_hours_squared": mse(hours, guess)})

Inspect residuals, not just one score

The failure mode is choosing a model from a single aggregate metric while ignoring where it fails. A model could improve average error yet consistently underestimate long repairs. Record residuals as prediction minus actual, group them by equipment family and time period, and inspect both large underestimates and overestimates. Use groups that describe the equipment process rather than personal characteristics of staff. Include counts so a group containing two cases does not appear as reliable as one containing hundreds.

As a documented extension, compare the baseline with a scikit-learn regression pipeline fitted within the same split rules. Evaluate on original hour units even if training uses a transformed target. Check whether back-transformation changes the meaning of the estimated quantity. A useful model card states the chosen loss, baseline, sample sizes and decision limitations; it does not claim the metric is a universal measure of intelligence or business value.

Build a baseline comparison sheet

  • Create a separate later-period list of synthetic hours and evaluate both frozen training predictions on it.
  • Add a case with a much longer turnaround and observe its effect on both losses.
  • Write down whether underestimation and overestimation have equal planning costs for the intended review workflow.

Expected checks

  • Validation labels never recompute the constant predictions.
  • MAE is reported in hours and MSE in squared hours.
  • The report includes large residual examples and the constant baseline.

Check your understanding

Why can a single long turnaround pull the mean baseline away from the typical short case?

  • Squared-error optimization gives a large deviation substantial influence.
  • The mean automatically removes outliers.
  • The median always predicts the same value as the mean.
Answer explanation

For constant predictions, the mean minimizes squared loss. Squaring increases the influence of large deviations; the median instead minimizes absolute loss.

Official tools & further reading

The curriculum

  1. Freeze the Prediction Moment — Free preview

    Choose the target, feature availability and time boundaries before fitting a model.

  2. Choose a Loss Before a More Complex Model — Free preview

    Compare constant baselines to understand regression losses.

  3. Turn Scores into Review Decisions — Sign-in access

    Use confusion counts and a stated cost model to select a threshold without confusing scores with calibrated probabilities.

  4. Combine Votes and Discover Groups — Sign-in access

    Contrast supervised voting with unsupervised clustering using small examples whose limitations remain visible.

  5. Inspect a Neuron's Learning Step — Free preview

    Check one small gradient numerically before relying on an automatic differentiation framework.

  6. Represent Text with Inspectable Vectors — Sign-in access

    Build a small lexical similarity baseline and identify where word overlap stops representing meaning.

  7. Version and Test the Inference Contract — Sign-in access

    Package model metadata, feature expectations and rejection behavior before exposing predictions to another component.

  8. Deliver the Repair Forecast Decision Pack — Sign-in access

    Integrate the experiments into a reproducible equipment-planning project with clear human-review boundaries.