如何使用 Matplotlib 渲染决策树的完整图像

发布于 2025-01-21 03:00:00 字数 625 浏览 2 评论 0原文

我正在研究一个程序来创建决策树。下面的以下方法应保存树的图像:

def save_image(clf_, filename, feat_names, class_names, show=True):
    """Plot the tree and save to file."""
    plt.figure(dpi=200)
    tree.plot_tree(
        clf_, filled=True,
        feature_names=feat_names,
        class_names=class_names
    )
    plt.savefig(f'{filename}.png')
    if show:
        plt.show()

预期:

完整树的输出图像

实际:

“在此处输入图像描述”

输出的图像不是预期的。我该如何解决这个问题?

I am working on a program to create a decision tree. The following method below should save an image of the tree:

def save_image(clf_, filename, feat_names, class_names, show=True):
    """Plot the tree and save to file."""
    plt.figure(dpi=200)
    tree.plot_tree(
        clf_, filled=True,
        feature_names=feat_names,
        class_names=class_names
    )
    plt.savefig(f'{filename}.png')
    if show:
        plt.show()

Expected:

Output image of full tree

Actual:

enter image description here

The image outputted is not what is expected. How can I trouble-shoot this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文