其他分享
首页 > 其他分享> > 有没有办法检索由sklearn.tree.DecisionTreeClassifier生成的节点的最终数量?

有没有办法检索由sklearn.tree.DecisionTreeClassifier生成的节点的最终数量?

作者:互联网

如果没有对max_depth,min_samples等进行限制,是否可以检索sklearn.tree.DecisionTreeClassifier生成的最终节点数?

解决方法:

部分原因是我没有自己的决策树,因此无法在此处提供具体示例.但是,我认为深入研究源代码可能会有所帮助.

https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/tree/_tree.pyx

sklearn的决策树具有属性tree_,这是基础的Tree对象. Tree对象在_tree.pyx类中定义. Tree对象是:

“Array-based representation of a binary decision tree.
The binary 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.

回想一下,树中有一些内部节点,但是决策树始终是二进制的.因此,当您从节点0开始遍历时,可以访问树属性children_left和children_right以确定哪些节点是每个节点的子级.阈值属性也可能对您有用.

一些可以(强调可能)起作用的伪代码是:

clf = DecisionTreeClassifier()
[... train and test...]
print(clf.tree_.node_count) #get the node count.
print(clf.tree_.children_left[node]) #where node is some integer
print(clf.tree_.children_right[node])
print(clf.tree_.threshold[node])

希望这可以帮助.

标签:scikit-learn,python
来源: https://codeday.me/bug/20191120/2046763.html