상세 컨텐츠

본문 제목

[ML/python] 범주형, 서열형 변수 처리 (더미화, 치환)

테크/ML

by fiftyline 2025. 2. 9. 17:26

본문

 

머신러닝 전처리과정 중 범주형 변수와 서열형 변수 처리방법이다.

 

df = pd.read_csv("mldata.csv")
X = df.drop('y', axis = 1)
y = df['y']
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state = 50)

 

 

더미화

from sklearn.preprocessing import OneHotEncoder
one = OneHotEncoder(sparse = False) #False:numpy배열, True:희소행렬
one.get_feature_names() #원본 열이름

ct = make_column_transformer(
    (StandardScaler(), ['score','weight']),
    (OneHotEncoder(sparse = False),['grade','class','gender'])
)
ct.fit(X_train)
X_train_trans = ct.transform(X_train)




# feature_engine 라이브러리 활용 방법
from feature_engine.encoding import OneHotEncoder as OHE
dummy_model = OHE(top_categories=5, drop_last = True).fit(X_train) #상위5개
Z_train = dummy_model.transform(X_train)
Z_test = dummy_model.transform(X_test)
# 입출력 모두 Df, 기존열 유지, 값 수 제한 가능(상위N개 카테고리만 인코딩후 나머지는 Other로)

 

라벨(y)을 활용한 치환

# 방법 설명
S = df.groupby('x1')['y'].mean() # 범주형변수(x1)의 값에 따라 y의 평균 계산
df['x1'].replace(S.to_dict()) # 딕셔너리로 변환 후 기존 x1값에 따라 연속형(y의 평균)으로 대체 
# -------------------------------

# 계산을 위해 X와 y 붙이기
train = pd.concat([X_train, y_train], axis = 1)

# 범주형 변수에 대해서만 치환
for col, dtype in zip(X_train.columns, X_train.dtypes):
    if dtype == object:
        S = train.groupby(col)['y'].mean().to_dict()
        X_train.loc[:, col] = X_train[col].replace(S)
        X_test.loc[:, col] = X_test[col].replace(S)

display(X_train['x1'].head())
display(X_test['x1'].head())



 

 

 

 

참고 서적 : GIL's LAB, 「파이썬을 활용한 머신러닝 자동화 시스템 구축」, 위키북스(2022)

관련글 더보기