TypeError:join()参数必须为str或字节,而不是'TextIOWrapper

时间:2020-05-29 23:59:48

标签: scikit-learn

我具有要生成决策树的功能和目标变量。但是,该代码将引发错误。由于“输出文件”没有产生错误,因此我认为“ Source.from_file”也不会发生错误,但是有一个错误。

import os
from graphviz import Source
from sklearn.tree import export_graphviz
f = open("C:/Users/julia/Desktop/iris_tree.dot", 'w')
export_graphviz(
        tree_clf,
        out_file=f,
        feature_names=sample2[0:2],
        class_names=sample2[5],
        rounded=True,
        filled=True
    )
Source.from_file(f)

1 个答案:

答案 0 :(得分:0)

docs中所述,from_file接受字符串路径,而不是文件对象:

文件名–用于加载/保存源文件的文件名。

只需通过以下路径即可

import os
from graphviz import Source
from sklearn.tree import export_graphviz
path = "C:/Users/julia/Desktop/iris_tree.dot"
f = open(path, 'w')
export_graphviz(
        tree_clf,
        out_file=f,
        feature_names=sample2[0:2],
        class_names=sample2[5],
        rounded=True,
        filled=True
    )
Source.from_file(path)