Depth and leaf constraints
Quick answer
Tree growth constraints control how complex a decision tree can become. Limiting depth and requiring enough samples in leaves can reduce overfitting, but can also hide real patterns. Examples run locally with Python 3.13.5, scikit-learn 1.8.0, NumPy 2.3.5 and SciPy 1.17.0 when imported. ByteMind library execution is unverified.
Check feasible split sizes
n=7; minimum=3print([i for i in range(1,n) if i>=minimum and n-i>=minimum])Expected output:
[3, 4]Only 3/4 and 4/3 meet both minimum leaf sizes.
How it works
The root has depth zero; max_depth=1 permits one split. min_samples_split restricts whether a node may split, whereas min_samples_leaf constrains both resulting children. With 7 samples and min_samples_leaf=3, a 2/5 split is disallowed even if the parent satisfies min_samples_split=4. max_leaf_nodes bounds leaf count; min_impurity_decrease requires a sufficient weighted impurity improvement. Scikit-learn accepts integer counts or fractions for sample constraints (fractions are converted using ceiling and the training sample count). These are pre-pruning controls. A shallow tree may underfit when it cannot represent an interaction; a deep tree may memorize noise. Compare training and validation error for a fixed set of candidate depths, choose using validation only and report the final held-out score after freezing the settings. On grouped or temporal data use suitable splits instead of random row splits. For a binary tree of depth d, at most 2^d leaves are possible, but actual leaf count can be much smaller. Min leaf size can make predictions more stable but is not a calibration guarantee. Inspect leaf counts and decision paths; do not infer population sample support from a single observed leaf.
Compare constrained and unrestricted trees
from sklearn.tree import DecisionTreeClassifierX=[[0],[1],[2],[3],[4],[5]]; y=[0,1,0,1,0,1]for d in [1,2,None]: m=DecisionTreeClassifier(max_depth=d,random_state=7).fit(X,y) print(m.get_depth(),m.get_n_leaves(),round(m.score(X,y),3))Expected output:
1 2 0.6672 3 0.6675 6 1.0Alternating labels encourage many thresholds. Perfect training accuracy is not evidence of generalization.
Common mistakes
Do not confuse min_samples_split with min_samples_leaf. Never choose depth using the final test set. Do not assume max_depth is the actual depth.
Practice and next steps
List feasible left counts for 9 samples and minimum leaf size 4. Explain what changes if the parent requires 10 samples to split. Complete the knowledge check and coding exercise. Next: Cost complexity pruning.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Depth and leaf constraints — knowledge check
Apply depth and leaf constraints to new examples.
Root depth is zero. What does max_depth=1 allow?
Does min_samples_split=4 guarantee two children of size 2?
Which split meets min_samples_leaf=3?
Where should depth be selected?
Why might a shallow tree underfit?