Random Forest의 파라미터 추출하기
이제 이전에 로지스틱 회귀 모델로 진행했던 작업을 Random Forest 모델로 옮겨 보겠습니다. 이 모델의 파라미터 중 하나는 각 트리에서 매 단계마다 어떤 기준으로 분할했는지입니다.
Random Forest 모델에서는 모든 분할과 모든 트리를 일일이 탐색할 가능성이 낮기 때문에, 이 분석이 로지스틱 회귀의 계수만큼 직접적이진 않을 수 있어요. 하지만 모델이 내부에서 무엇을 하는지 들여다보는 데에는 매우 유용한 연습입니다.
이번 연습에서는 Random Forest 모델에서 하나의 트리를 추출하고, 이를 시각화한 뒤, 프로그램적으로 분할 중 하나를 추출해 보겠습니다.
다음이 제공됩니다:
- Random Forest 모델 객체
rf_clf - 선택된 결정 트리 상단 이미지
tree_viz_image X_trainDataFrame과original_variables리스트
이 연습은 강의의 일부입니다
Python에서의 하이퍼파라미터 튜닝
연습 안내
- Random Forest 모델에서 7번째 트리(인덱스 6)를 추출하세요.
- 분할 기준을 확인하기 위해 이 트리를 시각화합니다(
tree_viz_image). - 최상단 분할의 특성과 임곗값을 추출하세요.
- 특성과 임곗값을 함께 출력하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
# Extract the 7th (index 6) tree from the random forest
chosen_tree = rf_clf.estimators_[____]
# Visualize the graph using the provided image
imgplot = plt.imshow(____)
plt.show()
# Extract the parameters and level of the top (index 0) node
split_column = chosen_tree.tree_.feature[____]
split_column_name = X_train.columns[split_column]
split_value = chosen_tree.tree_.threshold[____]
# Print out the feature and level
print("This node split on feature {}, at a value of {}".format(split_column_name, ____))