범주형 변수로 모델링하기
이전 연습 문제에서 width와 함께 설명 변수로 color를 사용해 로지스틱 회귀 모델을 적합했는데, 그때는 color를 양적 변수로 취급했어요. 이번 연습에서는 color를 범주형 변수로 취급합니다. 모델 행렬을 구성할 때 color는 0/1 인코딩으로 3개의 변수로 변환돼요.
기본적으로 dmatrix()에서는 첫 번째 그룹이 기준 그룹으로 인코딩됩니다. 모델 행렬을 데이터프레임으로 보려면 dmatrix()의 추가 인수인 return_type을 'dataframe'으로 설정하세요.
color 변수에는 다음과 같은 자연스러운 순서가 있어요:
1: medium light
2: medium
3: medium dark
4: dark
crab 데이터셋은 작업 공간에 미리 로드되어 있습니다.
이 연습은 강의의 일부입니다
Python으로 배우는 Generalized Linear Models
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
# Construct model matrix
model_matrix = ____('C(____, ____(____))' , data = ____,
return_type = 'dataframe')
# Print first 5 rows of model matrix dataframe
print(____.____)
# Fit and print the results of a glm model with the above model matrix configuration
model = ____('____ ~ ____(____, ____(____))', data = ____,
family = ____).____
print(____.____)