PY-69

Apply and Check an Elementwise Transform

  • Medium
  • Array Transforms
  • Python

Task

Write apply_and_check_transform(values, scale, offset).

values is a non-empty one- or two-dimensional NumPy array of real numeric values. Every value must be finite. scale and offset are finite real numbers. If one of these conditions is false, raise ValueError with the message "invalid transform input".

For valid input, apply the same rule to every element:

result = scale * values + offset

Return (result, evidence). result must have the same shape as values and must be an independent array: changing it later must not change values. evidence is a dictionary with these Python values:

  • shape: the result shape as a tuple;
  • first: the first result value in row-major order;
  • last: the last result value in row-major order;
  • minimum and maximum: the smallest and largest result values.

The four recorded values should be ordinary Python numbers, not NumPy scalar objects. The evidence gives a reader small boundary values to check by hand; it does not replace checking the complete returned array.

Example

For

values = np.array([[-2, 0, 3], [4, 5, -1]])

with scale=2 and offset=-1, the result is [[-5, -1, 5], [7, 9, -3]]. Its first value is -5, its last value is -3, and its minimum and maximum are -5 and 9.

Your implementation

Edit solution.py and keep this function signature:

You may import NumPy as np. Validate before computing. Do not modify an input, print, or ask for input.