범주형 열 인코딩 III: DictVectorizer
이제 파이프라인으로 들어가기 전에 마지막으로 한 가지 요령을 살펴보겠습니다. 방금 수행하신 두 단계(LabelEncoder 후에 OneHotEncoder)는 DictVectorizer를 사용하면 더 간단히 처리할 수 있어요.
DataFrame을 사전(dict)으로 변환한 뒤 DictVectorizer를 적용하면, 라벨 인코딩과 원-핫 인코딩을 한 번에 모두 수행할 수 있습니다.
이번 연습 문제에서는 이 방법을 직접 적용해 보세요!
이 연습은 강의의 일부입니다
XGBoost로 익히는 Extreme Gradient Boosting
연습 안내
sklearn.feature_extraction에서DictVectorizer를 임포트하세요..to_dict()메서드에 인수로"records"를 사용해df를 사전으로 변환하고, 이를df_dict라고 하세요.- 키워드 인수
sparse=False로DictVectorizer객체를 생성하고 이름을dv로 하세요. .fit_transform()메서드를 사용해df_dict에DictVectorizer를 적용하세요.- 결과의 처음 다섯 행과 vocabulary를 출력하려면 'Submit Answer'를 누르세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
# Import DictVectorizer
____
# Convert df into a dictionary: df_dict
df_dict = ____
# Create the DictVectorizer object: dv
dv = ____
# Apply dv on df: df_encoded
df_encoded = ____
# Print the resulting first five rows
print(df_encoded[:5,:])
# Print the vocabulary
print(dv.vocabulary_)