PART 2 • CORE MACHINE LEARNING • LEVEL 09

Decision Trees & Random Forests

Turn data into explainable questions, measure every split mathematically and combine randomized trees into a stable ensemble. Learn the complete path from one decision node to production-ready forest reasoning.

⏱️ 210–250 min🎯 Beginner → Interview Ready🧪 2 Interactive Labs💼 Placement Algorithms
Mock score ≥ 70?
Projects ≥ 2?
Not Placed
Placed
Review
RANDOM FORESTMany diverse trees → one reliable vote

By the End of This Level, You Can

01Explain nodes, branches, leaves and recursive partitioning.
02Calculate Gini impurity, entropy and information gain.
03Find candidate thresholds and select a best split.
04Control depth, minimum samples and cost-complexity pruning.
05Explain bagging, feature randomness and out-of-bag validation.
06Interpret predictions and importance without overstating causality.

Six Ideas to Bring Forward

A tree repeatedly divides data; a forest reduces the instability of one tree.

FEATURExⱼ

A measurable input considered for a split.

THRESHOLDxⱼ ≤ t

A rule that sends samples left or right.

IMPURITYI(node)

How mixed the target values are.

GAINParent − Children

The impurity removed by a candidate split.

BAGGINGBootstrap + aggregate

Train diverse models and combine them.

VALIDATIONGeneralization first

Control complexity using unseen folds.

A Decision Tree Is a Hierarchy of Questions

Each internal node tests a feature; each leaf produces a prediction.

ROOT NODEFirst question

Contains the complete training subset and selects the opening split.

INTERNAL NODEFollow-up question

Further separates the subset reaching that branch.

BRANCHRule outcome

Represents true/false or a categorical route.

LEAFFinal prediction

Stores a class distribution or numeric target estimate.

DETAILED EXPLANATION

A decision tree maps a feature vector through a sequence of if–else conditions. The root sees every training sample. Each split creates child regions, and recursion continues until a stopping condition is met. A classification leaf normally predicts its majority class and can expose class proportions as probabilities. A regression leaf usually predicts the mean target value in that region.

WORKED INTUITION

A placement tree may first test mock score, then projects, before predicting placed or not placed.

AI / PLACEMENT CONNECTION

Interviewers frequently ask you to translate a small tree into readable if–else code.

COMMON MISCONCEPTION

A tree branch describes an association learned from data; it does not automatically prove a causal relationship.

Recursive Partitioning Builds Rectangular Regions

Each split divides one current region into smaller, more homogeneous regions.

1
Search current node

Evaluate allowed feature–threshold pairs.

2
Choose maximum gain

Keep the split producing the purest weighted children.

3
Repeat independently

Grow left and right subtrees on their own subsets.

DETAILED EXPLANATION

Standard CART-style trees use axis-aligned questions such as score ≤ 70. One question creates two regions. Later questions operate only inside a selected region, so the final feature space is divided into rectangles or hyperrectangles. The greedy algorithm chooses the best immediate split at every node rather than searching every possible complete tree.

WORKED INTUITION

After separating low scores, the high-score branch can split again using project count without affecting the low-score branch.

AI / PLACEMENT CONNECTION

Know why greedy growth is computationally practical but does not guarantee the globally smallest or best tree.

COMMON MISCONCEPTION

A later split does not redraw the complete feature space; it divides only the samples that reach its node.

Gini Impurity Measures Class Mixing

Gini is zero when every sample in a node belongs to one class.

Gini(S) = 1 − Σ pᵢ²
PURE NODE[10 Yes, 0 No]1 − 1² − 0² = 0
BALANCED NODE[5 Yes, 5 No]1 − .5² − .5² = .5
MOSTLY YES[8 Yes, 2 No]1 − .8² − .2² = .32
DETAILED EXPLANATION

For each class, square its proportion and add the results. Subtracting from one gives the probability that two labels sampled according to the node distribution would differ. In binary classification, maximum Gini is 0.5 at an even split; with more classes, its maximum approaches one.

WORKED INTUITION

For six placed and two not placed students, Gini = 1 − (6/8)² − (2/8)² = 0.375.

AI / PLACEMENT CONNECTION

Gini calculations are common written-test questions because they are quick and reveal whether you understand proportions.

COMMON MISCONCEPTION

Gini impurity is not the same as the Gini coefficient used in inequality analysis.

Entropy and Information Gain

Entropy measures uncertainty; gain measures how much a split removes.

ENTROPYH(S) = −Σ pᵢ log₂pᵢ

Pure binary node: 0 bit

Balanced binary node: 1 bit

INFORMATION GAINGain = H(parent) − Σ (|child|/|parent|)H(child)

Larger positive gain means greater reduction in uncertainty.

DETAILED EXPLANATION

Entropy is the expected information needed to identify a node’s class. A pure node has no uncertainty, while a balanced binary node has one bit. To evaluate a split, calculate each child’s entropy, weight it by the fraction of samples entering that child, and subtract the weighted result from parent entropy.

WORKED INTUITION

If a parent entropy of 1 is split into two perfectly pure equal children, weighted child entropy is 0 and information gain is 1.

AI / PLACEMENT CONNECTION

Be able to compare entropy and Gini: both favor purity and often choose similar splits, but their formulas and scales differ.

COMMON MISCONCEPTION

Do not average child impurities equally unless the child nodes contain equal numbers of samples.

How Candidate Thresholds Are Generated

Sorted feature values reveal the meaningful places where a decision can change.

1Sort values42, 55, 68, 72, 90
2Form midpoints48.5, 61.5, 70, 81
3Score partitionsgain(feature, threshold)
4Keep the bestargmax gain
DETAILED EXPLANATION

For a numeric feature, predictions change only when a threshold passes a training value. Efficient implementations sort values and evaluate boundaries between adjacent distinct values, often prioritizing boundaries where labels change. Every candidate is assessed using the weighted impurity of its resulting children.

WORKED INTUITION

Between observed scores 68 and 72, threshold 70 creates exactly the same partition as any value strictly between them.

AI / PLACEMENT CONNECTION

A from-scratch best-split problem tests sorting, loops, counting and careful weighted arithmetic.

COMMON MISCONCEPTION

A threshold is usually not restricted to an observed value; a midpoint cleanly represents the boundary.

Regression Trees Reduce Target Variance

Instead of class impurity, regression splits minimize squared prediction error.

LEAF PREDICTIONŷleaf = mean(y in leaf)

The mean minimizes squared error inside the region.

NODE ERRORSSE = Σ(yᵢ − ȳnode)²

Measures remaining numeric variation.

SPLIT REDUCTIONSSEparent − SSEleft − SSEright

Select the largest decrease in squared error.

DETAILED EXPLANATION

A regression tree uses the same recursive splitting structure but works with continuous targets. Each candidate split is judged by how much it reduces within-node squared error or variance. Every terminal region predicts a constant value, so a single regression tree creates a piecewise-constant function.

WORKED INTUITION

If salaries [3, 4, 9, 10] split into [3,4] and [9,10], two tight leaves have much lower error than one mean of 6.5.

AI / PLACEMENT CONNECTION

Explain why regression-tree predictions do not naturally extrapolate beyond observed target patterns.

COMMON MISCONCEPTION

Classification criteria such as Gini should not be applied directly to continuous targets.

Stopping Rules Control Tree Complexity

Unrestricted growth can create nearly pure training leaves and poor generalization.

max_depthLimit question depth

Directly caps the longest root-to-leaf path.

min_samples_splitRequire enough node data

Blocks splitting very small nodes.

min_samples_leafProtect child support

Ensures each leaf has a minimum sample count.

max_leaf_nodesLimit terminal regions

Controls overall partition count.

max_featuresRestrict candidate features

Especially important for forest diversity.

min_impurity_decreaseDemand useful gain

Rejects tiny improvements.

DETAILED EXPLANATION

Pre-pruning stops growth before every training detail is captured. Depth controls interaction complexity, while minimum-sample settings prevent fragile decisions based on tiny groups. These are hyperparameters and must be selected using validation data, not the final test set.

WORKED INTUITION

A leaf containing one student can memorize an exception; requiring ten samples makes its estimate more stable.

AI / PLACEMENT CONNECTION

In interviews, connect deep trees with low training bias and high variance.

COMMON MISCONCEPTION

Maximum depth is not “the number of nodes”; a binary depth-d tree can contain many nodes.

Post-Pruning Removes Weak Branches

Grow a larger tree, then trade training fit for structural simplicity.

COMPLEX TREELow training error

Many branches may explain noise.

COST-COMPLEXITY OBJECTIVERα(T) = R(T) + α|leaves(T)|

Penalty α rewards smaller trees.

VALIDATED SUBTREEBetter generalization

Choose α through cross-validation.

DETAILED EXPLANATION

Cost-complexity pruning assigns a penalty to every leaf. When α is zero, fit dominates; as α grows, additional leaves must justify themselves through enough error reduction. A pruning path provides candidate subtrees, and cross-validation selects the complexity with the strongest unseen performance.

WORKED INTUITION

A three-sample branch that improves training accuracy by one case may disappear when its complexity penalty exceeds that benefit.

AI / PLACEMENT CONNECTION

Know the difference between pre-pruning controls and post-pruning with ccp_alpha.

COMMON MISCONCEPTION

Pruning is not selected by looking for the highest training score; that favors the unpruned tree.

Categorical Values, Missing Data and Pipelines

The tree may not need scaling, but it still needs a reliable feature pipeline.

RAW DATANumeric + categorical + missing
TRAINING-FITTED PREPARATIONImpute and encode consistently
TREE MODELFit validated decision rules
DETAILED EXPLANATION

Axis-aligned trees are generally unaffected by monotonic scaling because order and possible partitions stay unchanged. However, categorical features must match the implementation’s supported representation, and missing values need explicit handling unless the estimator has native missing-value logic. Put every learned transformation inside a pipeline to prevent leakage and keep inference consistent.

WORKED INTUITION

Changing age from years to months changes thresholds numerically but preserves the ordering and equivalent partitions.

AI / PLACEMENT CONNECTION

“Trees do not require scaling” does not mean “trees require no preprocessing.”

COMMON MISCONCEPTION

Integer-encoding unordered categories can introduce an artificial order if the estimator treats the codes as numeric.

Feature Importance Needs Careful Interpretation

Importance ranks model usage—not scientific cause.

IMPURITY IMPORTANCETotal weighted gain

Fast and built into many tree implementations, but biased toward features with many split opportunities.

PERMUTATION IMPORTANCEShuffle and measure damage

Evaluates how performance changes when one feature’s information is disrupted.

LOCAL EXPLANATIONExplain one prediction

Trace its path or use validated explanation tools while respecting feature dependence.

DETAILED EXPLANATION

Mean decrease in impurity adds the weighted gains credited to a feature across a fitted tree or forest. Permutation importance instead shuffles a feature and measures validation-score deterioration. Correlated features can share or substitute importance, so rankings require domain knowledge, stability checks and cautious language.

WORKED INTUITION

If attendance and study hours carry similar information, shuffling either alone may cause only a moderate score drop.

AI / PLACEMENT CONNECTION

A strong answer distinguishes global model importance from local prediction explanation.

COMMON MISCONCEPTION

High importance does not prove that changing the feature will cause the prediction target to change in the real world.

Bagging Reduces Variance Through Resampling

Each tree sees a different bootstrap version of the training data.

ORIGINAL DATAn samples
BOOTSTRAP 1A B B D F F
BOOTSTRAP 2A A C E E F
BOOTSTRAP 3B C C D E F
AGGREGATEVote / average
DETAILED EXPLANATION

A bootstrap sample draws n records with replacement from n training records. Some rows appear repeatedly and others are omitted. A high-variance learner is fitted independently to each resample, and predictions are averaged or voted. Averaging reduces variance most effectively when individual models are accurate enough and their errors are not perfectly correlated.

WORKED INTUITION

One unstable tree may change after a few records move, while the majority of 200 diverse trees usually changes much less.

AI / PLACEMENT CONNECTION

Explain why bagging mainly attacks variance rather than systematically removing bias.

COMMON MISCONCEPTION

Bootstrap samples are not disjoint folds; they overlap and contain duplicate records.

Random Forests Add Feature Randomness

Restricting features at each split decorrelates the trees.

1Bootstrap rows

Create a different training sample per tree.

2Sample features at each node

Only a random subset competes for the split.

3Grow many deep trees

Each learner captures a different view.

4Aggregate predictions

Classification votes; regression averages.

DETAILED EXPLANATION

Pure bagging can produce very similar trees when one dominant feature wins most root splits. Random forests restrict the candidate feature set at every node, forcing alternative signals to participate. This typically reduces correlation between trees and improves the variance reduction gained from averaging.

WORKED INTUITION

If mock score is dominant, some trees may be forced to begin with projects or attendance and learn complementary rules.

AI / PLACEMENT CONNECTION

Know that max_features is sampled for every split—not once for the entire forest.

COMMON MISCONCEPTION

A random forest is more than many identical trees; diversity is central to its benefit.

Out-of-Bag Evaluation Uses Omitted Samples

Every bootstrap tree leaves some training records unused.

≈ 63.2%Unique samples expected inside one bootstrap sample
≈ 36.8%Records left out and available as OOB cases for that tree
Aggregate OOB votesEvaluate each record using only trees that did not train on it
DETAILED EXPLANATION

The probability that a record is not selected in n draws is (1−1/n)ⁿ, approaching e⁻¹ ≈ 0.368. For each training record, collect predictions from trees whose bootstrap samples excluded it. Aggregating these predictions produces an internal generalization estimate without creating a separate validation split.

WORKED INTUITION

A row may train trees 1, 3 and 5 but be evaluated by trees 2, 4 and 6 where it was out-of-bag.

AI / PLACEMENT CONNECTION

OOB score is useful, but cross-validation may still be preferred for careful model comparison and tuning.

COMMON MISCONCEPTION

OOB evaluation is not based on one fixed 36.8% holdout; the omitted set differs for every tree.

Single Tree or Forest?

Choose based on the need for interpretability, stability, latency and accuracy.

DECISION FACTOR
DECISION TREE
RANDOM FOREST
Interpretability
One path is easy to inspect
Many paths need aggregate explanation
Variance
Often high
Usually lower through averaging
Prediction cost
One root-to-leaf traversal
Traverse every selected tree
Non-linearity
Strong axis-aligned interactions
Combines many different partitions
Scaling
Usually unnecessary
Usually unnecessary
Best use
Transparent rules or a compact baseline
Strong general-purpose tabular baseline
DETAILED EXPLANATION

A shallow tree offers a direct rule system but can underfit. A deep tree captures interactions yet changes easily with the sample. A random forest sacrifices single-tree transparency and adds memory and latency, but often produces stronger and more stable tabular predictions. Selection must be based on validated performance plus deployment constraints.

WORKED INTUITION

Use a depth-three tree when policy requires a short auditable rule; use a forest when predictive stability is more important.

AI / PLACEMENT CONNECTION

Answer algorithm-comparison questions using data type, bias–variance, interpretability, scaling and inference cost.

COMMON MISCONCEPTION

A forest is not guaranteed to beat every tuned alternative on every dataset; validation remains necessary.

Decision Split Laboratory

Change the feature, threshold and criterion. Watch samples move into child nodes and verify every impurity calculation.

LIVE EXPERIMENT
PARENT IMPURITY0.500
WEIGHTED CHILDREN0.000
INFORMATION GAIN0.500
QUERY PATHRight → Placed

🎬 Decision Tree Training — Visual Flow

The builder repeats the same search separately inside every non-terminal node.

01Receive node samples

Begin with the records that reached this node.

02Generate candidates

Try permitted features and thresholds.

03Measure gain

Compare parent impurity with weighted children.

04Create children

Store the best rule and divide the records.

05Stop or recurse

Make a leaf or repeat inside each child.

Random Forest Voting Laboratory

Generate a seeded forest, inspect randomized rules and see how diversity changes the ensemble prediction.

LIVE ENSEMBLE
FOREST PREDICTIONPlaced71.4% vote confidence
PLACED VOTES5
NOT PLACED VOTES2
DIVERSITY3 rule familiesTrees disagree on 28.6% of votes

Trace Best-Threshold Search from Scratch

Follow candidate thresholds, left/right partitions, weighted Gini and every best-gain update.

Decision Tree and Random Forest Logic

Use this compact view for revision before coding or interviews.

DECISION TREE TRAINING
  1. Send training records to the root.
  2. Generate valid feature–threshold candidates.
  3. Calculate parent and weighted-child impurity.
  4. Store the candidate with maximum gain.
  5. Partition records into child nodes.
  6. Repeat until a stopping rule is reached.
  7. Store a prediction distribution in each leaf.
RANDOM FOREST TRAINING
  1. Create a bootstrap row sample for every tree.
  2. At every node, sample a feature subset.
  3. Choose the best split among only those features.
  4. Grow the tree using configured stopping rules.
  5. Repeat independently for many trees.
  6. Vote for classification or average for regression.
  7. Optionally aggregate out-of-bag predictions.

💻 CodeBhavya Tree & Forest Challenges

Solve each problem first. Viewing the model program reduces the recorded score.

0 / 5Solved
0 / 500Best score
Progress

Test Your Tree and Forest Understanding

Select answers, then check to see your choice, the correct answer and its explanation.

Not checked yet

Choose the Right Explanation in Interviews

Strong candidates connect the mechanism with its practical consequence.

WHY DO TREES OVERFIT?

Recursive partitions can isolate noise and tiny sample groups, producing high variance.

WHY DOES A FOREST HELP?

Bootstrap and feature randomness create diverse trees whose averaged errors are more stable.

WHY NO SCALING?

Monotonic scaling preserves ordering and therefore preserves equivalent axis-aligned partitions.

WHY OOB?

Every training row can be evaluated using only trees whose bootstrap samples omitted it.

WHY NOT CLAIM CAUSALITY?

Split selection and importance describe predictive association under the observed data.

WHAT SHOULD BE TUNED?

Depth, leaf support, feature sampling, tree count and class-related settings using validation.

🎤 Decision Trees & Random Forests — Interview Questions

Answer aloud before opening each explanation.

From One Greedy Rule to a Stable Ensemble

Decision trees make local greedy partitions that are easy to follow but naturally unstable.

Impurity criteria identify useful splits, complexity controls protect generalization, and random forests reduce variance through row and feature randomness. Always validate the complete pipeline and interpret importance as model evidence—not causal truth.

Six Practical Habits

01

Begin with a shallow tree and inspect whether its rules make domain sense.

02

Tune minimum leaf support, not only maximum depth.

03

Use stratified validation when classification classes are imbalanced.

04

Increase forest size until validation stability and latency reach a sensible balance.

05

Compare impurity importance with permutation importance on held-out data.

06

Fix random seeds for reproducibility, then test stability across several seeds.

✍️ Questions for Independent Revision

  1. 01

    Calculate Gini impurity for class counts [7, 3].

  2. 02

    Calculate entropy for a perfectly balanced binary node.

  3. 03

    Explain why child impurities must be weighted by sample counts.

  4. 04

    Generate candidate midpoints from values [2, 5, 9, 10].

  5. 05

    Trace a query through a three-level tree and state its leaf probability.

  6. 06

    Compare max_depth and min_samples_leaf as overfitting controls.

  7. 07

    Explain cost-complexity pruning using the role of α.

  8. 08

    Why does a regression tree produce piecewise-constant predictions?

  9. 09

    Derive why about 36.8% of records are OOB for one large bootstrap sample.

  10. 10

    Explain how random feature selection reduces tree correlation.

  11. 11

    Compare impurity-based and permutation importance.

  12. 12

    Design a leakage-safe forest pipeline for mixed tabular data.