2017-07-30 31 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]))