简体   繁体   中英

scikit-learn tree.DecisionTreeClassifier export to pdf too wide to fit

I followed the the instructions in this thread to get my export working Python, PyDot and DecisionTree but a pdf export truncates the tree after a page width and the png is too small to read. Is there a way of creating a pdf with unlimited width?

My code is

from sklearn import tree

clf = tree.DecisionTreeClassifier().fit(X_train, y_train)


import pydotplus
from IPython.display import Image
from sklearn.externals.six import StringIO
dot_data = StringIO()
tree.export_graphviz(clf, out_file=dot_data,feature_names=cols_scaled.columns) 
graph = pydotplus.graph_from_dot_data(dot_data.getvalue()) 
Image(graph.create_png())
graph.write_pdf("decisiontree.pdf")

Many thanks

Try using the following code, PDF requires width and let me know if it worked.

from sklearn.externals.six import StringIO
import pydotplus   #using pydotplus in windows10, python 3.6.X
dot_data = StringIO()

tree.export_graphviz(clf, out_file=dot_data, 
                         feature_names=cols_scaled.column,  
                         class_names=<replace target column here>,  
                         filled=True, rounded=True,  
                         special_characters=True)  
graph = pydotplus.graph_from_dot_data(dot_data.getvalue())  
graph.write_pdf("iris.pdf")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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