You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hivemall.apache.org by my...@apache.org on 2019/11/28 07:11:24 UTC

[incubator-hivemall] branch master updated: [HIVEMALL-159][DOC] Add documentation about One-hot encoding

This is an automated email from the ASF dual-hosted git repository.

myui pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-hivemall.git


The following commit(s) were added to refs/heads/master by this push:
     new 23a21d9  [HIVEMALL-159][DOC] Add documentation about One-hot encoding
23a21d9 is described below

commit 23a21d9acce1e4ab3bb6ec06ce7d7bb9baa096d9
Author: Makoto Yui <my...@apache.org>
AuthorDate: Thu Nov 28 16:11:17 2019 +0900

    [HIVEMALL-159][DOC] Add documentation about One-hot encoding
    
    ## What changes were proposed in this pull request?
    
    Add documentation about One-hot encoding
    
    ## What type of PR is it?
    
    Documentation
    
    ## What is the Jira issue?
    
    https://issues.apache.org/jira/browse/HIVEMALL-159
    
    ## How to use this feature?
    
    See userguide
    
    Author: Makoto Yui <my...@apache.org>
    
    Closes #223 from myui/onehot_docs.
---
 .../hivemall/ftvec/hashing/FeatureHashingUDF.java  |   6 +-
 .../hivemall/ftvec/trans/OnehotEncodingUDAF.java   |  21 ++-
 .../java/hivemall/smile/tools/DecisionPathUDF.java |   2 +-
 .../hivemall/smile/tools/GuessAttributesUDF.java   |   4 +-
 docs/gitbook/SUMMARY.md                            |   1 +
 docs/gitbook/ft_engineering/onehot.md              | 205 +++++++++++++++++++++
 docs/gitbook/misc/funcs.md                         |  29 ++-
 7 files changed, 256 insertions(+), 12 deletions(-)

diff --git a/core/src/main/java/hivemall/ftvec/hashing/FeatureHashingUDF.java b/core/src/main/java/hivemall/ftvec/hashing/FeatureHashingUDF.java
index 0bc1c97..fc2aed7 100644
--- a/core/src/main/java/hivemall/ftvec/hashing/FeatureHashingUDF.java
+++ b/core/src/main/java/hivemall/ftvec/hashing/FeatureHashingUDF.java
@@ -51,13 +51,13 @@ import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectIn
         value = "_FUNC_(array<string> features [, const string options])"
                 + " - returns a hashed feature vector in array<string>",
         extended = "select feature_hashing(array('aaa:1.0','aaa','bbb:2.0'), '-libsvm');\n" + 
-                "> [\"4063537:1.0\",\"4063537:1\",\"8459207:2.0\"]\n" + 
+                " [\"4063537:1.0\",\"4063537:1\",\"8459207:2.0\"]\n" + 
                 "\n" + 
                 "select feature_hashing(array('aaa:1.0','aaa','bbb:2.0'), '-features 10');\n" + 
-                "> [\"7:1.0\",\"7\",\"1:2.0\"]\n" + 
+                " [\"7:1.0\",\"7\",\"1:2.0\"]\n" + 
                 "\n" + 
                 "select feature_hashing(array('aaa:1.0','aaa','bbb:2.0'), '-features 10 -libsvm');\n" + 
-                "> [\"1:2.0\",\"7:1.0\",\"7:1\"]\n" + 
+                " [\"1:2.0\",\"7:1.0\",\"7:1\"]\n" + 
                 "")
 //@formatter:on
 @UDFType(deterministic = true, stateful = false)
diff --git a/core/src/main/java/hivemall/ftvec/trans/OnehotEncodingUDAF.java b/core/src/main/java/hivemall/ftvec/trans/OnehotEncodingUDAF.java
index ca76fab..203eebb 100644
--- a/core/src/main/java/hivemall/ftvec/trans/OnehotEncodingUDAF.java
+++ b/core/src/main/java/hivemall/ftvec/trans/OnehotEncodingUDAF.java
@@ -54,9 +54,28 @@ import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectIn
 import org.apache.hadoop.hive.serde2.typeinfo.TypeInfo;
 import org.apache.hadoop.io.Writable;
 
+//@formatter:off
 @Description(name = "onehot_encoding",
-        value = "_FUNC_(PRIMITIVE feature, ...) - Compute onehot encoded label for each feature")
+        value = "_FUNC_(PRIMITIVE feature, ...) - Compute onehot encoded label for each feature",
+        extended = "WITH mapping as (\n" + 
+                "  select \n" + 
+                "    m.f1, m.f2 \n" + 
+                "  from (\n" + 
+                "    select onehot_encoding(species, category) m\n" + 
+                "    from test\n" + 
+                "  ) tmp\n" + 
+                ")\n" + 
+                "select\n" + 
+                "  array(m.f1[t.species],m.f2[t.category],feature('count',count)) as sparse_features\n" + 
+                "from\n" + 
+                "  test t\n" + 
+                "  CROSS JOIN mapping m;\n" + 
+                "\n" + 
+                "[\"2\",\"8\",\"count:9\"]\n" + 
+                "[\"5\",\"8\",\"count:10\"]\n" + 
+                "[\"1\",\"6\",\"count:101\"]")
 @UDFType(deterministic = true, stateful = true)
+//@formatter:on
 public final class OnehotEncodingUDAF extends AbstractGenericUDAFResolver {
 
     public OnehotEncodingUDAF() {
diff --git a/core/src/main/java/hivemall/smile/tools/DecisionPathUDF.java b/core/src/main/java/hivemall/smile/tools/DecisionPathUDF.java
index fe1dc18..9b4be97 100644
--- a/core/src/main/java/hivemall/smile/tools/DecisionPathUDF.java
+++ b/core/src/main/java/hivemall/smile/tools/DecisionPathUDF.java
@@ -66,7 +66,7 @@ import org.apache.hadoop.io.Text;
                 "  model_rf m\n" + 
                 "  LEFT OUTER JOIN\n" + 
                 "  test_rf t;\n" +
-                "> | 892 | [\"2 [0.0] = 0.0\",\"0 [3.0] = 3.0\",\"1 [696.0] != 107.0\",\"7 [7.8292] <= 7.9104\",\"1 [696.0] != 828.0\",\"1 [696.0] != 391.0\",\"0 [0.961038961038961, 0.03896103896103896]\"] |\n\n" +
+                " | 892 | [\"2 [0.0] = 0.0\",\"0 [3.0] = 3.0\",\"1 [696.0] != 107.0\",\"7 [7.8292] <= 7.9104\",\"1 [696.0] != 828.0\",\"1 [696.0] != 391.0\",\"0 [0.961038961038961, 0.03896103896103896]\"] |\n\n" +
                 "-- Show 100 frequent branches\n" +
                 "WITH tmp as (\n" + 
                 "  SELECT\n" + 
diff --git a/core/src/main/java/hivemall/smile/tools/GuessAttributesUDF.java b/core/src/main/java/hivemall/smile/tools/GuessAttributesUDF.java
index 5d79a18..be0c135 100644
--- a/core/src/main/java/hivemall/smile/tools/GuessAttributesUDF.java
+++ b/core/src/main/java/hivemall/smile/tools/GuessAttributesUDF.java
@@ -32,8 +32,8 @@ import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorUtils;
 import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;
 
 @Description(name = "guess_attribute_types", value = "_FUNC_(ANY, ...) - Returns attribute types",
-        extended = "select guess_attribute_types(*) from train limit 1;"
-                + "\n> Q,Q,C,C,C,C,Q,C,C,C,Q,C,Q,Q,Q,Q,C,Q")
+        extended = "select guess_attribute_types(*) from train limit 1;\n"
+                + " Q,Q,C,C,C,C,Q,C,C,C,Q,C,Q,Q,Q,Q,C,Q")
 @UDFType(deterministic = true, stateful = false)
 public final class GuessAttributesUDF extends GenericUDF {
 
diff --git a/docs/gitbook/SUMMARY.md b/docs/gitbook/SUMMARY.md
index 9e86cf7..6739ee3 100644
--- a/docs/gitbook/SUMMARY.md
+++ b/docs/gitbook/SUMMARY.md
@@ -66,6 +66,7 @@
     * [Feature vectorization](ft_engineering/vectorization.md)
     * [Quantify non-number features](ft_engineering/quantify.md)
     * [Binarize label](ft_engineering/binarize.md)
+    * [One-hot encoding](ft_engineering/onehot.md)
 * [Term Vector Model](ft_engineering/term_vector.md)
     * [TF-IDF Term Weighting](ft_engineering/tfidf.md)
     * [Okapi BM25 Term Weighting](ft_engineering/bm25.md)
diff --git a/docs/gitbook/ft_engineering/onehot.md b/docs/gitbook/ft_engineering/onehot.md
new file mode 100644
index 0000000..bd50f6c
--- /dev/null
+++ b/docs/gitbook/ft_engineering/onehot.md
@@ -0,0 +1,205 @@
+<!--
+  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.
+-->
+
+<!-- toc -->
+
+# What's One-hot encoding?
+
+Ont-hot encoding is a method to encode categorical features by a 1-of-K (thus called 1-hot) encoding scheme.
+
+Suppose the following table:
+
+| Company | Price |
+|:-:|:-:|
+| VW | 290 |
+| Toyota | 300 |
+| Honda | 190 |
+| Honda | 250 |
+
+A one-hot encoding output is expected as follows:
+
+| Company_VW | Company_Toyota | Company_Honda | Price |
+|:-:|:-:|:-:|:-:|
+| 1 | 0 | 0 | 290 |
+| 0 | 1 | 0 | 300 |
+| 0 | 0 | 1 | 190 |
+| 0 | 0 | 1 | 250 |
+
+The above one-hot table is a dense feature format and it can be expressed as follows by a sparse format:
+
+| Company | Price |
+|:-:|:-:|
+| {1} | 290 |
+| {2} | 300 |
+| {3} | 190 |
+| {3} | 250 |
+
+The mapping for company name is {VW->1, Toyota->2, Honda->3}.
+
+Now, suppose encoding two categorical variables as follows into a sparse vector.
+
+| category1 | category2 |
+|:-:|:-:|
+|cat | mammal |
+|dog |mammal |
+|human | mammal |
+|seahawk | bird |
+|wasp | insect |
+|wasp | insect |
+|cat | mammal |
+|dog | mammal |
+|human | mammal |
+
+The one-hot encoded feature vector could be as follows:
+
+| category1 | category2 | encoded_features |
+|:-:|:-:|:-:|
+| cat | mammal | {1,6} |
+| dog | mammal | {2,6} |
+| human | mammal | {3,6} |
+| seahawk | bird | {4,7} |
+| wasp | insect | {5,8} |
+
+We use this `test` table for explaration.
+
+```sql
+drop table test;
+create table test (species string, category string, count int);
+
+truncate table test;
+insert into table test values
+  ('cat','mammal',9), 
+  ('dog','mammal',10),
+  ('human','mammal',10),
+  ('seahawk','bird',101),
+  ('wasp','insect',3),
+  ('wasp','insect',9),
+  ('cat','mammal',101),
+  ('dog','mammal',1),
+  ('human','mammal',9);
+```
+
+# One-hot encoding table
+
+You can get one-hot encoding table for spieces as follows:
+
+```sql
+WITH t as (
+  select onehot_encoding(species) m
+  from test
+)
+select m.f1 from t;
+```
+
+| f1 |
+|:-|
+| {"seahawk":1,"cat":2,"human":3,"wasp":4,"dog":5} |
+
+```sql
+WITH t as (
+  select onehot_encoding(species, category) m
+  from test
+)
+select m.f1, m.f2 from t;
+```
+
+| f1 | f2 |
+| {"seahawk":1,"cat":2,"human":3,"wasp":4,"dog":5} | {"bird":6,"insect":7,"mammal":8} |
+
+You can create a mapping table as follows:
+
+```sql
+create table mapping as
+WITH t as (
+  select onehot_encoding(species, category) m
+  from test
+)
+select m.f1, m.f2 from t;
+
+desc mapping;
+
+col_name    | data_type
+------------|----------------
+f1          | map<string,int>                             
+f2          | map<string,int>   
+```
+
+# How to use One-hot encoding
+
+The following query applies one-hot encoding using the mapping table.
+
+```sql
+select
+  t.species, m.f1[t.species],
+  t.category, m.f2[t.category]
+from
+  test t
+  CROSS JOIN mapping m;
+
+cat 2   mammal  8 
+dog 5   mammal  8 
+human   3   mammal  8 
+seahawk 1   bird    6 
+wasp    4   insect  7 
+wasp    4   insect  7 
+cat 2   mammal  8 
+dog 5   mammal  8
+human   3   mammal  8
+```
+
+You can create a sparse feature vector as follows:
+
+```sql
+select
+  array(m.f1[t.species],m.f2[t.category],feature('count',count)) as sparse_feature 
+from
+  test t
+  CROSS JOIN mapping m;
+
+sparse_feature
+["2","8","count:9"]
+["5","8","count:10"]
+["3","8","count:10"]
+["1","6","count:101"]
+["4","7","count:3"]
+["4","7","count:9"]
+["2","8","count:101"]
+["5","8","count:1"]
+["3","8","count:9"]
+```
+
+It also can be achieved by a single query as follows:
+
+```sql
+WITH mapping as (
+  select 
+    m.f1, m.f2 
+  from (
+    select onehot_encoding(species, category) m
+    from test
+  ) tmp
+)
+select
+  array(m.f1[t.species],m.f2[t.category],feature('count',count)) as sparse_features
+from
+  test t
+  CROSS JOIN mapping m;
+```
+
+Note that one-hot encoding is required only for categorical variables. Feature hasing is another scalable way to encode categorical variables to numerical index.
\ No newline at end of file
diff --git a/docs/gitbook/misc/funcs.md b/docs/gitbook/misc/funcs.md
index e04713b..c986c5a 100644
--- a/docs/gitbook/misc/funcs.md
+++ b/docs/gitbook/misc/funcs.md
@@ -327,13 +327,13 @@ Reference: <a href="https://papers.nips.cc/paper/3848-adaptive-regularization-of
 - `feature_hashing(array<string> features [, const string options])` - returns a hashed feature vector in array&lt;string&gt;
   ```sql
   select feature_hashing(array('aaa:1.0','aaa','bbb:2.0'), '-libsvm');
-  > ["4063537:1.0","4063537:1","8459207:2.0"]
+   ["4063537:1.0","4063537:1","8459207:2.0"]
 
   select feature_hashing(array('aaa:1.0','aaa','bbb:2.0'), '-features 10');
-  > ["7:1.0","7","1:2.0"]
+   ["7:1.0","7","1:2.0"]
 
   select feature_hashing(array('aaa:1.0','aaa','bbb:2.0'), '-features 10 -libsvm');
-  > ["1:2.0","7:1.0","7:1"]
+   ["1:2.0","7:1.0","7:1"]
 
   ```
 
@@ -388,6 +388,25 @@ Reference: <a href="https://papers.nips.cc/paper/3848-adaptive-regularization-of
 - `indexed_features(double v1, double v2, ...)` - Returns a list of features as array&lt;string&gt;: [1:v1, 2:v2, ..]
 
 - `onehot_encoding(PRIMITIVE feature, ...)` - Compute onehot encoded label for each feature
+  ```sql
+  WITH mapping as (
+    select 
+      m.f1, m.f2 
+    from (
+      select onehot_encoding(species, category) m
+      from test
+    ) tmp
+  )
+  select
+    array(m.f1[t.species],m.f2[t.category],feature('count',count)) as sparse_features
+  from
+    test t
+    CROSS JOIN mapping m;
+
+  ["2","8","count:9"]
+  ["5","8","count:10"]
+  ["1","6","count:101"]
+  ```
 
 - `quantified_features(boolean output, col1, col2, ...)` - Returns an identified features in a dense array&lt;double&gt;
 
@@ -905,7 +924,7 @@ Reference: <a href="https://papers.nips.cc/paper/3848-adaptive-regularization-of
     model_rf m
     LEFT OUTER JOIN
     test_rf t;
-  > | 892 | ["2 [0.0] = 0.0","0 [3.0] = 3.0","1 [696.0] != 107.0","7 [7.8292] <= 7.9104","1 [696.0] != 828.0","1 [696.0] != 391.0","0 [0.961038961038961, 0.03896103896103896]"] |
+   | 892 | ["2 [0.0] = 0.0","0 [3.0] = 3.0","1 [696.0] != 107.0","7 [7.8292] <= 7.9104","1 [696.0] != 828.0","1 [696.0] != 391.0","0 [0.961038961038961, 0.03896103896103896]"] |
 
   -- Show 100 frequent branches
   WITH tmp as (
@@ -932,7 +951,7 @@ Reference: <a href="https://papers.nips.cc/paper/3848-adaptive-regularization-of
 - `guess_attribute_types(ANY, ...)` - Returns attribute types
   ```sql
   select guess_attribute_types(*) from train limit 1;
-  > Q,Q,C,C,C,C,Q,C,C,C,Q,C,Q,Q,Q,Q,C,Q
+   Q,Q,C,C,C,C,Q,C,C,C,Q,C,Q,Q,Q,Q,C,Q
   ```
 
 - `rf_ensemble(int yhat [, array<double> proba [, double model_weight=1.0]])` - Returns ensembled prediction results in &lt;int label, double probability, array&lt;double&gt; probabilities&gt;