How to Find the Depth of Leaves for X in a DecisionTreeClassifier? #27549
Unanswered
albertoazzari
asked this question in
Q&A
Replies: 1 comment
-
When it comes to samples, I assume that you can use the from sklearn.datasets import load_iris
from sklearn.model_selection import cross_val_score
from sklearn.tree import DecisionTreeClassifier
clf = DecisionTreeClassifier(random_state=0)
iris = load_iris()
clf.fit(iris.data, iris.target) Not let's imagine you want to know the path (the nodes and final leave) by which a sample goes, you can use path = clf.decision_path(iris.data[[-1]])
path
This is a sparse matrix where the indicator The depth for this sample can be based on the sum of the indicator then: path.sum()
The method clf.apply(iris.data[[-1]])
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello scikit-learn community,
I'm currently working on a project involving the use of the DecisionTreeClassifier from scikit-learn, and I need to determine the depth of the leaves for X (ndarray(n_ojects, n_features)).
I've already trained my DecisionTreeClassifier and I'm looking for a way to find out how deep the leaves are for n objects in my decision tree.
Is there a straightforward way to achieve this in scikit-learn? Perhaps a built-in function or a recommended approach to calculate the depth of leaves for a given set of samples?
Or more generally is there a function that compute the depth of a node given the index ?
Any guidance, code snippets, or pointers to relevant documentation would be greatly appreciated.
Thank you in advance for your help!
Best regards,
Alberto
Beta Was this translation helpful? Give feedback.
All reactions