Decision trees and random forests
Reading a tree as rules, pruning, why a forest votes, and how to read importance and the baseline.
You want a prediction model, but you also have to explain its results to other people. Then this question comes up.
"Can I show what the prediction was based on?"
A model that is just a mass of numbers can be accurate and still impossible to explain. The usual answer is decision trees and random forests. Here is what the two are and how to read their results.
What a decision tree is
A decision tree splits cases by asking yes/no questions one after another. It is a lot like twenty questions.
Each question, such as "Is the alcohol content 10.2% or less?" or "Is sulphates 0.66 or less?", becomes a branch, and an end that is not split further is a leaf.
At every step the tree picks the variable and cut-off that split the cases most cleanly into two groups. How mixed a group is gets measured with Gini impurity or entropy.
An example
- Data — 1,000 bottles of wine
- Target — quality grade (high / low)
- Available information — alcohol, volatile acidity, residual sugar, sulphates, citric acid, pH, density
400 bottles (40%) are high grade and 600 (60%) are low grade.
Read the tree as rules
Each leaf is one rule: "if … and …, then …".
If alcohol > 11.5 and volatile acidity ≤ 0.45 → high grade
This leaf is 88% high grade, 164 bottles. The 88% is the share of high-grade wines in that leaf, and 164 is how many bottles the rule rests on.
So when you read a rule, do not look at the percentage alone — always check how many cases it came from.
Depth and pruning
Grow a tree all the way and it gets almost every training wine right, yet goes wrong easily on wines it has never seen. It has memorised the data. This is overfitting.
So you prune the tree to keep it from growing too far.
- Maximum depth — how many levels of questions to ask
- Minimum samples to split — only split a node with at least this many cases
- Minimum samples per leaf — at least this many cases must stay in each leaf
Start at a depth of about 3–5 and adjust while comparing performance. A shallow tree often fits better.
Split into training and test sets
Scoring a model on the data it was built from makes it look better than it is. So you usually train on 80% of the data and test on the 20% held out. In the example, 800 bottles grow the tree and 200 grade it.
Add cross-validation, which splits the training data into several folds and checks each in turn, and you can also see whether a good score was just luck. A large gap between the training and test scores is a sign of overfitting.
Why grow many trees?
A single tree is so sensitive that a small change in the data can change even its first question. A random forest reduces this weakness by growing hundreds of trees and predicting by majority vote.
- Each tree learns from cases drawn again at random (bootstrap)
- At each split only some of the variables are candidates
- So the trees all come out a little different
Their mistakes cancel out, making predictions steadier and more accurate. The score each tree earns on the cases it did not train on is the OOB (out-of-bag) score — a second check alongside the test set.
The catch is that hundreds of trees cannot be explained by one rule. That is why you also look at variable importance.
Variable importance
- Impurity (Gini) importance — how much cleaner the splits became with that variable
- Permutation importance — how far test performance drops when that variable's values are shuffled
Impurity importance can favour variables with many distinct values, so read it together with permutation importance. And importance means "used a lot for prediction", not that the variable is a cause.
Confusion matrix and accuracy
A confusion matrix cross-counts actual and predicted grades. The diagonal is the correct predictions.
| Metric | Meaning |
|---|---|
| Accuracy | Share of all cases predicted correctly |
| Precision | Of those called "high", the share that really are high |
| Recall | Of the truly high, the share found as "high" |
And always compare accuracy with "always guessing the most common class". In the example 60% are low grade, so calling every wine "low" already gives 60% accuracy.
Which one to use
- You must explain the rules to people → decision tree
- You need more accurate, stable predictions → random forest
- Not sure → run both and compare on the same test set
In practice the two are often used together: one tree to understand the pattern, a forest to check performance.
Reporting it
To predict wine quality (high/low) from seven components of 1,000 bottles, a decision tree (maximum depth 4) and a random forest (300 trees) were trained. On a test set not used in training (200 bottles), the decision tree reached 82.0% accuracy and the random forest 89.0% (macro F1 = .885), both above the majority-class baseline (60.0%). By permutation importance, alcohol content and volatile acidity contributed most to the predictions.
Example sentence · the numbers are made up
In Skari
In Skari Statistics, choose Predictive → Tree & Ensemble Models to find Decision Tree and Random Forest. Each runs in the order Variables → Settings → Validation → Result.
In the Variables step, choose the dependent variable (Y) and the features (X), and set the task type to auto-detect, classification or regression.
In Settings you can set the maximum depth (max_depth), the minimum samples to split and per leaf, the test share (test_size) and the split criterion (Gini or entropy), each with a short note on what the value means.

Grow a decision tree on your data
Opens Statistics at Predictive → Tree & Ensemble Models → Decision Tree
Before the run, the Validation step checks the sample size, the training and test sizes, the size of the smallest class and whether there are enough cases for the tree depth.
The results come together as a report. At the top, next to the accuracy, you see how many percentage points it beats the majority-class baseline.

Along with model performance, per-class metrics, the confusion matrix, tree structure and variable importance, you get a table of decision rules, one per leaf. Each rule shows its prediction, confidence (the share inside the leaf) and n (number of cases) side by side.


For a random forest you can set the number of trees (n_estimators), the variables tried at each split (max_features), bootstrap sampling and the OOB score.

The top of the report shows the accuracy, its gain over the baseline, the number of trees and the OOB score, and the body lists Gini importance and permutation importance separately.


Results can be exported to Excel, PDF and Word.
Wrapping up
A decision tree gives you rules you can read; a random forest gives you steadier predictions. Check that you validated on held-out data, that the model really beats the baseline and how many cases each rule rests on, and your results become far more trustworthy.
Want the theory behind it?The Academy's decision tree page covers split criteria and pruning in depth →