我设置了一个函数来用DeccisionTreeClassifier算法对一些数据建模,在那里我可以调整树的最大深度。这个函数返回分数,混淆矩阵,以及它生成一个点文件与树转换成一个svg。顺便说一句,我在Windows工作。
这个函数的工作原理类似于从1到5的最大深度,但是当使用max_depth=6+时,它会使用Errno 9坏文件描述符崩溃。
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,但不知道为什么或如何解决。
抱歉,如果有什么是“坏贴”,第一次问问题。预先感谢任何能提供帮助的人
发布于 2019-03-28 17:04:53
在使用sklearn的graph_from_dot_file函数时,我们一直面临着这样的问题。更具体地说,我们发现这种行为是使用AnacondaPython3.6.5并试图从网络驱动器加载一个点文件。
在我们的例子中,切换到本地(非网络驱动器)点位置似乎解决了这个问题。这可能是一个解决你的问题的办法。
致以敬意,
https://stackoverflow.com/questions/55395965
复制相似问题