3
我的目標是確定在決策樹中兩個樣本分開的深度。在開發版本scikit學習,你可以使用decision_path()
方法來識別最後的共同節點:scikit-learn決策樹節點深度
from sklearn import tree
import numpy as np
clf = tree.DecisionTreeClassifier()
clf.fit(data, outcomes)
n_nodes = clf.tree_.node_count
node_indicator = clf.decision_path(data).toarray()
sample_ids = [0,1]
common_nodes = (node_indicator[sample_ids].sum(axis=0) == len(sample_ids))
common_node_id = np.arange(n_nodes)[common_nodes]
max_node = np.max(common_node_id)
有沒有一種方法來確定在樹內的什麼深度發生max_node
,可能與clf.tree_.children_right
和clf.tree_.chrildren_left
?