Coding Now – Best AI & Full Stack Courses in Delhi NCR | 100% Placement
Limited Offer: Get 50% OFF on AI & Full Stack Courses
📞 Call Now: +91 9667708830
Back to Machine Learning Notes
Topic #1002

Decision Tree Classification

A complete walkthrough of how a classification tree picks its very first split — computing impurity before and after a candidate split, and choosing whichever split reduces impurity the most.

The Dataset

10 historical examples of whether a customer played tennis, based on the day's outlook:

Outlook = Sunny?Played TennisCount
Yes (Sunny)No3
Yes (Sunny)Yes1
No (Not Sunny)Yes5
No (Not Sunny)No1

Root node: 10 examples total, 6 "Yes", 4 "No".

Step 1 — Impurity Before the Split

\[ \text{Gini(root)} = 1-(0.6^2+0.4^2) = 0.48, \qquad \text{Entropy(root)} = -[0.6\log_2 0.6+0.4\log_2 0.4] \approx 0.971 \]

Step 2 — Impurity After Splitting on "Sunny?"

GroupCountsGiniEntropy
Sunny (4 examples)1 Yes, 3 No\(1-(0.25^2+0.75^2)=0.375\)\(\approx 0.811\)
Not Sunny (6 examples)5 Yes, 1 No\(1-(0.833^2+0.167^2)\approx 0.278\)\(\approx 0.650\)

Step 3 — Weighted Impurity and Information Gain

\[ \text{Weighted Gini} = \tfrac{4}{10}(0.375)+\tfrac{6}{10}(0.278) \approx 0.317, \qquad \text{Gini reduction} = 0.48-0.317 = 0.163 \] \[ \text{Weighted Entropy} = \tfrac{4}{10}(0.811)+\tfrac{6}{10}(0.650) \approx 0.714, \qquad IG = 0.971-0.714 = 0.257 \]

See Gini Impurity, Entropy and Information Gain for the formulas in full depth. In practice, the tree would repeat this exact calculation for every candidate feature and threshold, then pick whichever split produces the highest information gain (or Gini reduction).

Python Implementation

from sklearn.tree import DecisionTreeClassifier, plot_tree
import matplotlib.pyplot as plt

# outlook_sunny (1/0), humidity_high (1/0) -> played (1/0)
X_train = [[1,1],[1,1],[1,0],[1,0],[0,1],[0,0],[0,0],[0,0],[0,0],[0,1]]
y_train = [0,0,0,1,1,1,1,1,0,1]

model = DecisionTreeClassifier(criterion="entropy", max_depth=3, random_state=42)
model.fit(X_train, y_train)

plt.figure(figsize=(10,6))
plot_tree(model, feature_names=["outlook_sunny","humidity_high"], class_names=["No","Yes"], filled=True)
plt.show()

print(model.predict([[1, 0]]))   # sunny, low humidity -> prediction

criterion="entropy" tells scikit-learn to use information gain (based on entropy) for split selection; criterion="gini" (the default) uses Gini impurity instead. In practice, the two criteria usually produce very similar trees.

How the Tree Grows Beyond the Root

After the first split, the algorithm repeats the exact same process independently on each resulting subset — the Sunny group and the Not-Sunny group each get their own best-split search, considering all remaining features again. This recursive splitting continues until a stopping condition (max depth, minimum samples per leaf, or zero remaining impurity) is reached — see Decision Tree Pruning for how these stopping rules are chosen deliberately.

Practical Use Cases

  • Any binary or multi-class classification problem where an interpretable decision path adds real value

Common Mistakes

  • Assuming Gini and entropy always pick the same split — they usually agree, but not always exactly, since they weight impurity slightly differently.
  • Growing the tree without limiting depth, letting it split all the way down to single-example leaves — a direct path to overfitting.

Interview Relevance

Q: "How does a decision tree decide which feature to split on first?" It evaluates every candidate feature and threshold, computes the impurity reduction (information gain) each would produce, and selects whichever split reduces impurity the most — exactly the calculation worked through above.

Practice Question

Using the impurity formulas, verify that Gini(Not Sunny) with 5 Yes / 1 No comes out to approximately 0.278.

Related ML Notes

Want to go beyond the notes?

Join CodingNow's Machine Learning course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available
💬 Talk to Advisor
1
WhatsApp

Latest from Our Blog

Insights on AI, Data Science, Full Stack & Career

View All Articles →