π Decision Tree
Descriptionβ
- Decision tree is a popular machine learning algorithm used for classification and regression tasks. It works by recursively splitting the data into subsets based on the most significant feature at each node, creating a tree-like structure. The decision tree can be visualized to understand the decision-making process and the importance of different features in the dataset.
Key pointsβ
< Gini impurity >β
-
Node impurity: Gini impurity measures how mixed a classification node's classes are:
Here, is the fraction of examples in class within that node. Lower values mean a purer node; means every example belongs to one class.
-
Split impurity: assess a candidate split using the sample-weighted average of its children's impurities:
Symbol Meaning Gini impurity of the left child, calculated from its own class proportions Gini impurity of the right child, calculated from its own class proportions , Numbers of samples in the left and right children Number of samples in the parent node -
Gini gain: among eligible candidate splits at the same parent, the tree chooses the lowest weighted child impurity, equivalently the largest decrease from the parent's impurity:
βGini scoreβ can mean either node impurity or weighted split impurity, so state which one is being used.
-
Example β recommend a lipstick to a user: train a decision tree to predict whether a user will click a recommendation for one particular lipstick. Inputs are gender, age, annual income, and nationality; the training label is clicked (
1) or did not click (0) after the lipstick was shown. The predicted click probability can be used as a recommendation score.1. Start with the same training data for every candidate split. This synthetic dataset is deliberately constructed so gender has the largest Gini gain. It illustrates the calculation, not a general claim about demographic groups or lipstick preferences.
User Gender Age Annual income (USD) Nationality Clicked U1 Woman 24 80,000 Japan 1 U2 Woman 24 80,000 France 1 U3 Woman 24 40,000 Japan 1 U4 Woman 40 80,000 France 1 U5 Woman 40 40,000 Japan 1 U6 Man 40 80,000 France 1 U7 Man 40 40,000 France 1 U8 Man 24 80,000 Japan 0 U9 Man 24 40,000 Japan 0 U10 Man 24 40,000 France 0 The parent has 7 clicked and 3 did-not-click samples:
2. Calculate the gender split. The question is βIs gender Woman?β Its left child contains the five women; its right child contains the five men in this dataset.
Child Label counts Sample count Gini impurity Left: Woman 5 clicked, 0 did not click Right: Man 2 clicked, 3 did not click Weight the child impurities by their sample counts, then subtract from the parent impurity:
3. Compare the four candidate splits. Counts below are written as clicked / did not click. Each right child is the complement of the left-child condition.
Feature / left-child condition Left counts Right counts Weighted child Gini Gini gain Gender: Woman 5 / 0 2 / 3 Age: at most 30 3 / 3 4 / 0 Income: above USD 60,000 4 / 1 3 / 2 Nationality: Japan 3 / 2 4 / 1 4. Choose gender at the root. It has the lowest weighted impurity () and largest gain (). The pure left child becomes a leaf; the right child can be split again using the remaining features if the stopping criteria allow it. βHelps the mostβ here means the largest impurity reduction at this node, not a causal effect or a universal feature ranking.
5. Produce a recommendation score. If the tree stops after this one split, a new user with features
(Woman, 24, 40000, Japan)reaches the left leaf and receives an empirical click probability of . A user with(Man, 40, 80000, France)reaches the right leaf and receives . These tiny-sample estimates demonstrate leaf scoring; they are not reliable real-world click probabilities. A deeper tree may produce different scores.
Comparisonβ
< Decision Tree vs. Naive Bayes >β
- When to use each: see Naive Bayes vs. Decision Tree for task choices, feature interactions, interpretability, and validation guidance.
Implementationβ
Favoritesβ
β³οΈ
Video Tutorialβ
- Decision & Classification Trees
- Decision Tree: Important things to know
- Decision Tree Explained Visually | Gini Impurity, Random Forests & Feature Importance
- The Gini Impurity Index
Referenceβ
- Decision Tree in Machine Learning (geeksforgeeks.org)