Variation
Z-Scores
Standardize each value as its distance from the mean in standard-deviation
units: z = (x − mean) / std. Z-scores let you compare values across
datasets with different scales. By hand, three loops: mean, then sample std
(ddof=1), then one z-score per element. With scipy, stats.zscore(values, ddof=1) computes all z-scores in one vectorised call.
By hand
Loop 1: mean. Loop 2: sample std (ddof=1, dividing by n−1 — same as
statistics.stdev). Loop 3: for each value, compute and append
round((v - mean) / std, 4). With mean=8.0 and std=3.0 the z-scores are
exact fractions of 1/3: −4/3, 0, −2/3, 5/3, 2/3, −2/3, 1/3.
import math
values = [4, 8, 6, 13, 10, 6, 9]
n = len(values)
total = 0.0
for v in values:
total = total + v
mean = total / n
sq_diff = 0.0
for v in values:
sq_diff = sq_diff + (v - mean) ** 2
std = math.sqrt(sq_diff / (n - 1))
result = []
for v in values:
result.append(round((v - mean) / std, 4))
print('RESULT:', result)
With the library
scipy.stats.zscore defaults to ddof=0 (population std). Passing
ddof=1 makes it use the sample std, matching the naive half. The snapshot
shows mean and std so the denominator choice is visible.
import numpy as np
from scipy import stats
from dalib.display import set_display
set_display()
values = [4, 8, 6, 13, 10, 6, 9]
z = stats.zscore(values, ddof=1)
result = [round(float(v), 4) for v in z]
print('mean:', float(np.mean(values)))
print('std (ddof=1):', round(float(np.std(values, ddof=1)), 4))
print('RESULT:', result)
mean: 8.0
std (ddof=1): 3.0
RESULT: [-1.3333, 0.0, -0.6667, 1.6667, 0.6667, -0.6667, 0.3333]
Implementation notes
scipy.stats.zscoredefaults toddof=0— always passddof=1explicitly when you want sample-std standardization to matchstatistics.stdevornp.std(ddof=1).- Z-scores sum to 0 and have mean 0; their sample std (ddof=1,
np.std(z, ddof=1)) equals 1.0. The population std (ddof=0) is less than 1 for any finite sample. Usenp.std(z, ddof=1)to verify. - Cross-reference:
standard-deviation(this chapter) for the ddof details;zscore-flag(python-data-cleaning ch06) for applying z-scores to outlier detection.