简体   繁体   English

如何在 sklearn 中调整 plot_tree 图的大小以使其可读?

[英]How can I adjust the size of the plot_tree graph in sklearn to make it readable?

I am trying to plot a plot_tree object from sklearn with matplotlib , but my tree plot doesn't look good.我正在尝试使用matplotlibsklearn绘制plot_tree对象,但我的树图看起来不太好。 My tree plot looks squished:我的树图看起来被压扁了:

在此处输入图片说明

Below are my code:下面是我的代码:

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?

So my questions is:所以我的问题是:

  • Could someone tell me how to adjust the size of the sklearn plot_tree object so it doesn't look squished?有人能告诉我如何调整sklearn plot_tree 对象的大小,使其看起来不会被压扁吗?

Thank you,谢谢,

这可能有帮助

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

this might help这可能有帮助

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)

You can do two things:你可以做两件事:

Method 1方法一


# 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()

Method 2方法二


# Decision tree
classifier = DecisionTreeClassifier()
classifier.fit(X_train, y_train)

plt.figure(figsize=(30, 30) # Resize figure
plot_tree(classifier, filled=True)
plt.show()

Whatever you prefer using无论您喜欢使用什么

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM