One-Hot Encoding

Machine Learning with PySpark

Andrew Collier

Data Scientist, Fathom Data

The problem with indexed values

# Counts for 'type' category

+-------+-----+
|   type|count|
+-------+-----+
|Midsize|   22|
|  Small|   21|
|Compact|   16|
| Sporty|   14|
|  Large|   11|
|    Van|    9|
+-------+-----+
# Numerical indices for 'type' category

+-------+--------+
|   type|type_idx|
+-------+--------+
|Midsize|     0.0|
|  Small|     1.0|
|Compact|     2.0|
| Sporty|     3.0|
|  Large|     4.0|
|    Van|     5.0|
+-------+--------+
Machine Learning with PySpark

Dummy variables

+-------+      +-------+-------+-------+-------+-------+-------+
|   type|      |Midsize|  Small|Compact| Sporty|  Large|    Van|
+-------+      +-------+-------+-------+-------+-------+-------+
|Midsize|      |   X   |       |       |       |       |       |
|  Small|      |       |   X   |       |       |       |       |
|Compact| ===> |       |       |   X   |       |       |       |
| Sporty|      |       |       |       |   X   |       |       |
|  Large|      |       |       |       |       |   X   |       |
|    Van|      |       |       |       |       |       |   X   |
+-------+      +-------+-------+-------+-------+-------+-------+

Each categorical level becomes a column.

Machine Learning with PySpark

Dummy variables: binary encoding

+-------+      +-------+-------+-------+-------+-------+-------+
|   type|      |Midsize|  Small|Compact| Sporty|  Large|    Van|
+-------+      +-------+-------+-------+-------+-------+-------+
|Midsize|      |   1   |   0   |   0   |   0   |   0   |   0   |
|  Small|      |   0   |   1   |   0   |   0   |   0   |   0   |
|Compact| ===> |   0   |   0   |   1   |   0   |   0   |   0   |
| Sporty|      |   0   |   0   |   0   |   1   |   0   |   0   |
|  Large|      |   0   |   0   |   0   |   0   |   1   |   0   |
|    Van|      |   0   |   0   |   0   |   0   |   0   |   1   |
+-------+      +-------+-------+-------+-------+-------+-------+

Binary values indicate the presence (1) or absence (0) of the corresponding level.

Machine Learning with PySpark

Dummy variables: sparse representation

+-------+      +-------+-------+-------+-------+-------+-------+      +------+-----+
|   type|      |Midsize|  Small|Compact| Sporty|  Large|    Van|      |Column|Value|
+-------+      +-------+-------+-------+-------+-------+-------+      +------+-----+
|Midsize|      |   1   |   0   |   0   |   0   |   0   |   0   |      |     0|    1|
|  Small|      |   0   |   1   |   0   |   0   |   0   |   0   |      |     1|    1|
|Compact| ===> |   0   |   0   |   1   |   0   |   0   |   0   | ===> |     2|    1|
| Sporty|      |   0   |   0   |   0   |   1   |   0   |   0   |      |     3|    1|
|  Large|      |   0   |   0   |   0   |   0   |   1   |   0   |      |     4|    1|
|    Van|      |   0   |   0   |   0   |   0   |   0   |   1   |      |     5|    1|
+-------+      +-------+-------+-------+-------+-------+-------+      +------+-----+

Sparse representation: store column index and value.

Machine Learning with PySpark

Dummy variables: redundant column

+-------+      +-------+-------+-------+-------+-------+      +------+-----+
|   type|      |Midsize|  Small|Compact| Sporty|  Large|      |Column|Value|
+-------+      +-------+-------+-------+-------+-------+      +------+-----+
|Midsize|      |   1   |   0   |   0   |   0   |   0   |      |     0|    1|
|  Small|      |   0   |   1   |   0   |   0   |   0   |      |     1|    1|
|Compact| ===> |   0   |   0   |   1   |   0   |   0   | ===> |     2|    1|
| Sporty|      |   0   |   0   |   0   |   1   |   0   |      |     3|    1|
|  Large|      |   0   |   0   |   0   |   0   |   1   |      |     4|    1|
|    Van|      |   0   |   0   |   0   |   0   |   0   |      |      |     |
+-------+      +-------+-------+-------+-------+-------+      +------+-----+

Levels are mutually exclusive, so drop one.

Machine Learning with PySpark

One-hot encoding

from pyspark.ml.feature import OneHotEncoder

onehot = OneHotEncoder(inputCols=['type_idx'], outputCols=['type_dummy'])

Fit the encoder to the data.

onehot = onehot.fit(cars)
# How many category levels?
onehot.categorySizes
[6]
Machine Learning with PySpark

One-hot encoding

cars = onehot.transform(cars)
cars.select('type', 'type_idx', 'type_dummy').distinct().sort('type_idx').show()
+-------+--------+-------------+
|   type|type_idx|   type_dummy|
+-------+--------+-------------+
|Midsize|     0.0|(5,[0],[1.0])|
|  Small|     1.0|(5,[1],[1.0])|
|Compact|     2.0|(5,[2],[1.0])|
| Sporty|     3.0|(5,[3],[1.0])|
|  Large|     4.0|(5,[4],[1.0])|
|    Van|     5.0|    (5,[],[])|
+-------+--------+-------------+
Machine Learning with PySpark

Dense versus sparse

from pyspark.mllib.linalg import DenseVector, SparseVector

Store this vector: [1, 0, 0, 0, 0, 7, 0, 0].

DenseVector([1, 0, 0, 0, 0, 7, 0, 0])
DenseVector([1.0, 0.0, 0.0, 0.0, 0.0, 7.0, 0.0, 0.0])
SparseVector(8, [0, 5], [1, 7])
SparseVector(8, {0: 1.0, 5: 7.0})
Machine Learning with PySpark

One-Hot Encode categoricals

Machine Learning with PySpark

Preparing Video For Download...