시작하기무료로 시작하기

의사결정나무 규칙 살펴보기

이 연습 문제에서는 의사결정나무에서 if-else 규칙을 추출하고 이를 시각화하여 이탈(churn)의 주요 요인을 파악해 보겠습니다.

학습된 의사결정나무 인스턴스는 mytree로, 스케일링된 특성은 train_X라는 pandas DataFrame으로 제공됩니다. sklearn 라이브러리의 tree 모듈과 graphviz 라이브러리는 이미 로드되어 있습니다.

display(graph)대신 전용displayimage()` 함수를 사용하여 결과를 더 쉽게 볼 수 있도록 했다는 점에 유의하세요._

이 연습은 강의의 일부입니다

Python으로 배우는 마케팅용 Machine Learning

강의 보기

연습 안내

  • 학습된 의사결정나무에서 graphviz 객체를 내보내세요.
  • feature 이름을 지정하세요.
  • 정밀도(precision)를 1로 설정하고 클래스 이름을 추가하세요.
  • graphvizSource() 함수를 호출하고, 내보낸 graphviz 객체를 전달하세요.

실습형 인터랙티브 연습

이 예제를 이 샘플 코드를 완성하여 풀어보세요.

# Export graphviz object from the trained decision tree 
exported = tree.___(decision_tree=mytree, 
			# Assign feature names
            out_file=None, ___=train_X.columns, 
			# Set precision to 1 and add class names
			precision=1, ___=['Not churn','Churn'], filled = True)

# Call the Source function and pass the exported graphviz object
graph = graphviz.___(exported)

# Display the decision tree
display_image("/usr/local/share/datasets/decision_tree_rules.png")
코드 편집 및 실행