35. Decision Trees and Random Forests in Python
35.1 Readable Rules from a Decision Tree
Why. Managers like rules they can read: “if complaints ≥ 2 and orders_last_30d < 3 → high churn risk.” Trees speak that language.
What. A decision tree asks yes/no questions on features and ends in a leaf prediction (class or number). You can print the rules — friendlier than a pile of coefs for teaching.
How.
Think in if/else
- Start at the top (root).
- Follow the branch that matches the row.
- Stop at a leaf — that is the prediction.
# Story tree (not fitted yet)
# if complaints >= 2:
# predict churn = 1
# else:
# if orders_last_30d < 3: predict 1
# else: predict 0
print("Trees = stacked if/else on features.")
Ravindra Bagale's Tip
A very deep tree can memorise the training data instead of learning general rules. This problem is called overfitting, and you will study it in Chapter 38. So learn how to control the depth of a tree, for example with the max_depth setting.
Ravindra Bagale's Tip – मराठी
खूप खोल tree सामान्य rules शिकण्याऐवजी training data पाठ करून टाकू शकते. या problem ला overfitting म्हणतात, आणि तो तुम्ही Chapter 38 मध्ये शिकाल. म्हणून tree ची depth control कशी करायची ते शिका, उदाहरणार्थ max_depth setting वापरून.
Ravindra Bagale's Tip – हिंदी
बहुत गहरा tree सामान्य rules सीखने के बजाय training data रट लेता है. इस problem को overfitting कहते हैं, और इसे आप Chapter 38 में पढ़ेंगे. इसलिए tree की depth को control करना सीखो, जैसे max_depth setting से.