KMeans clustering and weighted centroid analysis

This commit is contained in:
Ubuntu
2016-06-07 19:58:32 +00:00
parent 21dd956c15
commit 4e86965f03
8 changed files with 166 additions and 0 deletions

31
src/pg/sql/11_kmeans.sql Normal file
View File

@@ -0,0 +1,31 @@
CREATE OR REPLACE FUNCTION CDB_KMeans(query text, no_clusters integer,no_init integer default 20)
RETURNS table (cartodb_id integer, cluster_no integer) as $$
import plpy
plpy.execute('SELECT cdb_crankshaft._cdb_crankshaft_activate_py()')
from crankshaft.clustering import kmeans
return kmeans(query,no_clusters,no_init)
$$ language plpythonu;
CREATE OR REPLACE FUNCTION CDB_WeightedMean(query text, weight_column text, category_column text default null )
RETURNS table (the_geom geometry,class integer ) as $$
BEGIN
RETURN QUERY
EXECUTE format( $string$
select ST_SETSRID(st_makepoint(cx, cy),4326) the_geom, class from (
select
%I as class,
sum(st_x(the_geom)*%I)/sum(%I) cx,
sum(st_y(the_geom)*%I)/sum(%I) cy
from (%s) a
group by %I
) q
$string$, category_column, weight_column,weight_column,weight_column,weight_column,query, category_column
)
using the_geom
RETURN;
END
$$ LANGUAGE plpgsql;