scikit-learn tree.DecisionTreeClassifier导出到pdf太宽,不适合

时间:2017-06-07 16:01:52

标签: python scikit-learn

我按照此主题中的说明进行导出工作Python, PyDot and DecisionTree 但是pdf导出会在页面宽度后截断树,并且png太小而无法读取。有没有办法创建宽度不限的pdf?

我的代码是

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")

非常感谢

1 个答案:

答案 0 :(得分:0)

尝试使用以下代码,PDF需要宽度并让我知道它是否有效。

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")