【问题标题】:How can I adjust the size of the plot_tree graph in sklearn to make it readable?如何调整 sklearn 中 plot_tree 图的大小以使其可读?
【发布时间】:2020-03-29 04:13:19
【问题描述】:

我正在尝试用matplotlibsklearn 绘制一个plot_tree 对象,但我的树状图看起来不太好。我的树状图看起来被压扁了:

下面是我的代码:

from sklearn import tree
from sklearn.model_selection import cross_val_score
from sklearn.metrics import accuracy_score
import matplotlib.pyplot as plt

# create tree object 
model_gini_class = tree.DecisionTreeClassifier(criterion='gini')

# train the model using the training sets and check score
model_gini_class.fit(X_train, y_train)
model_gini_class.score(X_train, y_train)

# predict output
predicted_gini_class = model_gini_class.predict(X_test)

plt.figure()
tree.plot_tree(model_gini_class, filled=True)
plt.title("Decision trees on the Shakespear dataset (Gini)")
plt.show() # the tree looks squished?

所以我的问题是:

  • 有人能告诉我如何调整sklearn plot_tree 对象的大小,使其看起来不会被压扁吗?

谢谢,

【问题讨论】:

    标签: python matplotlib scikit-learn tree


    【解决方案1】:

    这可能会有所帮助

    plt.figure(figsize=(10,10))
    

    【讨论】:

      【解决方案2】:

      你可以做两件事:

      方法一

      
      # Decision tree
      classifier = DecisionTreeClassifier()
      classifier.fit(X_train, y_train)
      
      
      _, ax = plt.subplots(figsize=(30,30)) # Resize figure
      plot_tree(classifier, filled=True, ax=ax)
      plt.show()
      

      方法二

      
      # Decision tree
      classifier = DecisionTreeClassifier()
      classifier.fit(X_train, y_train)
      
      plt.figure(figsize=(30, 30) # Resize figure
      plot_tree(classifier, filled=True)
      plt.show()
      

      随心所欲地使用

      【讨论】:

        【解决方案3】:

        这可能会有所帮助

        from matplotlib import pyplot as plt
        fig, axes = plt.subplots(nrows = 1,ncols = 1,figsize = (5,5), dpi=300)
        tree.plot_tree(model_gini_class, filled=True)
        

        【讨论】:

          猜你喜欢
          • 2020-04-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-03-21
          • 1970-01-01
          相关资源
          最近更新 更多