Reputation: 4819
I would like to obtain all information of a node making a prediction using sklearn.tree.
For example:
from sklearn.datasets import load_iris
nfrom sklearn.tree import DecisionTreeClassifier
clf = DecisionTreeClassifier()
iris = load_iris()
clf = clf.fit(iris.data, iris.target)
Now we can predict the class using:
clf.predict(iris.data[0, :])
How can I obtain the leaf node making the prediction and also the information stored in the leaf?
I know that the graphical representation of the tree for the above example is the following:
http://scikit-learn.org/stable/modules/tree.html#tree-classification
So I know that the node corresponding to the input iris.data[0, :] (first left child) has the following statistics:
Is it possible to obtain the output node and the (above) information automatically without printing the tree? From my current understaning the key thing is to obtain the ID of the leaf node making the prediction, the relevant statistics are then contained in clf.tree_.value[ID] and clf.tree_.n_samples[ID].
Thank you
Upvotes: 0
Views: 956
Reputation: 6069
Take a look at this question. It says how to obtain leaf's ID. Then you can use those clf.tree_.value
and clf.tree.n_samples
.
Upvotes: 2