z-Score (Standard Score)
The -score (or standard score) measures how many standard deviations an observation lies above or below the population mean .
Formula
Population -Score:
Where:
- is the raw value.
- is the population mean: .
- is the population standard deviation: .
Sample -Score:
Where is sample mean and is sample standard deviation (using degrees of freedom).
Key Properties
- Standardized Distribution: After applying -score transformation, the new distribution has a mean of 0 and a standard deviation of 1.
- Empirical Rule (68-95-99.7 Rule for Normal Distributions):
- of values fall within .
- of values fall within .
- of values fall within .
Common Use Cases in Machine Learning
1. Feature Standardization
Gradient descent and distance-based algorithms (SVM, KNN, PCA, Logistic Regression, Deep Learning) require normalized feature magnitudes to prevent large-scale features from dominating gradients:
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
X_standardized = scaler.fit_transform(X_train)
2. Statistical Outlier Detection
Points with (or ) are commonly flagged as potential outliers in normally distributed data:
from scipy import stats
import numpy as np
z_scores = np.abs(stats.zscore(data))
outliers = np.where(z_scores > 3.0)
Robust Alternatives (For Heavy Outliers / Skewed Data)
Because mean () and standard deviation () are sensitive to extreme outliers, standard -scores can be distorted. Robust alternatives include:
1. Modified -Score (using Median Absolute Deviation)
Where .
2. Robust Scaler (IQR)
from sklearn.preprocessing import RobustScaler
scaler = RobustScaler()
X_robust = scaler.fit_transform(X_train)