You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@madlib.apache.org by ri...@apache.org on 2018/03/27 22:46:15 UTC

[3/5] madlib git commit: RF: Use NULL::integer[] when no continuous features

RF: Use NULL::integer[] when no continuous features

JIRA: MADLIB-1219

When variable importance is enabled, to compute importance score,
distribution of the categorical and continuous features are computed.
For continuous features, this function requires initializing a vector of
length = number of continuous features. When there are no continuous
features, this initialization fails.

This commit fixes the issue by inputing a NULL::integer[] vector when
there no continuous features.

Closes #249


Project: http://git-wip-us.apache.org/repos/asf/madlib/repo
Commit: http://git-wip-us.apache.org/repos/asf/madlib/commit/67e7cb27
Tree: http://git-wip-us.apache.org/repos/asf/madlib/tree/67e7cb27
Diff: http://git-wip-us.apache.org/repos/asf/madlib/diff/67e7cb27

Branch: refs/heads/master
Commit: 67e7cb272c19619c73454072544f62479fd1919c
Parents: c35107d
Author: Rahul Iyer <ri...@apache.org>
Authored: Sun Mar 25 21:45:47 2018 -0700
Committer: Rahul Iyer <ri...@apache.org>
Committed: Tue Mar 27 15:29:43 2018 -0700

----------------------------------------------------------------------
 .../recursive_partitioning/random_forest.py_in       | 15 +++++++++------
 .../recursive_partitioning/test/random_forest.sql_in |  3 ++-
 2 files changed, 11 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/madlib/blob/67e7cb27/src/ports/postgres/modules/recursive_partitioning/random_forest.py_in
----------------------------------------------------------------------
diff --git a/src/ports/postgres/modules/recursive_partitioning/random_forest.py_in b/src/ports/postgres/modules/recursive_partitioning/random_forest.py_in
index 3961df4..ce6ef7f 100644
--- a/src/ports/postgres/modules/recursive_partitioning/random_forest.py_in
+++ b/src/ports/postgres/modules/recursive_partitioning/random_forest.py_in
@@ -946,6 +946,11 @@ def _calculate_oob_prediction(
 
     oob_var_dist_view = unique_string()
     if importance:
+        if con_features:
+            initialized_float_array = ("{0}.array_of_float({1})::integer[]".
+                                       format(schema_madlib, len(con_features)))
+        else:
+            initialized_float_array = "NULL::integer[]"
         sql_create_oob_var_dist_view = """
             CREATE VIEW {oob_var_dist_view} AS
             SELECT
@@ -966,12 +971,10 @@ def _calculate_oob_prediction(
                             {con_features_str}::double precision[],
                             con_splits
                         ), -- bin_indices, -1 for NaN
-                        1 -- -1 shift to 0 for nulls
+                        1 -- -1 shifted to 0 for null values
                     ),
-                    {schema_madlib}.array_fill(
-                        {schema_madlib}.array_of_float({n_con})::integer[],
-                        ({num_bins}+1)::integer
-                    )
+                    {schema_madlib}.array_fill({initialized_float_array},
+                                               ({num_bins} + 1)::integer)
                     -- level of any continuous feature == num_bins
                 ) AS con_index_distributions
             FROM
@@ -986,7 +989,7 @@ def _calculate_oob_prediction(
                 {con_splits_table}
             USING (gid)
             GROUP BY gid
-        """.format(n_con=len(con_features), **locals())
+        """.format(**locals())
     else:
         sql_create_oob_var_dist_view = """
             CREATE VIEW {oob_var_dist_view} AS

http://git-wip-us.apache.org/repos/asf/madlib/blob/67e7cb27/src/ports/postgres/modules/recursive_partitioning/test/random_forest.sql_in
----------------------------------------------------------------------
diff --git a/src/ports/postgres/modules/recursive_partitioning/test/random_forest.sql_in b/src/ports/postgres/modules/recursive_partitioning/test/random_forest.sql_in
index 8fcb75b..6ab00a7 100644
--- a/src/ports/postgres/modules/recursive_partitioning/test/random_forest.sql_in
+++ b/src/ports/postgres/modules/recursive_partitioning/test/random_forest.sql_in
@@ -265,6 +265,7 @@ from generate_series(1, 5) sid;
 
 -------------------------------------------------------------------------
 -- Test case for the case where a group has a filtered feature
+-- Also testing var_importance=TRUE with no continuous features (MADLIB-1219)
 
 DROP TABLE IF EXISTS rf_gr_test CASCADE;
 CREATE TABLE rf_gr_test (
@@ -289,7 +290,7 @@ SELECT forest_train(
                   'train_output',    -- output model table
                   'id',              -- id column
                   'cl',           -- response
-                  'f1, f2, f3',   -- features
+                  'f1, f2',   -- features
                   NULL,        -- exclude columns
                   'gr',        -- grouping
                   2,                -- num of trees