Calculate the standard deviation of a dataset used for feature normalization.
Standard deviation is the square root of variance, expressed in the same units as the original data, which makes it more directly interpretable than variance for understanding data spread. Sample standard deviation uses the (n−1) denominator (Bessel's correction); population standard deviation uses n. In ML feature engineering, standard deviation is the key ingredient in z-score normalization (standardization): scaling a feature to zero mean and unit variance by subtracting the mean and dividing by the standard deviation.
s = sqrt(sum((x_i - mean)^2) / (n - 1))
Variance is in squared units (e.g. dollars² if the data is in dollars), which has no direct real-world meaning, while standard deviation is back in the original units, making it directly comparable to the data itself and to the mean.
Standardization computes z = (x − mean) / std for each feature value, producing a rescaled feature with mean 0 and standard deviation 1 — this puts differently-scaled features on comparable footing for gradient-based models and distance-based algorithms.
It expresses standard deviation as a percentage of the mean (std/mean × 100%), which is useful for comparing the relative variability of datasets with different units or very different average magnitudes.
In almost all practical ML scenarios, yes — your training data is a sample from a larger underlying data distribution, so the (n−1) sample formula is the statistically appropriate choice.