liveBook · Manning

Z-score

As explained in Classic Computer Science Problems in Swift: Essential techniques for practicing programmers

A z-score is a standardized value expressing how many standard deviations a value lies from its distribution's mean.

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

How Classic Computer Science Problems in Swift: Essential techniques for practicing programmers covers it

How deep it goes
Defines the formula and implements it in Swift, while deferring elementary statistical instruction.
Examples
Uses a five-value Double array and applies normalization independently across dimensions of three-dimensional data points.
What it assumes
Assumes rudimentary understanding of mean and standard deviation rather than teaching elementary statistics from zero.
What you can do after
Build z-score normalization for Double arrays and apply it to data prepared for k-means clustering.
How it connects
Connects z-score to mean, standard deviation, variance, normalization, Euclidean distance, and k-means clustering.

Also explained in

Compare how 8 books explain z-score →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Classic Computer Science Problems in Swift: Essential techniques for practicing programmers. The section links point to the parts of the book it draws on.