Neural Mastery
← Practice
NumPy Vectorized Z-Score Standardizationeasy

NumPy Vectorized Z-Score Standardization

easy NumPy & Data Wrangling 10–15 min

NumPy allowed · plain Python earns 10 bonus points

Linear algebra step 1/4

A vector transforms

Matrix multiplication is the first computation behind every model.
Open lesson →
01vector
02matrix
03transform
04result
[2, −1] × [[1, 2], [−.5, 1]] = [0, −2]

Computed matrix × vector transformation

Standardize feature distributions to zero mean and unit variance using vectorized NumPy operations.

Task

Implement z_score_standardize(values). Compute the population z-score (x - mean) / std for each element, rounded to 4 decimal places. Return zeros if variance is zero.

Function Signature

z_score_standardize(values: list[float]) -> list[float]

Examples

Example 1: Simple 3-Element Series
Input: values = [1, 2, 3]
Output: [-1.2247, 0, 1.2247]
Example 2: Zero Variance Series
Input: values = [4, 4, 4]
Output: [0, 0, 0]
Example 3: Four Spread Elements
Input: values = [10, 20, 30, 40]
Output: [-1.3416, -0.4472, 0.4472, 1.3416]

Constraints

  • values is a list of numerical floats or ints.
  • Use population standard deviation (ddof=0).
  • Return [0.0] * len(values) if standard deviation is 0.0.
  • Return [] for empty input.
Python3Saved ✓
import numpy as np

def z_score_standardize(values: list[float]) -> list[float]:
"""Standardize numerical values to zero mean and unit variance: (x - mean) / std.
Use population std (ddof=0). Round each value to 4 decimal places.
If std == 0 or len(values) <= 1, return [0.0] * len(values).
If values is empty, return []."""
# Your implementation here
pass

Case 1: Simple 3-Element Series
Input: values = [1, 2, 3]
Expected: [-1.2247, 0, 1.2247]
Case 2: Zero Variance Series
Input: values = [4, 4, 4]
Expected: [0, 0, 0]
Case 3: Four Spread Elements
Input: values = [10, 20, 30, 40]
Expected: [-1.3416, -0.4472, 0.4472, 1.3416]