A z-score standardizes a value by measuring its distance from the mean in standard-deviation units: (value − mean) ÷ standard deviation. Values above the mean are positive; values below it are negative. section 6.2
Standardizing each dimension makes differently scaled data comparable. This matters for k-means, whose distance calculations otherwise favor dimensions with the largest numerical differences. section 6.2
A small Swift implementation builds the calculation from reusable array properties:
- Compute the mean, then variance, then standard deviation. section 6.1
- Map every element to its z-score using the shared mean and standard deviation. section 6.1
- Return 0.0 when the standard deviation is zero, avoiding division by zero for constant data. section 6.1
import Foundation // for pow(), srand48(), drand48()
extension Array where Element == Double {
var sum: Double {
return self.reduce(0.0, +)
}
// Find the average (mean)
var mean: Double {
return sum / Double(self.count)
}
// Find the variance sum((Xi - mean)^2) / N
var variance: Double {
let mean = self.mean // cache so not recalculated for every element
return self.map { pow(($0 - mean), 2) }.mean
}
// Find the standard deviation sqrt(variance)
var std: Double {
return sqrt(variance)
}
// Convert elements to respective z-scores (formula z-score =
(x - mean) / std)
var zscored: [Double] {
let mean = self.mean
let std = self.std
return self.map{ std != 0 ? (($0 -
mean) / std) : 0.0 } // avoid divide by zero
}
}
Use the compact Swift extension to inspect the complete mean-to-z-score implementation. section 6.1
Worked values
For [600, 470, 170, 430, 300], the mean is 394 and the standard deviation is approximately 147.32. The corresponding z-scores are approximately [1.398, 0.516, −1.520, 0.244, −0.638]. Applying the calculation independently to each dimension prepares points for k-means clustering. section 6.1 section 6.2







