머신러닝 전처리과정 중 범주형 변수와 서열형 변수 처리방법이다.
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로)
# 방법 설명
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)
[ML] 사이킷런 지도 학습 클래스 정리 (0) | 2025.02.12 |
---|---|
[ML/python] 필터링 기반 특징 선택 (Feature selection, Filter Method, SelectKBest) (0) | 2025.02.10 |
[ML/python] 불균형데이터 오버샘플링, 언더샘플링 (SMOTE, NearMiss) (0) | 2025.02.09 |
[ML/python] 스케일링 (정규화, 표준화, Normalization, Min-Max Scaling, Standardization, Z-score Scaling) (0) | 2025.02.09 |
[ML/python] 결측값 처리 (SimpleImputer, KNNImputer) (0) | 2025.02.09 |