범주형 변수를 컴퓨터가 인식할 수 있게 해주기 위해서는 따로
encoding 과정을 거쳐야 한다.
범주형 변수란?
- 연속형 변수가 아닌 것을 의미한다. 연속하지 않기 때문에 그래프로 표현하게 되면
아래 그래프와 같이 뚝뚝 끊기게 나타난다.
ex ) grade , state , job
대표적으로는
- One - Hot Encoding
- Ordinal - Encoding
- Label Encoding
- Target Encoding
- Binary Encoding
등 다용한 종류가 존재한다.
<One - Hot Encoding>
단어 집합의 크기를 벡터의 차원으로 하고, 표현하고 싶은 단어의 인덱스에 1의 값을 부여하며 다른 인덱스에는 0을 부여하는 단어의 벡터 표현 방식이다.
(고유 값의 개수만큼 컬럼을 만든다는 특징이 있다. )
pd.get_dummies(data[ ])
또는
from category_encoders import OneHotEncoder
#또는
#from sklearn.preprocessing import OneHotEncoder
features = ['Pclass', 'Sex', 'Age', 'SibSp', 'Parch', 'Fare', 'Embarked']
target = 'Survived'
X_train = train[features]
y_train = train[target]
X_val = val[features]
y_val = val[target]
encoder = OneHotEncoder(use_cat_names=True)
X_train_encoded = encoder.fit_transform(X_train)
X_val_encoded = encoder.transform(X_val)
X_train_encoded.head(3)
<Ordinal Encoding>
순서형 자료에 적합한 인코딩 방식이다.
변수가 가지는 순서뿐만 아니라 알파벳 순서까지도 고려할 수 있다.
<Label-encoding>
범주형 변수의 n개 종류의 값들을 1에서 n값으로 숫자를 부여한다.
부여된 숫자들 사이에서는 관계가 존재한다
from sklearn.preprocessing import LabelEncoder
.<Target-encoding>
레이블 인코딩과 비슷하다. 하지만 범주형태의 독립변수들이 종속변수의 값들과 상과관계가 있음을 가정하는 것이 차이점이다. 즉.. 구분으로 끝내는 것이 아닌 의미를 가진 encoding을 하자는 시도이다.
'label'값과 'target' 값 사이의 correlation 효과를 볼 수 있다.
Tip
target encoder 와 cat-boost와 비교를 해보자!!
'Data analysis > Data Process' 카테고리의 다른 글
데이터 기본 전처리 (결측치 제거 , 데이터 타입 / Python)jupyter notebook, Colab (3) | 2021.04.08 |
---|---|
Scikit-learn/선형회귀(Linear Regression) in Python 예제 / 캐글(kaggle) 데이터 사용/Predictive Model (0) | 2021.02.14 |
Simple Regression/ 선형회귀(Linear Regression) (0) | 2021.02.14 |
Matrix Calculation(Matrix multiplication)/행렬의 연산 Python으로 구현하기 (0) | 2021.01.13 |
선형대수 in AI (벡터와 매트릭스vectors and Matrices)/python 파이썬 (0) | 2021.01.13 |