Adding a segmentation version that doesn't use query string passing

This commit is contained in:
Stuart Lynn
2016-06-23 20:23:04 +00:00
parent 911a0ccccc
commit 03aada8758
2 changed files with 50 additions and 5 deletions

View File

@@ -1,3 +1,41 @@
CREATE OR REPLACE FUNCTION
CDB_CreateAndPredictSegment(
target NUMERIC[],
features NUMERIC[],
target_features NUMERIC[],
target_ids NUMERIC[],
n_estimators INTEGER DEFAULT 1200,
max_depth INTEGER DEFAULT 3,
subsample DOUBLE PRECISION DEFAULT 0.5,
learning_rate DOUBLE PRECISION DEFAULT 0.01,
min_samples_leaf INTEGER DEFAULT 1
)
RETURNS TABLE( cartodb_id Numeric, prediction Numeric , accuracy Numeric)
AS $$
import numpy as np
import plpy
from crankshaft.segmentation import create_and_predict_segment_agg
model_params = { 'n_estimators' : n_estimators,
'max_depth' : max_depth,
'subsample' : subsample,
'learning_rate' : learning_rate,
'min_samples_leaf' : min_samples_leaf}
def unpack2D(data):
dimension = data.pop(0)
a = np.array(data, dtype=float)
return a.reshape(len(a)/dimension, dimension)
return create_and_predict_segment_agg( np.array(target, dtype=float),
unpack2D(features),
unpack2D(target_features),
target_ids,
model_params)
$$ Language plpythonu;
CREATE OR REPLACE FUNCTION
CDB_CreateAndPredictSegment (
query TEXT,
@@ -16,4 +54,3 @@ AS $$
model_params = {'n_estimators': n_estimators, 'max_depth':max_depth, 'subsample' : subsample, 'learning_rate': learning_rate, 'min_samples_leaf' : min_samples_leaf}
return create_and_predict_segment(query,variable_name,target_table, model_params)
$$ LANGUAGE plpythonu;