📘
Normalize Array
MediumNumPy Broadcasting
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 with:
- "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:
Column means/stds are computed, then broadcast to normalize
Constraints:
- Normalize along axis=0 (column-wise)
- Use ddof=0 for std calculation
- Round to specified decimal places
Editor
Python 3.13.1
Test Results
0/0Run code to see test results.