Tree Construction and Splitting Criteria
Explore how decision trees classify data by constructing splits that reduce impurity and uncertainty in labels. Learn about Gini impurity and entropy as criteria for choosing splits, and understand the process of building a tree that predicts outcomes by refining data groups through local questions.
We'll cover the following...
A fraud label cannot always come from one global rule such as amount > 200. A $500 transaction at a trusted merchant may be legitimate, while a $90 transaction can be suspicious when the merchant risk score is high and the card has been used many times in one hour.
A decision tree handles these combinations with a sequence of yes-or-no questions. Each node is a group of transactions currently under consideration. A split divides one node using a rule such as amount >= 200. A leaf is an endpoint where the tree stops asking questions and makes its classification. Depth is the number of splits from the starting node to a particular node.
The tree begins with every transaction in one root node. It selects a split that makes the resulting groups less mixed in their fraud labels, then repeats that choice within each child group. The goal is not to find a single perfect threshold. The goal is to form leaves where nearly all transactions share the same label.
Measuring label mixes
Before scoring a split, we need a concrete way to inspect the labels in a node. The root node in the transaction table contains all 12 transactions. Its fraud proportion is the count of rows where fraud is 1, divided by 12. The candidate rule amount >= 200 creates two child nodes, one for higher-value transactions and one for lower-value transactions.
Lines 4–9:
label_mixforces a consistent two-row summary (non-fraud then fraud) viareindex([0, 1]), so a child node that happens to contain only one class still prints both classes with a0count and a0.0proportion.Lines 12–19: The example dataset is fixed at 12 rows and includes
amount,risk_score,velocity_1h, and a binary ...