Skip to main content

Model Training & Evaluation

Docs: Cross-validation · TimeSeriesSplit · Hyperparameter tuning · Optuna · MLflow Tracking

Overview​

Training turns clean data and honest features into a model you can sign off. Picking the model is already solved by the classification cheatsheet. What's left is discipline: validate the way production predicts, tune without fooling yourself, and touch the test set exactly once. The failure people hit is a flattering validation number. On the machine-failure problem, a random split reported AUC 0.87 and production delivered 0.79.

Random 5-fold mixes validation rows into every month and over-reports; a time-based split trains on Jan to Apr, validates on May and tests once on JunRandom 5-fold mixes validation rows into every month and over-reports; a time-based split trains on Jan to Apr, validates on May and tests once on Jun

Figure L4-1: The same six monthly snapshots, split two ways. Blue is training data, green is validation, amber is the test set.

Key concepts​

Decisions at a glance​

DecisionDefaultAvoid#1 failure mode
First modelRule-based baseline, then logistic regressionStarting with the fanciest modelNo baseline → no idea if 0.84 is good
Validation splitTime-based for anything temporal; stratified k-fold otherwiseRandom k-fold on temporal dataValidating on the past, predicting the future
Hyperparameter searchRandom search or Optuna, ~50 trialsExhaustive grid searchBurning compute on a 6-D grid
Search targetCV score inside the training windowTuning against the test setTest set becomes a training signal
Experiment trackingLog params + data hash + metrics per runNotebook memory"Which run was the good one?"
Final evaluationHeld-out test set, touched once, sliced by segmentRe-running until the number looks goodSilent failure on the machines that matter

Baseline first​

A model score means nothing until you know what a dumb strategy scores. Score the free baselines before training anything.

StrategyPR-AUCWhat it tells you
Maintenance rule: active temperature alarm or over 5,000 hours since service0.24The bar the model must clear by enough to justify existing
Logistic regression on the final features0.31Two minutes of work. The linear ceiling
Tuned XGBoost0.46Roughly double the rule, +50% over linear

Without the first two rows, 0.46 is a number in a vacuum. If XGBoost had barely cleared the rule, the right ship decision would have been the CMMS query.

Choosing the validation split​

The split must simulate how the model will be used. For failure prediction that means predicting the future, not a random 20%. The mechanics of k-fold live in the supervised intro. This section is about choosing.

Training data is monthly snapshots, Jan–Jun. Random stratified k-fold shuffles all six months together, so the model trains on May machines and validates on February ones. It learns seasonal load, the March line upgrade and fleet ageing from the future. Production never gets that.

Validation schemeValidation AUCProduction AUC (next month)Result
Random stratified 5-fold across all months0.870.79❌ Over-reports by 0.08. A number you gave the plant manager and then missed
Train Jan–Apr → validate May → test Jun0.820.81✅ Lower, and honest

The lower validation number is the better validation. Use random stratified k-fold only when rows are exchangeable, with no time structure. That's why the supervised pages use it. Deciding which regime you're in is the skill.

Random search beats grid search at equal budget, because the parameters that matter get 50 distinct values instead of 5.

ApproachBudgetResult
Full grid, 5 values × 6 params15,625 fits❌ Almost all spent on parameters that don't matter
Random search~50 trials✅ Covers the space better at any fixed budget
Optuna (Bayesian)~50 trials✅ Use when fits are expensive. Later trials search near earlier winners
Any search scored on the Jun test set—❌ The test set stops measuring generalisation

Search inside the training window only. Tuning moved the failure XGBoost from PR-AUC 0.42 to 0.46 and plateaued around trial 40. When the search flatlines, the next gain is in the features, not the hyperparameters.

Knob (XGBoost)Setting hereWhy
max_depthsearch 3–8Main capacity control
learning_ratesearch 0.01–0.1, log scalePairs with early stopping
subsamplesearch 0.6–1.0Row sampling against overfitting
n_estimators + early_stopping_rounds2000 + 50, stopped on the May setLets the data pick the tree count
scale_pos_weight≈ 32 (9,700 / 300)Corrects the 3% base rate
eval_metricaucprMatches the sign-off metric

Experiment tracking​

MLflow, W&B or a CSV. The tool matters less than what each run logs.

LogQuestion it answers
Params + code versionCan I reproduce the winner?
Training-data hash or snapshot rangeDid the data change, or the model?
Validation metrics, all of them, slicedWhich run is actually better?
The model artifactWhat exactly did we deploy?

Evaluate and sign off​

One aggregate metric can hide a broken model. Metric definitions and the threshold worked example live on the classification page. This is the pre-ship discipline on top.

StepOn the failure modelWhat it catches
Slice the metricOverall PR-AUC 0.46, CNC spindles 0.21 (only 40 spindle failures)The model guessing on the most expensive machines
Read the errors20 high-confidence false positives broke down on day 31–35, just outside the label windowA label-definition disagreement, not a modelling bug
Touch the test set onceJun scored one time, after every decision is frozenA test set quietly turned into a second validation set
Business sign-offDowntime hours prevented at the chosen threshold and inspection budget, known weak slices, the monitoring planAn AUC in an email standing in for a decision

A weak slice needs an explicit decision: ship with a documented carve-out ("planners handle spindles on the old schedule"), or fix it first.

Gotchas​

  • Random k-fold on monthly snapshots. Validation leaks the future and over-reports. Split by snapshot date.
  • Early stopping on the test set. The early-stopping set is a tuning input. Stop on validation (May), never on Jun.
  • Re-scoring the test set after each tweak. Each look turns it into a validation set and its number into fiction. Freeze decisions, then score once.
  • Comparing runs on different data. Without a data hash, a gain could be a new snapshot, not a better model. Log the snapshot range with every run.
  • Tuning past the plateau. 200 trials buying 0.01 is noise. Go back to features.

Scenario questions​

Q1 ★ Your tuned XGBoost scores PR-AUC 0.46. Is that good?

Model answer
  • Clarify: good compared to what? What does the plant do today?
  • Observe: score the maintenance rule (0.24) and logistic regression (0.31) on the same split.
  • Hypothesise: 0.46 could be strong or trivial. The base rate is 3%, so a random model scores about 0.03.
  • Fix: report it as roughly double the current rule and +50% over linear.
  • Prevent: baselines go in the first experiment log, before any model. The trade-off is half a day spent on models nobody will ship.

Q2 ★★ Validation AUC was 0.87. The first month in production scored 0.79. Walk me through it.

Model answer
  • Clarify: is the data temporal, and how was the validation set drawn?
  • Observe: the split was random stratified 5-fold across Jan–Jun snapshots. Rerun with train Jan–Apr, validate May: 0.82.
  • Hypothesise: the random split let the model learn from later months to predict earlier ones. Less likely: real drift after launch.
  • Fix: switch to a time-based split and re-tune inside the training window.
  • Prevent: the split mirrors how production predicts. The honest number is lower, and the stakeholder hears it before launch.

Q3 ★★ Tuning has run 200 trials for +0.01. A teammate wants a bigger grid. What do you say?

Model answer
  • Clarify: what's the variance between runs with different seeds?
  • Observe: the Optuna history plateaued around trial 40, from 0.42 to 0.46.
  • Hypothesise: the remaining 0.01 is within seed noise. The model has hit the ceiling of its features.
  • Fix: stop tuning. Spend the time on features from the EDA decision log.
  • Prevent: set a trial budget (~50) and a plateau rule up front. The trade-off is maybe leaving a real 0.005 on the table.

Q4 ★★★ The model clears the bar overall, but the plant manager says it misses spindle failures. What do you do?

Model answer
  • Clarify: how costly is a missed spindle failure compared to other machines?
  • Observe: slice PR-AUC by machine type. Spindles score 0.21 against 0.46 overall, from only 40 failures.
  • Hypothesise: too few spindle failures to learn from, or spindle failures look different from the rest of the fleet.
  • Fix: ship with a documented carve-out where planners keep spindles on the old schedule, or delay for more data or a spindle-specific feature.
  • Prevent: sign-off always includes per-segment metrics. The trade-off is slower sign-off and more uncomfortable conversations.

Summary​

  • No baseline, no meaning. The model must beat the dumb strategy by enough to justify existing.
  • Validate the way production predicts. Temporal problem, temporal split. The lower honest number beats the higher flattering one.
  • Random beats grid. About 50 trials, inside the training window only. When the search flatlines, go back to the features.
  • Log every run. Params, data hash, sliced metrics and artifact, or you can't tell which run won.
  • Slice, read errors, then touch the test set once. In that order, and the last one only once.

Next → Inference & Production