Back to Practice
#0008
Standardize a Numeric Column
EasyNumPy12 min10 XP
Problem
Given a one-dimensional NumPy array, return the standardized values: each value minus the mean, divided by the standard deviation.
Why This Matters
Standardization appears everywhere in ML because many algorithms behave better when features share a comparable scale.
Function Signature
def standardize(x):
Examples
Example 1
Inputx = np.array([10, 20, 30])
Outputarray([-1.2247, 0.0, 1.2247])
The mean is 20. Values below the mean become negative; values above become positive.
Constraints
- Input is a one-dimensional numeric array.
- If standard deviation is zero, return zeros to avoid division by zero.
- Use population standard deviation, matching np.std default.
CodePython
Visible browser tests run here when available.
Testcases2 visible / 4 hidden categories
Three evenly spaced values
Inputnp.array([10, 20, 30])
Expectedapproximately [-1.2247, 0.0, 1.2247]
The middle value equals the mean, so its standardized value is zero.
Constant column
Inputnp.array([5, 5, 5])
Expected[0.0, 0.0, 0.0]
There is no spread, so dividing by standard deviation would be invalid.
Hidden Test Categories
Negative valuesFloating point valuesLarge arraysConstant arrays