Back to questions
JuniorTheoryCommon
What does this program print?
import numpy as np
from sklearn.tree import DecisionTreeRegressor
X = np.array([[1], [2], [3], [4], [5]])
y = np.array([10, 20, 30, 40, 50])
model = DecisionTreeRegressor(random_state=0).fit(X, y)
print(model.predict(np.array([[100]]))[0])50.0
- Expecting
1000.0, as if the tree continued the liney = 10x:x = 100passes every threshold likex = 5and lands in the rightmost leaf, whose constant is50.0. - Expecting
30.0, the mean of all targets: the tree returns the constant of the leaf the sample reaches, not the overall mean, and that leaf holds onlyx = 5.
- What would
LinearRegressionpredict for the same input, and why does it differ from the tree? - How can a tree-based model still be used to forecast a value that keeps growing over time?