Logistic Regression 파라미터 추출하기
이제 로지스틱 회귀 모델의 중요한 파라미터를 추출하는 연습을 해 보겠습니다. 로지스틱 회귀에는 여기서 다루지 않는 다른 파라미터들도 있지만, scikit-learn.org의 LogisticRegression() 문서에서 'Attributes' 항목을 참고해 복습할 수 있어요.
이 파라미터는 각 변수들이 타깃에 미치는 영향의 방향과 크기를 이해하는 데 중요합니다.
이번 연습에서는 계수 파라미터(coef_ 속성)를 추출해 원래 열 이름과 함께 zip으로 묶고, 어떤 변수가 타깃 변수에 가장 큰 양의 영향을 주었는지 확인해 볼 거예요.
다음이 제공됩니다:
log_reg_clf라는 로지스틱 회귀 모델 객체X_trainDataFrame
sklearn과 pandas는 이미 임포트되어 있어요.
이 연습은 강의의 일부입니다
Python에서의 하이퍼파라미터 튜닝
연습 안내
- 학습용 DataFrame에서 사용한 원래 열 이름 목록을 만드세요.
- 로지스틱 회귀 추정기의 계수를 추출하세요.
- 계수와 변수 이름으로 DataFrame을 만들고 확인하세요.
- 계수 크기를 기준으로 ‘양의’ 변수를 상위 3개까지 출력하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
# Create a list of original variable names from the training DataFrame
original_variables = ____
# Extract the coefficients of the logistic regression estimator
model_coefficients = ____.____[____]
# Create a dataframe of the variables and coefficients & print it out
coefficient_df = pd.DataFrame({"Variable" : ____, "Coefficient": ____})
print(coefficient_df)
# Print out the top 3 positive variables
top_three_df = coefficient_df.sort_values(by=____, axis=0, ascending=____)[0:____]
print(top_three_df)