Ravindra BagaleCourses & study guides मराठी Track your progress

35. Decision Trees and Random Forests in Python

Data Science

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

  1. Start at the top (root).
  2. Follow the branch that matches the row.
  3. 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.