Created
January 26, 2016 23:45
-
-
Save pramsey/003f20b8c4f631560fba to your computer and use it in GitHub Desktop.
Simple k-means example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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