PY-78

Center Columns and Check Their Means

  • Medium
  • Array Reductions
  • Python

Task

Write center_columns(values). values is a non-empty two-dimensional numeric NumPy array whose rows are observations and columns are measurements. For each column calculate its mean across axis 0, then subtract that mean from every value in the same column. The output is an independent float64 array with the original shape.

Return (centered, means, near_zero). means is a one-dimensional float64 array with one entry per column. near_zero is true exactly when np.allclose(centered.mean(axis=0), np.zeros(number_of_columns), atol=1e-9, rtol=0) is true. The input must contain finite numbers; an invalid shape, empty dimension, non-numeric dtype, or non-finite value may raise ValueError. Do not modify the input.

Example

For [[2., 10.], [4., 14.], [6., 18.]], the column means are [4., 14.] and the centered result is [[-2., -4.], [0., 0.], [2., 4.]]. Its two new column means are near zero.

Your implementation

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