AUC 곡선 만들기
전진 단계적 변수 선택 절차는 예측 변수 집합에 변수를 최적으로 추가하는 순서를 제공합니다. 어떤 지점에서 변수를 잘라낼지 결정하려면 학습과 테스트 AUC 곡선을 만들어 볼 수 있어요. 이 곡선은 모델에서 첫 번째 변수, 처음 두 개, 처음 세 개 … 식으로 사용할 때의 학습 및 테스트 AUC를 그립니다.
이번 연습에서는 이러한 AUC 곡선을 그리는 방법을 배웁니다. AUC 값을 계산하는 메서드 auc_train_test는 이미 구현되어 있으며 다음과 같이 사용할 수 있어요:
auc_train, auc_test = auc_train_test(variables, target, train, test)
여기서 variables는 로지스틱 회귀 모델에 사용하는 변수 집합, target은 타깃 이름이 담긴 리스트, train과 test는 각각 학습과 테스트 베이스테이블이에요.
전진 단계적 절차에 따라 정렬된 변수들은 리스트 variables에 제공됩니다. 콘솔에서 살펴보실 수 있어요. 추가로, 다음의 빈 리스트 세 개가 미리 정의되어 있어요:
- 각 반복에서 모델의 학습 AUC 값을 담을
auc_values_train - 각 반복에서 모델의 테스트 AUC 값을 담을
auc_values_test - 각 반복에서 평가된 변수를 담을
variables_evaluate
이 연습은 강의의 일부입니다
Python으로 배우는 예측 분석 입문
연습 안내
- 변수들을 순회하세요.
- 각 반복에서
variables의 다음 변수를variables_evaluate에 추가하세요. - 각 반복에서
auc_train_test메서드를 사용해 학습과 테스트 AUC를 계산하세요. DataFrametrain과test에는 각각 학습과 테스트 데이터가 들어 있어요. - 각 반복에서 계산한 값을
auc_values_train과auc_values_test에 추가하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
# Keep track of train and test AUC values
auc_values_train = []
auc_values_test = []
variables_evaluate = []
# Iterate over the variables in variables
for v in ____:
# Add the variable
variables_evaluate.append(____)
# Calculate the train and test AUC of this set of variables
auc_train, auc_test = ____(____, ["target"], ____, ____)
# Append the values to the lists
auc_values_train.append(____)
auc_values_test.append(____)
# Make plot of the AUC values
import matplotlib.pyplot as plt
import numpy as np
x = np.array(range(0,len(auc_values_train)))
y_train = np.array(auc_values_train)
y_test = np.array(auc_values_test)
plt.xticks(x, variables, rotation = 90)
plt.plot(x,y_train)
plt.plot(x,y_test)
plt.ylim((0.6, 0.8))
plt.show()