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



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

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

有一种方法可以使用类的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]))

最新更新