Week 11 · Serving parity and drift monitoring

Preserve the training transform and detect a shift in new inputs.

← Week 10 · Subject contents · Week 12 →

Preserve the training transform and detect a shift in new inputs.

Work through the problem

A scoring service needs the same parameters and feature order as training. A pipeline that learned μ=10 and σ=2 maps input 14 to z=2. A service that rescales from its own batch can silently deliver a different score for the same record.

This lab serializes an illustrative pipeline’s means, scales, and regression coefficients to JSON, then independently evaluates the serving equation. Monitor feature drift separately from outcome performance. A shift of 2 training standard deviations is a trigger for investigation in this example.

Run the Python lab

import json
import numpy as np
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LinearRegression
X = np.array([[8.],[10.],[12.]])
pipe = make_pipeline(StandardScaler(),LinearRegression()).fit(X,[4,5,6])
scaler, model = pipe.steps[0][1],pipe.steps[1][1]
wire = json.dumps(dict(mean=scaler.mean_.tolist(), scale=scaler.scale_.tolist(), w=model.coef_.tolist(), b=model.intercept_))
spec = json.loads(wire)
batch = np.array([[13.],[14.],[15.]])
z = (batch-spec["mean"])/spec["scale"]
online = z@spec["w"]+spec["b"]
print("Parity:", np.allclose(online,pipe.predict(batch)))
print("Drift in training units:", z.mean().round(2))
assert np.allclose(online,pipe.predict(batch)) and abs(z.mean())>2

Output from the lab

Parity: True
Drift in training units: 2.45

Practice

  1. Can feature drift establish that model accuracy fell?

Check your reasoning

  1. No. Check actual outcomes when labels arrive. The feature shift is evidence to investigate.

← Week 10 · Subject contents · Week 12 →

Further reading · Rules of Machine Learning