Visualizing decision tree in scikit-learn

后端 未结 11 2186
广开言路
广开言路 2020-12-02 10:26

I am trying to design a simple Decision Tree using scikit-learn in Python (I am using Anaconda\'s Ipython Notebook with Python 2.7.3 on Windows OS) and visualize it as follo

11条回答
  •  日久生厌
    2020-12-02 11:26

    sklearn.tree.export_graphviz doesn't return anything, and so by default returns None.

    By doing dotfile = tree.export_graphviz(...) you overwrite your open file object, which had been previously assigned to dotfile, so you get an error when you try to close the file (as it's now None).

    To fix it change your code to

    ...
    dotfile = open("D:/dtree2.dot", 'w')
    tree.export_graphviz(dtree, out_file = dotfile, feature_names = X.columns)
    dotfile.close()
    ...
    

提交回复
热议问题