범주형 변수 인코딩
scikit-learn으로 모델링하기 전에 인코딩이 필요한 UFO 데이터셋의 열이 몇 개 있어요. 여기서는 이 변환을 이진 인코딩과 원-핫 인코딩 두 가지 방식으로 수행해 보겠습니다.
이 연습은 강의의 일부입니다
Python으로 배우는 Machine Learning 전처리
연습 안내
apply()를 사용해 값이"us"이면1을, 그렇지 않으면 0을 반환하는 조건부lambda함수를 작성하세요.type열의.unique()값 개수를 출력하세요.pd.get_dummies()를 사용해type열에 대해 원-핫 인코딩된 집합을 만드세요.- 마지막으로
pd.concat()을 사용해 인코딩된type_set변수를ufo데이터셋과 연결하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
# Use pandas to encode us values as 1 and others as 0
ufo["country_enc"] = ufo["country"].____
# Print the number of unique type values
print(len(____.unique()))
# Create a one-hot encoded set of the type values
type_set = ____
# Concatenate this set back to the ufo DataFrame
ufo = pd.concat([____, ____], axis=1)