當我在visualize決策樹,運行以下代碼時,報錯: InvocationException: GraphViz's executables not found
import pydotplus from io import StringIO dot_data = StringIO() tree.export_graphviz(clf, out_file=dot_data, feature_names=['age','sex','1st_class','2nd_class', '3rd_class']) graph = pydotplus.graph_from_dot_data(dot_data.getvalue()) graph.write_png('titanic.png') from IPython.core.display import Image Image(filename='titanic.png')
查閱資料后發現,原來我沒有安裝GraphViz's executables.我是用pip安裝的Graphviz,但是Graphviz不是一個python tool,你仍然需要安裝GraphViz's executables.
problem solved:
You need to install the GraphViz package <http://www.graphviz.org/> so that graph.dag can invoke dot.
安裝完之后,再運行還是報錯:Exception: "dot.exe" not found in path.
那是因為你沒有將GraphViz安裝目錄的bin目錄放到環境變量的path路徑中,dot.exe在那個目錄下。
Note: As of version 2.31, the Visual Studio package no longer alters the PATH variable or accesses the registry at all. If you wish to use the command-line interface to Graphviz or are using some other program that calls a Graphviz program, you will need to set the PATH variable yourself.
設置完如果還是報同樣的錯誤,你需要重啟pyhon IDE.
REF.