我如何从预测决策树 x_train 的位置获取叶子的节点号?

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

我有一个训练有素的决策树。当我输入一个特征向量来预测时,我想知道它是从哪个决策路径预测的,或者新特征落在树的哪个叶子下。

我正在使用 python 的 Sklearn 决策树实现。

有一种方法可以使用 class 的 decision_path 方法访问树中的决策路径。

示例

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)

结果:

(<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]))