Algorithm
Decision Tree Regression
Current Depth
1

No More Formulas

Linear Regression uses math formulas (lines/curves). Decision Trees use Rules. They ask a series of Yes/No questions to split the data into smaller groups.
Continue

Depth 0: The Average

At the start (Root), we have no rules. The best prediction for everyone is just the Global Average (a flat line).
Continue

Depth 1: The First Split

The tree asks: 'Is X < 5.0?' • YES: Average of left points. • NO: Average of right points. Now we have 2 flat steps.
Continue

Depth 2: Recursion

We repeat the process for the Left Group and the Right Group separately. Now we have 4 steps. The prediction starts to look like the Sine curve!
Continue

Depth 6+: Overfitting

If we treat every tiny wiggle as a 'rule', the tree memorizes the Noise. This looks like a frantic, jagged line. It fails on new data.
Continue

Grow the Tree

Drag the slider to increase Depth. Watch how a simple set of Yes/No questions approximates a complex curve.
Continue

Coding Decision Trees

Using `DecisionTreeRegressor`. It handles the recursive splitting for you.

tree_regressor.py
from sklearn.tree import DecisionTreeRegressor

# 1. Create Model
# random_state=0 ensures same results every time
model = DecisionTreeRegressor(random_state=0)

# 2. Train (Fit)
# It finds the best splits automatically
model.fit(X, y)

# 3. Predict new value
pred = model.predict([[6.5]])

print(pred)
# Output: [0.25] (Average of leaf node)
Using Scikit-Learn
AlgoAnimator: Interactive Data Structures