C.W.K.
Stream
Lesson 07 of 08 · published

Feature Scaling과 Encoding

~28 min · preprocessing, scaling, encoding

Level 0Scout
0 XP0/48 lessons0/11 achievements
0/120 XP to next level120 XP to go0% complete

scaling이 중요할 때

linear model, distance-based model(kNN, k-means), 신경망은 scaled feature 필요. tree-based model은 안 필요해 — 순서만 신경 써. 대칭 분포엔 StandardScaler, outlier 있으면 RobustScaler, bounded input 필요하면 MinMaxScaler.

categorical encoding

  • One-hot — low-cardinality(~30 이하), 어떤 모델과도 호환.
  • Ordinal — 순서가 의미 있고 모델이 tree-based일 때.
  • Target encoding — cardinality 높고 모델이 tree-based일 때, 항상 CV fold 안에서 적용.
  • Hashing — 메모리가 제약이고 collision 견딜 수 있을 때.

pipeline 배치

scaler와 encoder는 pipeline 안에 살아. training만 보고 fit, validation과 production에 동일하게 apply. raw row의 단일 entrypoint로 column transformer를 유지해. pipeline에 없는 노트북 cell에서 preprocess 절대 하지 마.

Code

혼합 numeric + categorical preprocessing·python
from sklearn.preprocessing import StandardScaler, OneHotEncoder
from sklearn.compose import ColumnTransformer
from sklearn.impute import SimpleImputer
from sklearn.pipeline import Pipeline

numeric_pipe = Pipeline([
    ("impute", SimpleImputer(strategy="median")),
    ("scale", StandardScaler()),
])
categorical_pipe = Pipeline([
    ("impute", SimpleImputer(strategy="most_frequent")),
    ("onehot", OneHotEncoder(handle_unknown="ignore")),
])
preprocess = ColumnTransformer([
    ("num", numeric_pipe, numeric_cols),
    ("cat", categorical_pipe, categorical_cols),
])
category_encoders로 안전한 target encoding·python
from category_encoders import TargetEncoder

te = TargetEncoder(cols=["merchant_id", "city"])  # fit per CV fold
X_tr_enc = te.fit_transform(X_tr, y_tr)
X_val_enc = te.transform(X_val)

External links

Exercise

dataset의 모든 numeric / categorical column을 나열. column 당 scaling/encoding 전략 선택. 다 wrap 하는 ColumnTransformer를 만들고 output shape 확인.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.