[英]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.我正在尝试使用
matplotlib
从sklearn
绘制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:所以我的问题是:
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:你可以做两件事:
# 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()
Whatever you prefer using无论您喜欢使用什么
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.