Clustering divides data points into groups without deciding their membership in advance. It can reveal structure when a dataset’s constituent parts are unknown, but people must judge whether the resulting groups are meaningful. section 6.0
K-means groups points into a user-chosen number of clusters. After normalizing dimensions so their magnitudes are comparable, it assigns each point to its nearest centroid and recalculates each centroid as the mean of its assigned points. It repeats until centroids stop moving or the iteration limit is reached. Random initialization can produce different results, so choosing k may take experimentation. section 6.2
public func run(maxIterations: UInt = 100) -> [Cluster] {
for iteration in 0..<maxIterations {
clusters.forEach{ $0.points.removeAll() } // clear all clusters
assignClusters() // find clusters each is closest to - assign
let lastCentroids = centroids // record centroids
generateCentroids() // find new centroids
if lastCentroids == centroids { // have centroids moved?
print("Converged after \(iteration) iterations.")
return clusters // they haven't moved, so we've converged
}
}
return clusters
}
}
The run loop clears assignments, reassigns points, recalculates centroids, and stops when they no longer move. section 6.2
Governors by age and longitude
Grouping governors using age and state longitude can reveal an interesting cluster of western states. Random starts may change the result, and a cluster suggests correlation rather than a conclusive explanation. section 6.3
Clustering can support tasks such as:
- Finding customer groups for relevant grocery promotions. section 6.0
- Recognizing patterns in biology, images, patrol data, or voter groups. section 6.5

















