PY-79

Fit and Reuse a Column Standardizer

  • Medium–Hard
  • Array Standardization
  • Python

Task

Write fit_reuse_standardizer(training, new_rows). Both inputs are finite, two-dimensional numeric arrays with the same number of columns. training must have at least one row; new_rows may have zero rows.

Fit one summary per training column. The location is mean(training, axis=0). The spread is the population root-mean-square deviation:

sqrt(mean((training - location) ** 2, axis=0))

When a spread is zero, save 1.0 as its scale. This is the zero-spread rule: the constant training column becomes all zeros, while a new value is still measured relative to the constant location. Otherwise the saved scale is the spread. Transform both arrays with (values - location) / scale, using only the training summaries for both transformations.

Return a dictionary with independent float64 arrays under "mean", "spread", "scale", "training", and "new". The first three have shape (columns,); the last two retain their input shapes. Do not refit on new_rows or modify either input. Invalid dimensions, column counts, dtypes, or non-finite values may raise ValueError.

Example

For training column [2., 4., 6.], the mean is 4. and the population spread is sqrt(8/3). A new value 8. uses that same mean and spread; it does not change the fitted summary.

Your implementation

You may import NumPy as np. Do not print or ask for input.