我需要导出决策树的一些高分辨率可视化图像。 Graphviz可以完美地创建图像,我使用PyDotPlus来调整颜色,但是我找不到一种以高分辨率(至少是FullHD)导出图像的方法。
我尝试了this,但是很显然,在我使用PyDotPlus之后,该对象不再是点类型,因此简单的shell命令无效。这是代码。
from sklearn.datasets import load_iris
import pydotplus
import numpy
iris = load_iris()
FEATURES = ['Sepal length (cm)','Sepal width (cm)','Petal length (cm)','Petal width (cm)']
CLASS = ['Iris setosa','Iris versicolor','Iris virginica']
# Model
from sklearn.tree import DecisionTreeClassifier
model = DecisionTreeClassifier(max_depth=1)
# Train
model.fit(iris.data, iris.target)
from sklearn.tree import export_graphviz
dot_data = export_graphviz(model,
feature_names=FEATURES,
out_file=None,
class_names = CLASS,
filled=True,
proportion = False,
precision = 2,
rounded=True,
)
graph = pydotplus.graph_from_dot_data(dot_data)
nodes = graph.get_node_list()
colors = ('lightskyblue3', 'green2', 'lightcoral', 'white')
for node in nodes:
if node.get_name() not in ('node', 'edge'):
values = model.tree_.value[int(node.get_name())][0]
#color only nodes where only one class is present
if max(values) == sum(values):
node.set_fillcolor(colors[numpy.argmax(values)])
#mixed nodes get the default color
else:
node.set_fillcolor(colors[-1])
graph.write_png('Tree.png')
# Display in jupyter notebook
from IPython.display import Image
Image(filename = 'Tree.png')
尽管我可以调整函数“ write_png”,但没有找到任何有关它的文档。当我使用“帮助”时,我只能得到
Help on function <lambda> in module pydotplus.graphviz:
<lambda> lambda path, f='png', prog='dot'
Refer to the docstring accompanying the'write' method for more information.
对不起,我知道它看起来很幼稚,但是我无法解决这个问题。生成的图像对于可视化很有用,但对于纸张而言则不是。有小费吗?预先感谢。