Encodage one-hot

Machine Learning avec PySpark

Andrew Collier

Data Scientist, Fathom Data

Le problème avec les valeurs indexées

# Comptes pour la catégorie 'type'

+-------+-----+
|   type|count|
+-------+-----+
|Midsize|   22|
|  Small|   21|
|Compact|   16|
| Sporty|   14|
|  Large|   11|
|    Van|    9|
+-------+-----+
# Indices numériques pour la catégorie 'type'

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

Variables indicatrices

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

Chaque modalité devient une colonne.

Machine Learning avec PySpark

Variables indicatrices : codage binaire

+-------+      +-------+-------+-------+-------+-------+-------+
|   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   |
+-------+      +-------+-------+-------+-------+-------+-------+

Des valeurs binaires indiquent la présence (1) ou l'absence (0) de la modalité correspondante.

Machine Learning avec PySpark

Variables indicatrices : représentation clairsemée

+-------+      +-------+-------+-------+-------+-------+-------+      +------+-----+
|   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|
+-------+      +-------+-------+-------+-------+-------+-------+      +------+-----+

Représentation clairsemée : enregistrer l'indice de colonne et la valeur.

Machine Learning avec PySpark

Variables indicatrices : colonne redondante

+-------+      +-------+-------+-------+-------+-------+      +------+-----+
|   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   |      |      |     |
+-------+      +-------+-------+-------+-------+-------+      +------+-----+

Les modalités s'excluent mutuellement, donc on en retire une.

Machine Learning avec PySpark

Encodage one-hot

from pyspark.ml.feature import OneHotEncoder

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

Ajustez l'encodeur aux données.

onehot = onehot.fit(cars)
# Combien de modalités ?
onehot.categorySizes
[6]
Machine Learning avec PySpark

Encodage one-hot

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 avec PySpark

Denses ou clairsemés

from pyspark.mllib.linalg import DenseVector, SparseVector

Enregistrer ce vecteur : [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 avec PySpark

Encoder en one-hot les variables catégorielles

Machine Learning avec PySpark

Preparing Video For Download...