시작하기무료로 시작하기

라벨 인코딩

범주형 변수 인코딩을 연습해 보겠습니다. 이번에도 Kaggle의 House Prices 대회 데이터 일부를 사용해요.

목표는 범주형 특성 "RoofStyle"과 "CentralAir"를 라벨 인코딩으로 변환하는 것입니다. traintest DataFrame은 워크스페이스에 이미 준비되어 있어요.

이 연습은 강의의 일부입니다

Python으로 Kaggle 대회 공략하기

강의 보기

연습 안내

  • traintest DataFrame을 하나로 이어 붙여 단일 DataFrame houses를 만드세요.
  • 인자를 주지 않고 LabelEncoder 객체를 생성해 le에 할당하세요.
  • 동일한 le 객체를 사용해 "RoofStyle"과 "CentralAir"에 대한 새 라벨 인코딩 특성을 만드세요.

실습형 인터랙티브 연습

이 예제를 이 샘플 코드를 완성하여 풀어보세요.

# Concatenate train and test together
houses = ____.____([train, test])

# Label encoder
from sklearn.preprocessing import LabelEncoder
le = ____()

# Create new features
houses['RoofStyle_enc'] = le.fit_transform(houses[____])
houses['CentralAir_enc'] = ____.____(____[____])

# Look at new features
print(houses[['RoofStyle', 'RoofStyle_enc', 'CentralAir', 'CentralAir_enc']].head())
코드 편집 및 실행