我已经阅读了stackoverflow的问题,解决方案似乎是插入完整路径,但是这样做之后,它给了我名称错误。我正在使用Windows 10 python 3.7.1
这是我的代码:
from sklearn.tree import export_graphviz
export_graphviz(
tree_clf,
out_file = image_path("C:/Users/my_name/Desktop/iris_tree.dot"),# path where you want it to output
feature_names=iris.feature_names[2:],
class_names = iris.target_names,
rounded=True,
filled=True
)
答案 0 :(得分:1)
image_path
到底是什么? export_graphviz
接受一个名为out_file
的参数,该参数可以是字符串或文件对象:
文件对象或字符串,可选(默认=无)
所以我会写:
from sklearn.tree import export_graphviz
f = open("C:/Users/my_name/Desktop/iris_tree.dot", 'w')
export_graphviz(
tree_clf,
out_file=f, # path where you want it to output
feature_names=iris.feature_names[2:],
class_names = iris.target_names,
rounded=True,
filled=True
)
如果您正在关注Aurelien Geron撰写的“使用Scikit-Learn和TensorFlow进行机器学习动手”一书,那么这就是他的GitHub中image_path
的定义(仍然没有按照您的意愿做,我只会使用我的第一个解决方案):
import os
# Where to save the figures
PROJECT_ROOT_DIR = "."
CHAPTER_ID = "decision_trees"
def image_path(fig_id):
return os.path.join(PROJECT_ROOT_DIR, "images", CHAPTER_ID, fig_id)
答案 1 :(得分:0)
我以前有同样的问题。基于AdamGold和Jack Stevens,这就是我所做的,我将点文件转换为png(也可以将其转换为pdf)。
from sklearn.tree import export_graphviz
with open("/Users/Desktop/tree.dot", 'w') as f:
export_graphviz(tree_clf,
out_file = f,
feature_names = iris.feature_names[2:],
class_names = iris.target_names,
rounded = True, filled = True)
# convert a dot file to a png/pdf
from graphviz import Source
dot_path = "/Users/Desktop/tree.dot"
output = Source.from_file(dot_path, format = "png") # can change png to pdf
output.view()