# How to obtain the final values of a DecisionTreeRegressor in Scikit-Learn?

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?

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

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

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]:
leaf_nodes.append(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.

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