This page shows the paths in the decision trees in scikit-learn. After reaching the leaf nodes of the decision tree, where do we obtain the final resultant value?

  • $\begingroup$ Check in the value attribute i.e. regressor.tree_.value $\endgroup$
    – 10xAI
    Commented Dec 25, 2020 at 10:18
  • $\begingroup$ Alright. Is there any documentation for this? $\endgroup$ Commented Dec 25, 2020 at 10:37

2 Answers 2


For documentation, check the Scikit-Learn code at Github i.e. line#535 [Link]

value : array of double, shape [node_count, n_outputs, max_n_classes]
Contains the constant prediction value of each node.

You can check the same using this sample code.

  • Identify the leaf nodes
  • Slice the value attributes for leaf nodes
from sklearn.datasets import load_diabetes
import numpy as np, matplotlib.pyplot as plt
from sklearn.tree import DecisionTreeRegressor

X, y = load_diabetes(return_X_y=True)
regressor = DecisionTreeRegressor(max_depth=5)
regressor.fit(X, y)

children_left = regressor.tree_.children_left
children_right = regressor.tree_.children_right
leaf_nodes = []
for i in range(n_nodes):
    if children_left[i] == children_right[i]:

y_pred =  regressor.predict(X)
all_val = regressor.tree_.value[leaf_nodes,0,0] # Sliced on Leaf nodes

# Check if all the y_pred is from these values
set(y_pred) - set(all_val) # Or, [elem for elem in y_pred if elem not in all_val]


The decision depends on the loss function used. If you are using the Mean Square Error you use the within-leaf mean. The fastest way is to use the .apply() method and estimate the average of observations that fall in the same leaf.

  • $\begingroup$ Could you show any relevant documentation? $\endgroup$ Commented Dec 25, 2020 at 11:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.