4

しかし、これらの変数への書き込みしようとしているが書き込み可能でない例外scikit-learnで学習したツリーを変更/削除することは可能ですか?

を提起修正する方法はあり

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

とそうでsklearnにツリーパラメータにアクセスすることが可能です学習されたツリー、または書き込み不可能なAttributeErrorをバイパスしますか?

答えて

3

属性は、両方とも上書きできない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であることを知る必要があります。この男のための

[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] 
+0

ありがとう:ここ

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の出力を発生する例です。 –

関連する問題