Github user njayaram2 commented on a diff in the pull request:
https://github.com/apache/incubator-madlib/pull/69#discussion_r87238676
--- Diff:
src/ports/postgres/modules/elastic_net/elastic_net_generate_result.py_in ---
@@ -13,50 +15,131 @@ def __elastic_net_generate_result (optimizer,
iteration_run, **args):
"""
Generate result table for all optimizers
"""
- plpy.execute("""
- drop table if exists {tbl_result};
- create table {tbl_result} (
- family text,
- features text[],
- features_selected text[],
- coef_nonzero double precision[],
- coef_all double precision[],
- intercept double precision,
- log_likelihood double precision,
- standardize boolean,
- iteration_run integer)
- """.format(**args))
-
standardize_flag = "True" if args["normalization"] else "False"
-
+ source_table = args["rel_source"]
if optimizer == "fista":
- result_func = "__gaussian_fista_result(_state)"
- tbl_state = "{tbl_fista_state}"
+ result_func =
"__gaussian_fista_result({col_grp_state})".format(col_grp_state=args["col_grp_state"])
elif optimizer == "igd":
- result_func = "__gaussian_igd_result(_state, '{sq_str}'::double
precision[], {threshold}::double precision, {tolerance}::double precision)"
- tbl_state = "{tbl_igd_state}"
+ result_func = """__gaussian_igd_result({col_grp_state},
+ '{sq_str}'::double precision[],
+ {threshold}::double precision,
+ {tolerance}::double precision
+ )
+ """.format(col_grp_state=args["col_grp_state"],
+ tolerance=args["warmup_tolerance"],
+ threshold=args["threshold"],
+ sq_str=args["sq_str"])
+ tbl_state = "{rel_state}".format(rel_state=args["rel_state"])
- result = plpy.execute(
- """
- select
- (result).coefficients as coef,
- (result).intercept as intercept
- from (
- select {{schema_madlib}}.{result_func} as result
- from {tbl_state}
- where _iteration = {{iteration_run}}
+ grouping_column = args['grouping_col']
+ grouping_str1 = ""
+ if grouping_column:
+ col_grp_key = args['col_grp_key']
+ grouping_str = args['grouping_str']
+ groupby_str = "GROUP BY {grouping_col}, {col_grp_key}".format(
+ grouping_col=grouping_column, col_grp_key=col_grp_key)
+ cols_types = dict(get_cols_and_types(args["tbl_source"]))
+ grouping_str1 = grouping_column + ","
+ select_grouping_info = ','.join([grp_col.strip()+"\t" +
cols_types[grp_col.strip()]
+ for grp_col in grouping_column.split(',')]) + ","
+ using_str = "USING ({col_grp_key})".format(col_grp_key=col_grp_key)
+ out_table_qstr = """
+ SELECT
+ {grouping_str1}
+ (result).coefficients AS coef,
+ (result).intercept AS intercept
+ FROM (
+ SELECT {schema_madlib}.{result_func} AS result,
+ {col_grp_key}
+ FROM {tbl_state}
+ WHERE {col_grp_iteration} = {iteration_run}
) t
- """.format(result_func = result_func, tbl_state =
tbl_state).format(
+ JOIN
+ (
+ SELECT
+ {grouping_str1}
+ array_to_string(ARRAY[{grouping_str}],
+ ','
+ ) AS {col_grp_key}
+ FROM {source_table}
+ {groupby_str}
+ ) n_tuples_including_nulls_subq
+ {using_str}
+ """.format(result_func = result_func, tbl_state = tbl_state,
+ col_grp_iteration = args["col_grp_iteration"],
+ iteration_run = iteration_run,
+ groupby_str=groupby_str,
+ grouping_str1=grouping_str1,
+ grouping_str=grouping_str,
+ using_str=using_str,
+ col_grp_key=col_grp_key,
+ source_table=source_table,
+ schema_madlib = args["schema_madlib"])
+ else:
+ ## Its a much simpler query when there is no grouping.
+ select_grouping_info = ""
+ out_table_qstr = """
+ SELECT
+ (result).coefficients AS coef,
+ (result).intercept AS intercept
+ FROM (
+ SELECT {schema_madlib}.{result_func} AS result
+ FROM {tbl_state}
+ WHERE {col_grp_iteration} = {iteration_run}
+ ) t
+ """.format(result_func = result_func, tbl_state = tbl_state,
+ col_grp_iteration = args["col_grp_iteration"],
iteration_run = iteration_run,
- **args))[0]
+ schema_madlib = args["schema_madlib"])
- r_coef = mad_vec(result["coef"], text = False)
+ ## Create the output table
+ plpy.execute("""
+ DROP TABLE IF EXISTS {tbl_result};
+ CREATE TABLE {tbl_result} (
+ {select_grouping_info}
+ family text,
+ features text[],
+ features_selected text[],
+ coef_nonzero double precision[],
+ coef_all double precision[],
+ intercept double precision,
+ log_likelihood double precision,
+ standardize boolean,
+ iteration_run integer)
+ """.format(select_grouping_info=select_grouping_info, **args))
+
+ result = plpy.execute(out_table_qstr)
+ for res in result:
+ build_output_table(res, grouping_column, grouping_str1,
+ standardize_flag, iteration_run, **args)
+ ## Create summary table, listing the grouping columns used.
--- End diff --
These were the statistics that were already present in elastic net. Adding
more stats to the summary table might be possible, but would that warrant a
broader discussion?
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---