Many models working together.
Understand bagging, random forests, boosting, and why combining weak rules can help.
Start with the meaning.
An ensemble combines predictions from several models. A single decision tree can change substantially when training data changes. A random forest fits many trees on varied samples and feature choices, then averages their numeric predictions or combines class votes. This reduces some of the instability of one tree. The trees are not independent experts with human understanding; the variation in their training views helps cancel some errors.
Bagging, short for bootstrap aggregating, repeatedly samples training rows with replacement. A row can appear more than once in one sample and not at all in another. Each tree learns from a different sample. Random forests add randomness in candidate features at splits, making trees less alike and often more useful together. A forest can model nonlinear relationships and interactions without explicit feature formulas.
How the idea works.
Boosting builds a sequence rather than a set of mostly independent trees. Each new small model focuses on improving the current ensemble's errors according to an optimisation objective. Gradient boosting fits additions that reduce loss. A learning rate controls each new contribution, while number of stages and tree complexity control capacity. Too many or too strong stages can overfit; too few can underfit. The fitting process and tuning need validation.
Forests and boosted trees often perform well on structured tabular data, especially when relationships are nonlinear. They do not eliminate data problems. A leaked feature remains leaked, a missing population remains missing, and a biased target remains biased. They can also be harder to explain than one shallow tree. Feature importance and local explanations help inspect behavior but should not be mistaken for causal evidence.
Trees typically do not require standard scaling of numeric features, but mixed categorical and missing-data support varies by estimator. Some implementations accept missing values or categorical features directly; others require preprocessing. Code should follow the exact estimator's contract instead of assuming all tree-based methods have identical input behavior.
Follow the code.
from sklearn.ensemble import RandomForestRegressor
X = [[1], [2], [3], [4], [5], [6]]
y = [2, 4, 6, 8, 10, 12]
forest = RandomForestRegressor(
n_estimators=50, max_depth=3, random_state=0
)
forest.fit(X, y)
print(forest.predict([[3.5]]))The six rows express a simple increasing relationship. n_estimators=50 asks for fifty trees. Each tree sees a varied view of training data; max_depth=3 limits its complexity. The forest averages their numeric predictions for input 3.5. The output will be one number in an array, probably near the nearby training targets but not guaranteed to equal 7. A forest's piecewise predictions behave differently from the straight line learned by linear regression.
random_state makes the stochastic choices reproducible for this illustration. It does not prove the prediction is correct. Six rows are far too few to measure forest performance. The example exists to show the fit/predict interface and the idea that many fitted trees contribute to one result.
What the result does not say.
An ensemble can be accurate yet operationally expensive. Model size, prediction time, memory, and monitoring requirements matter alongside a validation metric. A simpler model may be preferable when performance is similar.
Some boosted tree systems are separate libraries with different handling of categories, missing values, and hyperparameters. The shared conceptual idea is sequential correction of an ensemble, not a promise that all implementations have identical behavior.
One clear idea.
Forests average varied trees; boosting adds corrective stages. Both still depend on honest data and evaluation.
The ML cookbook
Complete, explained Python workflows sit alongside this reading course.