Implement z-Score Standardization
Data Processing
Easy
Problem
Standardize numeric data to zero mean and unit population variance along a selected axis:
z = \frac{x - \mu}{\sigma}
Here, x is an input value, \mu is the mean of its selected slice, \sigma is the population standard deviation of that slice, and z is the standardized value. Use column-wise slices when axis=0 and row-wise slices when axis=1. If a slice has standard deviation at most eps, return zeros for that slice. Return a NumPy array.
Theory
Z-score standardization transforms data to have zero mean and unit standard deviation. Each value is converted to the number of standard deviations it lies from the mean. This is one of the most fundamental preprocessing techniques in statistics and machine learning, making features comparable regardless of their original scales.
Why Standardize?
Equal feature contribution: Features with larger scales would otherwise dominate distance calculations and gradient updates.
Algorithm requirements: Many algorithms assume or perform better with standardized data (PCA, SVM, regularized regression).
Interpretable scores: Z-scores directly indicate how unusual a value is - a z-score of 2 means the value is 2 standard deviations above the mean.
Numerical stability: Keeps values in reasonable ranges, preventing overflow in calculations.
The Z-Score Formula
For a value x from a distribution with mean \mu and standard deviation \sigma:
z = \frac{x - \mu}{\sigma}
For a sample:
z = \frac{x - \bar{x}}{s}
Where:
- \bar{x} = sample mean
- s = sample standard deviation
Properties of Z-Scores
Mean of zero: After transformation, the mean of z-scores is exactly 0
E[Z] = E\left[\frac{X - \mu}{\sigma}\right] = \frac{E[X] - \mu}{\sigma} = 0
Unit variance: Standard deviation of z-scores is exactly 1
\text{Var}(Z) = \text{Var}\left(\frac{X - \mu}{\sigma}\right) = \frac{\text{Var}(X)}{\sigma^2} = 1
Shape preservation: Standardization is a linear transformation; it does not change the shape of the distribution.
Worked Example
Data: [10, 20, 30, 40, 50]
Step 1 - Calculate mean:
\bar{x} = \frac{10 + 20 + 30 + 40 + 50}{5} = 30
Step 2 - Calculate standard deviation:
s = \sqrt{\frac{(10-30)^2 + (20-30)^2 + (30-30)^2 + (40-30)^2 + (50-30)^2}{5-1}}
= \sqrt{\frac{400 + 100 + 0 + 100 + 400}{4}} = \sqrt{250} \approx 15.81
Step 3 - Calculate z-scores:
z_1 = \frac{10 - 30}{15.81} = -1.26
z_2 = \frac{20 - 30}{15.81} = -0.63
z_3 = \frac{30 - 30}{15.81} = 0.00
z_4 = \frac{40 - 30}{15.81} = 0.63
z_5 = \frac{50 - 30}{15.81} = 1.26
Result: [-1.26, -0.63, 0.00, 0.63, 1.26]
Verification: Mean ≈ 0, Standard deviation ≈ 1
Sample vs Population Standard Deviation
Population standard deviation (divide by N):
\sigma = \sqrt{\frac{1}{N} \sum_{i=1}^{N} (x_i - \mu)^2}
Sample standard deviation (divide by N-1, Bessel's correction):
s = \sqrt{\frac{1}{N-1} \sum_{i=1}^{N} (x_i - \bar{x})^2}
Which to use?
- Sample std when data is a sample from a larger population (most ML scenarios)
- Population std when you have the entire population
Interpretation of Z-Scores
Z = 0: Value equals the mean
Z > 0: Value is above the mean
- Z = 1: One standard deviation above
- Z = 2: Two standard deviations above (unusual)
- Z = 3: Three standard deviations above (rare)
Z < 0: Value is below the mean
- Z = -1: One standard deviation below
- Z = -2: Two standard deviations below
For normal distributions:
- ~68% of values have |z| < 1
- ~95% of values have |z| < 2
- ~99.7% of values have |z| < 3
Handling Zero Standard Deviation
When all values are identical:
\sigma = 0 \Rightarrow z = \frac{x - \mu}{0} = \text{undefined}
Solutions:
- Return 0 for all values (they are at the mean)
- Return NaN and handle separately
- Remove the constant feature (has no variance to contribute)
Column-wise Standardization
For a dataset with multiple features, standardize each column independently:
For each column:
- Compute column mean
- Compute column standard deviation
- Apply z-score formula to each element
Result: Each feature has mean 0 and std 1, but different features may still have different ranges.
Train-Test Split Considerations
Critical rule: Compute mean and std from training data only
Apply to training data:
z_{train} = \frac{x_{train} - \mu_{train}}{\sigma_{train}}
Apply to test data using training statistics:
z_{test} = \frac{x_{test} - \mu_{train}}{\sigma_{train}}
Why? Using test statistics causes data leakage - the model would have information about the test distribution.
Inverse Transform
To convert z-scores back to original scale:
x = z \cdot \sigma + \mu
Use case: After making predictions in standardized space, convert back to interpretable units.
Standardization vs Normalization
Standardization (Z-score):
- Centers at mean 0, scales to std 1
- Unbounded output range
- Based on mean and std (sensitive to outliers)
- Good for: Gaussian-like distributions, many ML algorithms
Normalization (Min-Max):
- Scales to fixed range [0, 1]
- Bounded output range
- Based on min and max (very sensitive to outliers)
- Good for: Neural networks, bounded domains
Impact on Different Algorithms
Benefits from standardization:
- PCA (finds directions of maximum variance)
- SVM with RBF kernel (distance-based)
- Regularized regression (L1, L2 penalties)
- K-means clustering (distance-based)
- Gradient descent optimization
Indifferent to standardization:
- Decision trees (split on rank, not magnitude)
- Random forests (tree-based)
- Naive Bayes (probabilistic, not distance-based)
Numerical Stability
Catastrophic cancellation: Computing (x - \bar{x}) when x and \bar{x} are large and similar can lose precision.
Two-pass algorithm: First pass computes mean, second pass computes variance (more stable)
Welford's algorithm: Single-pass, numerically stable online algorithm for computing mean and variance
Where Z-Score Standardization Shows Up
Principal Component Analysis: Requires standardized features for meaningful variance decomposition
Support Vector Machines: Kernel methods are sensitive to feature scales
Neural Networks: Standardized inputs help with gradient flow and convergence
Regularized Regression: Ridge and Lasso penalties treat features equally only when standardized
Statistical Testing: Z-tests use standardized statistics
Anomaly Detection: Outliers identified by extreme z-scores
Comparing Across Domains: Exam scores, performance metrics from different scales
Scientific Research: Combining measurements from different instruments
Financial Analysis: Comparing returns across different asset classes
Examples
Example 1
- Input
X = [[1, 2], [3, 6], [5, 10]], axis = 0, eps = 1e-8- Output
[[-1.224745, -1.224745], [0.0, 0.0], [1.224745, 1.224745]]- Explanation
- Both columns have the same relative spacing, so their standardized values match.
Hints
- Use np.mean(..., keepdims=True) and np.std(..., keepdims=True) along axis.
- Use np.where(std > eps, std, 1.0) before dividing.
Requirements
- Compute the population mean and standard deviation along the selected axis
- Preserve dimensions during reductions so values broadcast back to X
- Map every slice with standard deviation at most eps to zeros
- Return a NumPy array of floating-point values
Constraints
- X is a nonempty one-dimensional or two-dimensional numeric list
- axis is 0 for one-dimensional inputs and either 0 or 1 for two-dimensional inputs
- eps is positive
- Use NumPy only
Starter Code
import numpy as np
def zscore_standardize(X: list, axis: int = 0, eps: float = 1e-12) -> np.ndarray:
"""
Returns population Z-scores as a NumPy array matching the shape of X.
"""
# Write code here
passTest Cases
| Case | Matches | |
|---|---|---|
| Column-wise | Example 1 | public |