ถอดกฎของ Decision Tree
ในแบบฝึกหัดนี้ คุณจะดึงกฎ if-else ออกจาก decision tree และพล็อตเพื่อระบุปัจจัยหลักที่ขับเคลื่อนการเลิกใช้บริการ
โมเดล decision tree ที่ฝึกแล้วถูกโหลดไว้เป็น mytree และฟีเจอร์ที่ปรับสเกลแล้วถูกโหลดไว้เป็น DataFrame ของ pandas ชื่อว่า train_X โดยได้โหลดโมดูล tree จากไลบรารี sklearn และไลบรารี graphviz ไว้ให้แล้ว
หมายเหตุ: เราใช้ฟังก์ชัน display_image() แบบกำหนดเองแทน display(graph) เพื่อให้ดูผลลัพธ์ได้ง่ายขึ้น
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
Machine Learning สำหรับการตลาดด้วย Python
คำแนะนำการฝึกหัด
- ส่งออกออบเจ็กต์
graphvizจาก decision tree ที่ฝึกแล้ว - กำหนดชื่อฟีเจอร์
- ตั้งค่าความละเอียดเป็น 1 และเพิ่มชื่อคลาส
- เรียกใช้ฟังก์ชัน
Source()จากgraphvizและส่งออบเจ็กต์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")