创建保存png按钮到ipywidgets

时间:2019-11-24 14:22:03

标签: python tree ipywidgets

我具有将分类树显示为ipywidget的功能,如下所示:

labels = X.columns

#functions to create and fit the decision tree
def plot_tree(criterion, depth, split, min_split, min_leaf=0.2):
    #create tree
    clf = DecisionTreeClassifier(criterion = criterion, max_depth = depth, 
                                       splitter = split, min_samples_split=min_split, min_samples_leaf=min_leaf)
    clf.fit(X, y)

    #create graph
    graph = Source(tree.export_graphviz(clf, out_file=None, feature_names=labels, filled=True))

    display(SVG(graph.pipe(format='svg')))

    return clf

#create buttons for interactive graph
inter = interactive(plot_tree, criterion=["gini", "entropy"], depth=np.arange(1, 12),
                   split = ["best", "random"], min_split=(0.1,1), min_leaf=(0.1,0.5))

display(inter)

以及将树另存为png的方法:

png_bytes = graph.pipe(format='png')
with open('../graph/tree.png','wb') as f:
    f.write(png_bytes)
Image(png_bytes);

问题是,如果我将保存部分作为函数放在“ def plot_tree”之后,我将得到png作为只有一个树深度的第一个显示。然后,如果将保存部分添加到另一个单元格中,则将保存整个树。

我想向小部件添加一个按钮,该按钮将保存显示所有设置的参数的显示树。我该怎么办?

0 个答案:

没有答案