liveBook · Manning

Clustering

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

Clustering is a computational technique that divides data points into groups whose members are related according to some measure or interpretation.

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

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

How deep it goes
Defines clustering and details k-means assignment, centroid updates, normalization, initialization, and stopping; leaves choosing k to experimentation.
Examples
Uses grocery-shopping patterns, governor age and longitude, and applications in patrol, biology, image recognition, and voter targeting.
What you can do after
Apply k-means in Swift to group data points, then inspect whether the resulting clusters are meaningful.
How it connects
Connects clustering to unsupervised learning, k-means, normalization, centroids, and pattern recognition.

Also explained in

Compare how 18 books explain clustering →

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.