gpt4 book ai didi

python - 可以在 scikit-learn 中修改/修剪学习树吗?

转载 作者:太空狗 更新时间:2023-10-30 01:06:14 24 4
gpt4 key购买 nike

可以使用 sklearn 访问树参数

tree.tree_.children_left
tree.tree_.children_right
tree.tree_.threshold
tree.tree_.feature

等等

但是,尝试写入这些变量会引发不可写异常

有没有办法修改学习树,或者绕过不可写的AttributeError?

最佳答案

属性都是不可重写的int数组。您仍然可以修改这些数组的元素。这不会减轻数据。

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].

feature : array of int, shape [node_count]
feature[i] holds the feature to split on, for the internal node i.

threshold : array of double, shape [node_count]
threshold[i] holds the threshold for the internal node i.

为了根据节点中的观察数量修剪决策树,我使用了这个函数。您需要知道 TREE_LEAF 常量等于 -1。

def prune(decisiontree, min_samples_leaf = 1):
if decisiontree.min_samples_leaf >= min_samples_leaf:
raise Exception('Tree already more pruned')
else:
decisiontree.min_samples_leaf = min_samples_leaf
tree = decisiontree.tree_
for i in range(tree.node_count):
n_samples = tree.n_node_samples[i]
if n_samples <= min_samples_leaf:
tree.children_left[i]=-1
tree.children_right[i]=-1

这是一个在前后生成 graphviz 输出的示例:

[from sklearn.tree import DecisionTreeRegressor as DTR
from sklearn.datasets import load_diabetes
from sklearn.tree import export_graphviz as export

bunch = load_diabetes()
data = bunch.data
target = bunch.target

dtr = DTR(max_depth = 4)
dtr.fit(data,target)

export(decision_tree=dtr.tree_, out_file='before.dot')
prune(dtr, min_samples_leaf = 100)
export(decision_tree=dtr.tree_, out_file='after.dot')][1]

关于python - 可以在 scikit-learn 中修改/修剪学习树吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39002230/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com