Complete worked example
python
import numpy as np
def inspect_example(reference_rows, evaluation_rows):
reference = np.asarray(reference_rows, dtype=np.float64)
if reference.ndim != 2 or 0 in reference.shape or not np.isfinite(reference).all():
raise ValueError("expected a non-empty finite reference matrix")
samples = (np.asarray(evaluation_rows, dtype=np.float64) if len(evaluation_rows)
else np.empty((0, reference.shape[1]), dtype=np.float64))
if samples.ndim != 2 or samples.shape[1] != reference.shape[1] or not np.isfinite(samples).all():
raise ValueError("evaluation features must match the reference")
mean = reference.mean(axis=0)
scale = reference.std(axis=0, ddof=0)
scale = np.where(scale == 0, 1.0, scale)
result = (samples - mean) / scale
return result- Reasoning
- The worked example validates non-empty finite reference rows and the same number of feature columns in the evaluation rows. It computes mean and std with axis=0 on reference. A zero standard deviation is replaced with one as an explicit numerical policy, avoiding division by zero while leaving differences on that constant feature visible. The resulting transformation uses column-wise broadcasting already introduced earlier in the course. ddof=0 here describes a population-standard-deviation calculation on the supplied reference. It is not a universal unbiased estimator claim. A different statistical estimator would require different sample-size assumptions, especially for a reference containing only one row.