Github user decibel commented on a diff in the pull request:
https://github.com/apache/incubator-madlib/pull/41#discussion_r61940732
--- Diff: src/ports/postgres/modules/pred_metrics/pred_metrics.py_in ---
@@ -0,0 +1,391 @@
+# coding=utf-8
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+from utilities.utilities import unique_string
+import plpy
+
+def mean_abs_error(
+ schema_madlib, table_in, table_out, prediction_col, observed_col,
+ grouping_cols=None):
+ sql_st1 = """
+ CREATE TABLE {table_out} AS
+ SELECT
+ SUM(ABS({prediction_col}- {observed_col}))
+ /COUNT(*) AS mean_abs_error """.format(**locals())
+ sql_st2= ""
+ sql_st3= """ FROM {table_in} """.format(**locals())
+ sql_st4= ""
+ if grouping_cols:
+ sql_st2= """ , {grouping_cols} """.format(**locals())
+ sql_st4= """ GROUP BY {grouping_cols}""".format(**locals())
+ sql_st = sql_st1+sql_st2+sql_st3+sql_st4
+ plpy.execute(sql_st)
+
+def mean_abs_perc_error(
+ schema_madlib, table_in, table_out, prediction_col, observed_col,
+ grouping_cols=None):
+ sql_st1 = """
+ CREATE TABLE {table_out} AS
+ SELECT
+ SUM(ABS({prediction_col}- {observed_col})/{observed_col})
+ /COUNT(*) AS mean_abs_perc_error """.format(**locals())
+ sql_st2= ""
+ sql_st3= """ FROM {table_in} """.format(**locals())
+ sql_st4= ""
+ if grouping_cols:
+ sql_st2= """ , {grouping_cols} """.format(**locals())
+ sql_st4= """ GROUP BY {grouping_cols}""".format(**locals())
+ sql_st = sql_st1+sql_st2+sql_st3+sql_st4
+ plpy.execute(sql_st)
+
+def mean_perc_error(
+ schema_madlib, table_in, table_out, prediction_col, observed_col,
+ grouping_cols=None):
+ sql_st1 = """
+ CREATE TABLE {table_out} AS
+ SELECT
+ SUM(({prediction_col}- {observed_col})/{observed_col})
+ /COUNT(*) AS mean_perc_error """.format(**locals())
+ sql_st2= ""
+ sql_st3= """ FROM {table_in} """.format(**locals())
+ sql_st4= ""
+ if grouping_cols:
+ sql_st2= """ , {grouping_cols} """.format(**locals())
+ sql_st4= """ GROUP BY {grouping_cols}""".format(**locals())
+ sql_st = sql_st1+sql_st2+sql_st3+sql_st4
+ plpy.execute(sql_st)
+
+def mean_squared_error(
+ schema_madlib, table_in, table_out, prediction_col, observed_col,
+ grouping_cols=None):
+ sql_st1 = """
+ CREATE TABLE {table_out} AS
+ SELECT
+ SUM(({prediction_col}- {observed_col})^2)
+ /COUNT(*) AS mean_squared_error """.format(**locals())
+ sql_st2= ""
+ sql_st3= """ FROM {table_in} """.format(**locals())
+ sql_st4= ""
+ if grouping_cols:
+ sql_st2= """ , {grouping_cols} """.format(**locals())
+ sql_st4= """ GROUP BY {grouping_cols}""".format(**locals())
+ sql_st = sql_st1+sql_st2+sql_st3+sql_st4
+ plpy.execute(sql_st)
+
+
+def __r2_score(
--- End diff --
This scans {table_in} 3 times for no good reason. It would be better to use
the query below, but I'm not sure that's even necessary. Do any of the
functions at
http://www.postgresql.org/docs/9.5/static/functions-aggregate.html#FUNCTIONS-AGGREGATE-STATISTICS-TABLE
do what's necessary here?
CREATE TABLE {table_out} AS
SELECT 1 - ssres/sstot AS r2_score FROM (
SELECT sum(({prediction_col} - {observed_col})^2) AS ssres, sum((
{observed_col} - (SELECT SUM({observed_col})/count(*) AS mean FROM {table_in})
)^2) AS sstot FROM {table_in}
) intermediate
---
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.
---