如何解决“pydotplus.graph_from_dot_file”中出现的“[Errno 9]错误文件描述符”?

问题描述 投票:0回答:1

我有一个函数设置为使用DeccisionTreeClassifier算法建模一些数据,我可以调整树的最大深度。此函数返回分数,混淆矩阵以及生成带有树的点文件,该文件将转换为svg。顺便说一句,我在Windows工作。

此函数的作用类似于1到5的最大深度的魅力,但是当执行max_depth = 6 +时,它会使用[Errno 9] Bad文件描述符崩溃。

def dtc (x_train, y_train, x_test, y_test, max_depth):
    dtc_model = tree.DecisionTreeClassifier(max_depth=max_depth)
    dtc_model.fit(x_train, y_train)
    dtc_score=dtc_model.score(x_test, y_test)
    dtc_scoret=dtc_model.score(x_train, y_train)
    y_dtc = dtc_model.predict(x_test)
    dtc_matrix= confusion_matrix(y_test,y_dtc)
    tree.export_graphviz(dtc_model,out_file=(r'tree'+str(max_depth)+'.dot'),feature_names=list_variables)
    graph = pydotplus.graph_from_dot_file(r'tree'+str(max_depth)+'.dot')  
    graph.write_svg(r'tree'+str(max_depth)+'.svg')
    return dtc_score, dtc_scoret, dtc_matrix


results1=dtc (x_train, y_train, x_test, y_test, 1)
results2=dtc (x_train, y_train, x_test, y_test, 2)
results3=dtc (x_train, y_train, x_test, y_test, 3)
results4=dtc (x_train, y_train, x_test, y_test, 4)
results5=dtc (x_train, y_train, x_test, y_test, 5)
results6=dtc (x_train, y_train, x_test, y_test, 6)
results7=dtc (x_train, y_train, x_test, y_test, 7)
results8=dtc (x_train, y_train, x_test, y_test, 8)
results9=dtc (x_train, y_train, x_test, y_test, 9)
results10=dtc (x_train, y_train, x_test, y_test, 10)

直到result5一切正常,但在result6之后我得到了这个错误:

Traceback (most recent call last):

  File "<ipython-input-19-60d2876d3701>", line 1, in <module>
    results6=dtc (x_train, y_train, x_test, y_test, 6)

  File "<ipython-input-11-6cb4ba135170>", line 63, in dtc
    graph = pydotplus.graph_from_dot_file(r'tree'+str(max_depth)+'.dot')

  File "C:\XXX\librerias_anaconda\pydotplus\graphviz.py", line 314, in graph_from_dot_file
    data = fd.read()

OSError: [Errno 9] Bad file descriptor

我已经读过这是一个有时会在Windows中发生但却不知道为什么或如何解决的错误。

对不起,如果有任何“发布不好”,第一次问一个问题。提前感谢能够提供任何帮助的任何人

python graphviz dot pydot
1个回答
0
投票

在使用sklearn的graph_from_dot_file函数时,我们一直面临着这类问题。更具体地说,我们使用Anaconda Python 3.6.5并尝试从网络驱动器加载点文件时遇到此行为。

在我们的例子中,切换到本地(非网络驱动器)点位置似乎解决了这个问题。它可能是您的问题的解决方法。

问候,

© www.soinside.com 2019 - 2024. All rights reserved.