可以在scikit-learn中修改/修剪学过的树吗?

时间:2016-08-17 16:49:15

标签: python machine-learning scikit-learn random-forest decision-tree

可以使用

访问sklearn中的树参数
tree.tree_.children_left
tree.tree_.children_right
tree.tree_.threshold
tree.tree_.feature

等等

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

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

1 个答案:

答案 0 :(得分:5)

属性都是无法覆盖的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.

要根据节点中的观察数量修剪DecisionTree,我使用此函数。您需要知道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]