Fit preprocessing on training data only
A model evaluation is misleading when preprocessing learns from held-out observations. Compute the mean and population standard deviation on the training values, then reuse those parameters for later values. A constant training feature needs an explicit policy: use a scale of one so it transforms to zero without division by zero.
train = [2.0, 4.0]
mean, scale = 3.0, 1.0
print((10.0 - mean) / scale)Think before coding
Separating fit and transform prevents held-out values from influencing the training representation. The variance here is a population statistic, and a constant feature contributes zero after centering. Work through the example by hand before changing the starter. Decide what should happen for an empty input, a single value and a repeated value where those cases apply. The checks use several inputs so a hard-coded answer cannot stand in for the rule.
Your task
Write fit_scale(train), returning (mean, population_standard_deviation), with scale 1.0 for a constant feature. Raise ValueError for empty training data. Write transform(values, mean, scale) returning standardized values using the supplied parameters.
The explanation opens here once every check passes.