Skip to content

Instantly share code, notes, and snippets.

@pramsey
Created January 26, 2016 23:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pramsey/003f20b8c4f631560fba to your computer and use it in GitHub Desktop.
Save pramsey/003f20b8c4f631560fba to your computer and use it in GitHub Desktop.
Simple k-means example
-- Run against https://github.com/pramsey/postgis/tree/kmeans
CREATE TABLE g (id integer, g geometry);
INSERT INTO g
WITH r AS (
SELECT random() u1, random() u2 FROM generate_series(1,10)
),
xy AS (
SELECT a + sqrt(-2*ln(u1))*cos(2*pi()*u2) AS x,
a + sqrt(-2*ln(u1))*sin(2*pi()*u2) AS y,
a
FROM r, generate_series(1,2) a
)
SELECT a, ST_MakePoint(x, y) g FROM xy;
SELECT ST_KMeans(g, 2) OVER (),ST_AsText(g),id FROM g;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment