2017-07-30 36 views
1

我有一个训练有素的决策树。当我输入一个特征向量来预测时,我想知道它从哪个决策路径中预测出来的,这个决定路径是从哪个树叶的哪个位置预测的。我如何从x_train被预测的决策树中获取叶的节点号?

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

+0

我想你需要使用DecisionTree对象的decision_path(X,check_input =真)方法 – sera

回答

1

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