0
$\begingroup$

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?

$\endgroup$
2
  • $\begingroup$ Check in the value attribute i.e. regressor.tree_.value $\endgroup$ – 10xAI Dec 25 '20 at 10:18
  • $\begingroup$ Alright. Is there any documentation for this? $\endgroup$ – Chong Lip Phang Dec 25 '20 at 10:37
1
$\begingroup$

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]:
        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]

$\endgroup$
0
$\begingroup$

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.

$\endgroup$
1
  • $\begingroup$ Could you show any relevant documentation? $\endgroup$ – Chong Lip Phang Dec 25 '20 at 11:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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