From 5d2a1881b1ed52b8bc4cc555762b60ceb6c16164 Mon Sep 17 00:00:00 2001 From: Andy Eschbacher Date: Thu, 13 Oct 2016 15:00:28 +0000 Subject: [PATCH 1/2] make numpy with global scope in module --- src/py/crankshaft/crankshaft/clustering/kmeans.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/py/crankshaft/crankshaft/clustering/kmeans.py b/src/py/crankshaft/crankshaft/clustering/kmeans.py index c99ded1..18a711f 100644 --- a/src/py/crankshaft/crankshaft/clustering/kmeans.py +++ b/src/py/crankshaft/crankshaft/clustering/kmeans.py @@ -1,6 +1,6 @@ from sklearn.cluster import KMeans import plpy - +import numpy as np def kmeans(query, no_clusters, no_init=20): """ @@ -39,7 +39,6 @@ def kmeans_nonspatial(query, colnames, num_clusters=5, num_clusters (int): number of clusters (greater than zero) id_col (string): name of the input id_column """ - import numpy as np out_id_colname = 'rowids' # TODO: need a random seed? From 0feaf36cf62f8963b05014d50be477778e7e818f Mon Sep 17 00:00:00 2001 From: Andy Eschbacher Date: Thu, 13 Oct 2016 15:52:00 +0000 Subject: [PATCH 2/2] outputting consistent labels and centers --- src/py/crankshaft/crankshaft/clustering/kmeans.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/py/crankshaft/crankshaft/clustering/kmeans.py b/src/py/crankshaft/crankshaft/clustering/kmeans.py index 18a711f..86e8931 100644 --- a/src/py/crankshaft/crankshaft/clustering/kmeans.py +++ b/src/py/crankshaft/crankshaft/clustering/kmeans.py @@ -53,14 +53,13 @@ def kmeans_nonspatial(query, colnames, num_clusters=5, try: db_resp = plpy.execute(full_query) - plpy.notice('query: %s' % full_query) except plpy.SPIError, err: plpy.error('k-means cluster analysis failed: %s' % err) # fill array with values for kmeans clustering if standarize: cluster_columns = scale_data( - extract_columns(db_resp, id_col='cartodb_id')) + extract_columns(db_resp, id_col=out_id_colname)) else: cluster_columns = extract_columns(db_resp) @@ -69,7 +68,8 @@ def kmeans_nonspatial(query, colnames, num_clusters=5, kmeans = KMeans(n_clusters=num_clusters, random_state=0).fit(cluster_columns) - return zip(kmeans.labels_, map(str, kmeans.cluster_centers_), + return zip(kmeans.labels_, + map(str, kmeans.cluster_centers_[kmeans.labels_]), db_resp[0][out_id_colname])