【问题标题】:Sklearn plot_tree plot is too smallsklearn plot_tree 图太小
【发布时间】:2020-04-14 06:33:56
【问题描述】:

我有这个简单的代码:

clf = tree.DecisionTreeClassifier()
clf = clf.fit(X, y)

tree.plot_tree(clf.fit(X, y))
plt.show()

我得到的结果是这张图:

如何使这个图表清晰易读?我正在使用 PyCharm Professional 2019.3 作为我的 IDE。

【问题讨论】:

    标签: python graphics sklearn-pandas


    【解决方案1】:

    我认为您正在寻找的设置是fontsize。你必须用max_depthfigsize 来平衡它以获得可读的情节。这是一个例子

    from sklearn import tree
    from sklearn.datasets import load_iris
    import matplotlib.pyplot as plt
    
    # load data
    X, y = load_iris(return_X_y=True)
    
    # create and train model
    clf = tree.DecisionTreeClassifier(max_depth=4)  # set hyperparameter
    clf.fit(X, y)
    
    # plot tree
    plt.figure(figsize=(12,12))  # set plot size (denoted in inches)
    tree.plot_tree(clf, fontsize=10)
    plt.show()
    

    如果你想捕捉整棵树的结构,我想用小字体和高 dpi 保存绘图是解决方案。然后你可以打开一张图片并放大到特定的节点来检查它们。

    # create and train model
    clf = tree.DecisionTreeClassifier()
    clf.fit(X, y)
    
    # save plot
    plt.figure(figsize=(12,12))
    tree.plot_tree(clf, fontsize=6)
    plt.savefig('tree_high_dpi', dpi=100)
    

    这是它在大树上的样子的示例。

    【讨论】:

      【解决方案2】:

      先设置好图片的大小怎么样:

      clf = tree.DecisionTreeClassifier()
      clf = clf.fit(X, y)
      
      fig, ax = plt.subplots(figsize=(10, 10))  # whatever size you want
      tree.plot_tree(clf.fit(X, y), ax=ax)
      plt.show()
      

      【讨论】:

      • 这并没有真正适合 plot_tree 以使其像 OP 想要的那样清晰。所有这一切都是扩展子图,使其适合更多项目,但不会将子图扩展到任何内容都可以作为动态方式读取的程度。
      猜你喜欢
      • 2020-03-29
      • 2021-06-17
      • 2017-07-29
      • 2022-01-23
      • 2021-04-19
      • 2020-06-28
      • 1970-01-01
      • 1970-01-01
      • 2012-05-19
      相关资源
      最近更新 更多