Week 12 · Capstone · Ship a reproducible prediction system

Package inference, report held-out and shifted results, and rehearse rollback.

← Week 11 · Subject contents

Package inference, report held-out and shifted results, and rehearse rollback.

Work through the problem

Predict processing time from file size and compression flag. Use the first 120 generated requests for training and report MAE on the next 60. The contract returns one prediction in seconds for each input row. Compare with a training median and expose the fitted feature transformation through the same pipeline used offline.

A later batch simulates a service change for compressed files. Compare the slices, then apply an explicit operating rule: suspend reliance on this model for a slice when its mean absolute error exceeds 2 seconds on a labelled monitoring batch of this size. A rollback is a tested action, and a constant fallback still has its own error to report.

Run the Python lab

import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
rng = np.random.default_rng(10)
X = np.c_[rng.uniform(1,10,240),rng.integers(0,2,240)]
y = 1 + 1.7*X[:,0] + .5*X[:,1] + rng.normal(0,.3,240)
y[180:] += 4*X[180:,1]
baseline = np.median(y[:120])
pipe = make_pipeline(StandardScaler(),LinearRegression()).fit(X[:120],y[:120])
def predict(seconds_inputs):
    return pipe.predict(np.asarray(seconds_inputs,dtype=float))
test = abs(y[120:180]-predict(X[120:180]))
print("Held-out MAE, model and baseline:", round(test.mean(),2),round(abs(y[120:180]-baseline).mean(),2))
errors = abs(y[180:]-predict(X[180:]))
for flag in [0,1]:
    use = X[180:,1]==flag
    mae = errors[use].mean()
    fallback = abs(y[180:][use]-baseline).mean()
    print("Compressed",flag,"MAE",round(mae,2),"fallback MAE",round(fallback,2),"suspend",bool(mae>2))
assert test.mean()<1 and errors[X[180:,1]==1].mean()>2

Output from the lab

Held-out MAE, model and baseline: 0.26 4.18
Compressed 0 MAE 0.19 fallback MAE 3.5 suspend False
Compressed 1 MAE 4.08 fallback MAE 3.94 suspend True

Practice

  1. What if the fallback is worse on the flagged slice?

Check your reasoning

  1. Suspend automated use for that slice or route it to an established manual process while investigating. Compare fallback loss before switching traffic to it.

← Week 11 · Subject contents

Further reading · Rules of Machine Learning