원-핫 인코딩

PySpark로 하는 Machine Learning

Andrew Collier

Data Scientist, Fathom Data

인덱스 값의 문제점

# 'type' 범주 빈도

+-------+-----+
|   type|count|
+-------+-----+
|Midsize|   22|
|  Small|   21|
|Compact|   16|
| Sporty|   14|
|  Large|   11|
|    Van|    9|
+-------+-----+
# 'type' 범주의 수치 인덱스

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

더미 변수

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

각 범주 수준이 하나의 열이 됩니다.

PySpark로 하는 Machine Learning

더미 변수: 이진 인코딩

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

이진 값은 해당 수준의 존재(‘1’) 또는 부재(‘0’)를 나타냅니다.

PySpark로 하는 Machine Learning

더미 변수: 희소 표현

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

희소 표현: 열 인덱스와 값만 저장합니다.

PySpark로 하는 Machine Learning

더미 변수: 중복 열

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

수준은 상호 배타적이므로 하나는 제거합니다.

PySpark로 하는 Machine Learning

원-핫 인코딩

from pyspark.ml.feature import OneHotEncoder

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

인코더를 데이터에 맞춥니다.

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

원-핫 인코딩

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,[],[])|
+-------+--------+-------------+
PySpark로 하는 Machine Learning

밀집 대 희소

from pyspark.mllib.linalg import DenseVector, SparseVector

이 벡터를 저장합니다: [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})
PySpark로 하는 Machine Learning

범주형 원-핫 인코딩

PySpark로 하는 Machine Learning

Preparing Video For Download...