🎉 75% of content is free forever — Unlock Premium from $10/mo →
CW
Search courses…
💼 Servicesℹ️ About✉️ ContactView Pricing Plansfrom $10

Decision Trees — Complete Guide with Visualizations

ML FoundationsClassification🟢 Free Lesson

Advertisement

Supervised Learning

If-Then Rules That Learn — The Most Interpretable Algorithm

Decision trees split data using simple if-then-else rules. They are easy to visualize, handle mixed data types, and form the basis for powerful ensemble methods.

  • Gini Impurity — Measuring node purity for optimal splits
  • Information Gain — Entropy-based splitting criterion
  • Pruning — Preventing overfitting by limiting tree complexity

"A decision tree is the only ML algorithm that can be explained to your grandmother."

Decision Trees — Complete Guide

Decision trees make predictions by learning simple rules from data — like a flowchart of if-then-else decisions.


How Decision Trees Work

Decision Tree Structure: Should I Play Tennis?Outlook?SunnyOvercastRainHumidity?Yes ✓Wind?HighNormalNo ✗Yes ✓WeakStrongYes ✓No ✗Feature Importance:Outlook > Humidity > Wind > TempRoot node split = highest information gain

Splitting Criteria

Gini Impurity

Information Gain (Entropy)

Gini Impurity vs Entropy ComparisonGini Impurityp (proportion of class 1)010.5Gini = 1 − p² − (1−p)²Max at p=0.5Entropyp (proportion of class 1)011.0H = −p log₂(p) − (1−p) log₂(1−p)Max at p=0.5Both curves are concave, maximized at p=0.5. CART uses Gini; ID3/C4.5 use Entropy.

CART Algorithm

from sklearn.tree import DecisionTreeClassifier, export_text
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split

iris = load_iris()
X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.2)

tree = DecisionTreeClassifier(max_depth=3, criterion='gini', random_state=42)
tree.fit(X_train, y_train)
print(f"Accuracy: {tree.score(X_test, y_test):.3f}")
print(export_text(tree, feature_names=iris.feature_names))

for name, imp in zip(iris.feature_names, tree.feature_importances_):
    print(f"{name}: {imp:.3f}")

Pruning

Pruning: Before and AfterBefore Pruning (Overfitting)RootNode ANode BLeafLeafLeafLeafTrain: 100% | Test: 70%7 leaf nodes — memorizes noiseAfter Pruning (Good Fit)RootLeaf ✓Leaf ✓Train: 95% | Test: 93%3 leaf nodes — better generalization

Feature Importance


Key Takeaways


What to Learn Next

-> Random Forest Ensemble of decision trees for better accuracy and stability.

-> XGBoost Gradient boosting taken to the extreme — state-of-the-art performance.

-> Ensemble Methods Bagging, boosting, and stacking for stronger models.

Need Expert Machine Learning Help?

Get personalized tutoring, project support, or professional consulting.

Advertisement