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

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.

naive.py
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)
library.py
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.zscore defaults to ddof=0 — always pass ddof=1 explicitly when you want sample-std standardization to match statistics.stdev or np.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. Use np.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.