v9.1.1
Menu

Normalize Array

mediumFREEBroadcastingNumPy BroadcastingNumPy Foundations

Problem Statement

Normalize array data using broadcasting.

Background

Normalization scales data to a standard range. Common methods:

  • Min-max: (x - min) / (max - min) → scales to [0, 1]
  • Z-score: (x - mean) / std → centers around 0

These operations use broadcasting naturally.

Your Task

Write a function normalize(arr) that normalizes a 2D array column-wise.

Output Format

Return a dictionary whose values are (nested) Python lists of floats, not NumPy arrays:

  • "minmax": Min-max normalized (rounded to 3 decimals)
  • "zscore": Z-score normalized (rounded to 3 decimals)
  • "col_means": Mean of each column before normalization (rounded to 2 decimals)
  • "col_stds": Std of each column before normalization (rounded to 2 decimals)

Example

Input
[[1, 2], [3, 4], [5, 6]]
Output
Min-max scales each column to [0, 1], z-score centers each column
Reasoning
  • arr is a 3×23 \times 2 array: 3 samples (rows) by 2 features (columns). Every statistic is taken per column.
  • Column 0 holds 1,3,51, 3, 5: min 11, max 55, mean μ=3\mu = 3. With divisor n=3n = 3 (not n−1n-1), σ=(−2)2+02+223=8/3≈1.633\sigma = \sqrt{\frac{(-2)^2 + 0^2 + 2^2}{3}} = \sqrt{8/3} \approx 1.633, reported as 1.63.
  • Min-max: x−15−1\frac{x - 1}{5 - 1} maps 1,3,51, 3, 5 to 0,0.5,10, 0.5, 1. Z-score: x−31.633\frac{x - 3}{1.633} gives −1.225,0,1.225-1.225, 0, 1.225, because 2/8/3=1.52 / \sqrt{8/3} = \sqrt{1.5}.
  • Column 1 (2,4,62, 4, 6) is column 0 shifted up by 1: its mean becomes 4.0, but the spread is unchanged, so its normalized values and its std 1.63 match column 0 exactly.
  • Output: minmax = [[0.0, 0.0], [0.5, 0.5], [1.0, 1.0]], zscore = [[-1.225, -1.225], [0.0, 0.0], [1.225, 1.225]], col_means = [3.0, 4.0], col_stds = [1.63, 1.63].
  • Notice: both normalizations ignore a constant shift, and every value is a float even though the input is integers.

Constraints

  • Normalize along axis=0 (column-wise)
  • Use ddof=0 for std calculation
  • Round to specified decimal places

Learn this in

More from NumPy Foundations

solution.py

Test results

Sign in to run your code against the test cases.