Farshid Rayhan
Farshid Rayhan

Reputation: 1214

how do i get the node number of the leaf from where x_train is being predicted of a decision tree?

I have a trained decision tree . When i input a feature vector to predict i want to know from which decision path it has been predicted from or under which leaf of the tree the new feature falls under .

I am using python's Sklearn's implementation of decision tree .

Upvotes: 1

Views: 496

Answers (1)

seralouk
seralouk

Reputation: 33197

There is a way to access the decision path in the tree using the decision_path method of the class.

Example:

from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import load_iris 
import numpy as np

data = load_iris()

x = data.data
y = data.target

clf = RandomForestClassifier()

clf.fit(x,y)

clf.decision_path(x)

Results:

(<150x140 sparse matrix of type '<type 'numpy.int64'>'
with 5406 stored elements in Compressed Sparse Row format>, array([  0,  13,  
26,  41,  54,  71,  86,  97, 106, 119, 140]))

Upvotes: 1

Related Questions