ML_DL/딥러닝 공부하기
[Preprocessing] Categorical Feature Encoding
swwho
2023. 4. 25. 15:59
728x90
반응형
Encoding
- categorical feature를 numerical feature로 변환하는 작업
OneHotEncoder
- 해당 범주를 1, 나머지는 0으로 하는 feature가 새로 생성된다.
ohe = OneHotEncoder(sparse=False)
a = pd.DataFrame(ohe.fit_transform(df[['weather']]), columns=['ranniy', 'sunny', 'windy'])
df_a = pd.concat([df.drop('weather', axis=1), a], axis=1)
df_a
LabelEncoder
- categorie feature의 범주에 정수값을 부여한다. (ex - sunny 1, rainny 2, windy 3)
- 순서대로 정수값이 부여되기 때문에 범주간 순서가 없다면 사용을 지양한다. (sunny의 2배가 rainny인 것은 관계가 없다)
lbe = LabelEncoder()
b = pd.DataFrame(lbe.fit_transform(df['weather']))
b_df = pd.concat([df.drop('weather', axis=1), b], axis=1)
b_df
get_dummies
- dataframe을 넣으면 categorie feature만을 numerical feature로 변환한다.
c = pd.get_dummies(df)
c