Z-Score Standardize a 1D Feature

Basics
BasicsFeature ScalingNumPyGoogleMetaAmazon

Problem

Implement zscore_1d(x) to standardize a 1D numeric array to mean 0 and population standard deviation 1 using z=(x−μ)/σz = (x - \mu) / \sigma. If σ=0\sigma = 0, return zeros.

Why It Matters: Z-score standardization centers features at mean 0 and unit variance—critical for gradient descent convergence, Ridge/Lasso regularization, and PCA.

Sample Inputs & Expected Outputs

Example 1 — Symmetric array

>>> zscore_1d([10, 20, 30])
array([-1.2247, 0.0, 1.2247])

mean=20, population std=8.1650 -> centered at 0 with std 1.

Example 2 — Two-point binary

>>> zscore_1d([0, 2])
array([-1.0, 1.0])

mean=1, std=1 -> exact -1 and +1.

Example 3 — Constant array edge case

>>> zscore_1d([4, 4, 4])
array([0.0, 0.0, 0.0])

When std == 0, returns all zeros safely.

Constraints & Edge Cases

  • Return a 1D float NumPy array.

Constraints & Evaluation Criteria

  • Return a 1D float NumPy array.

Hints

Python 3
Loading Code Editor...
+1 Hidden Submit Case
Input / Scenario
[10, 20, 30]
Expected Output / Criteria
[-1.2247, 0.0, 1.2247]