Skip to main content
100%

Better Kmeans loop for any number of clusters

✓ Published0🌍 Public
Aandrewxhill
Last edited Oct 17, 2012
Created on Oct 17, 2012

A PostgreSQL recursive query iteratively refines k-means clustering on natural earth point data, partitioning features into an arbitrary number of groups. The process seeds centroids from a rectangular grid generated with CartoDB’s `CDB_RectangleGrid`, then repeatedly reassigns points to the nearest centroid and recomputes cluster centers using `ST_Centroid` and `ST_Collect`. After ten iterations, each point is labeled with its final cluster ID via a spatial join on `ST_Distance`. The output shows the clustered point assignments, not the centroid geometries.

AI-generated description

Similar vizzes