我正在尝试使用&#39树提取最深节点的规则_' sklearn DecisionTreeClassifier中的方法。我很难理解' children_left'和' children_right'数组意味着从模型。任何人都可以帮忙解释一下吗?
estimator = DecisionTreeClassifier(max_depth=4, random_state=0)
estimator.fit(X_train, y_train)
estimator.tree_.children_left
[6] array([ 1, 2, 3, 4, 5, -1, -1, 8, -1, -1, 11, 12, -1, -1, 15, -1, -1,
18, 19, 20, -1, -1, 23, -1, -1, 26, 27, -1, -1, 30, -1, -1, 33, 34,
35, 36, -1, -1, 39, -1, -1, 42, 43, -1, -1, 46, -1, -1, 49, 50, 51,
-1, -1, 54, -1, -1, 57, 58, -1, -1, 61, -1, -1])
tree_model.tree_.children_right
[7] array([32, 17, 10, 7, 6, -1, -1, 9, -1, -1, 14, 13, -1, -1, 16, -1, -1,
25, 22, 21, -1, -1, 24, -1, -1, 29, 28, -1, -1, 31, -1, -1, 48, 41,
38, 37, -1, -1, 40, -1, -1, 45, 44, -1, -1, 47, -1, -1, 56, 53, 52,
-1, -1, 55, -1, -1, 60, 59, -1, -1, 62, -1, -1])
在Sklearn的例子http://scikit-learn.org/stable/auto_examples/tree/plot_unveil_tree_structure.html中,它说:
`# The decision estimator has an attribute called tree_ which stores the entire
# tree structure and allows access to low level attributes. The binary tree
# tree_ is represented as a number of parallel arrays. The i-th element of each
# array holds information about the node `i`. Node 0 is the tree's root. NOTE:
# Some of the arrays only apply to either leaves or split nodes, resp.`
但它并没有解释children_left数组中数字的含义
答案 0 :(得分:2)
from sklearn.datasets import load_iris
from sklearn import tree
iris = load_iris()
clf = tree.DecisionTreeClassifier()
clf = clf.fit(iris.data, iris.target)
children_left = clf.tree_.children_left
print (children_left)
它打印:
[ 1 -1 3 4 5 -1 -1 8 -1 10 -1 -1 13 14 -1 -1 -1]
您可以在Google中找到有关虹膜数据的17节点决策树。看到它并将其与解释进行比较。
现在是解释:
它继续。希望你能得到解释。
答案 1 :(得分:0)
children_left : array of int, shape [node_count]
children_left[i] holds the node id of the left child of node i.
For leaves, children_left[i] == TREE_LEAF. Otherwise,
children_left[i] > i. This child handles the case where
X[:, feature[i]] <= threshold[i].
children_right : array of int, shape [node_count]
children_right[i] holds the node id of the right child of node i.
For leaves, children_right[i] == TREE_LEAF. Otherwise,
children_right[i] > i. This child handles the case where
X[:, feature[i]] > threshold[i].
答案 2 :(得分:0)
只是为了向您展示一个可视化决策树的小技巧。你可以在你选择的绘图函数中指定一个参数 node_ids = True(在我的例子中是 export_graphviz),它会在你的树的图像上显示节点 ID!
export_graphviz(clf, out_file=dot_data, node_ids=True,
filled=True, rounded=True,
special_characters=True,feature_names = feature_cols,class_names=['0','1'])
!!! :)