Skip to main content
MahiDev — Learn. Build. Become AI-ready.

Lesson 24

Random Forest Regressor

Average many decision trees trained on different row and feature samples so one noisy tree cannot dominate. One example is on this page.

Intermediate12 min
Random Forest Regressor MahiDev

What You Will Learn

  • Idea
  • Example
  • When to use it

Idea

A random forest grows many trees. Each tree sees a bootstrap sample of the rows and a random subset of the inputs at each split. The prediction is the average of the trees. Averaging cancels mistakes that only one tree makes.

Example

python
import numpy as np
from sklearn.ensemble import RandomForestRegressor

x = np.array([1, 2, 3, 8, 9], dtype=float).reshape(-1, 1)
y = np.array([2.0, 2.2, 1.8, 9.0, 9.4])
pred = RandomForestRegressor(n_estimators=20, max_depth=2, random_state=0).fit(x, y).predict([[8]])[0]
print(f"predict: {pred:.2f}")

Output:

text
predict: 8.70

When to use it

  • Use it as a strong default when the target is numeric and interactions matter.
  • More trees stabilize the average. Depth still needs a limit.
  • The forest is harder to explain than one tree.

The theory lesson Ensemble Methods covers bagging.

Loading playground…