用pydotplus写png在jupyter中不起作用

Write png with pydotplus doesn't work in jupyter

我尝试从我之前从 data 制作的决策树制作 png。使用包 pydotplus,我想将 png 保存在我的本地存储中并显示它。它在 google colab 中工作,但在 jupyter 中出错。它说

GraphViz's executables not found

这是我的代码

from sklearn.tree import export_graphviz 
from six import StringIO
from IPython.display import Image  
import pydotplus

feature_cols = ['Pregnancies', 'Glucose', 'BloodPressure', 'SkinThickness', 'Insulin',
       'BMI', 'DiabetesPedigreeFunction', 'Age']
dot_data = StringIO()
export_graphviz(clf, out_file=dot_data,  
                filled=True, rounded=True,
                special_characters=True, feature_names = feature_cols, class_names=['0','1'])
graph = pydotplus.graphviz.graph_from_dot_data(dot_data.getvalue())  
graph.write_png('diabetes.png')
Image(graph.create_png())

jupyter中显示的错误

---------------------------------------------------------------------------
InvocationException                       Traceback (most recent call last)
Input In [46], in <cell line: 14>()
     12 graph = pydotplus.graphviz.graph_from_dot_data(dot_data.getvalue())  
     13 # graph.write_png('file//diabetes.png')
---> 14 Image(graph.create_png())

File ~\.conda\envs\butterfly\lib\site-packages\pydotplus\graphviz.py:1797, in Dot.__init__.<locals>.<lambda>(f, prog)
   1792 # Automatically creates all the methods enabling the creation
   1793 # of output in any of the supported formats.
   1794 for frmt in self.formats:
   1795     self.__setattr__(
   1796         'create_' + frmt,
-> 1797         lambda f=frmt, prog=self.prog: self.create(format=f, prog=prog)
   1798     )
   1799     f = self.__dict__['create_' + frmt]
   1800     f.__doc__ = (
   1801         '''Refer to the docstring accompanying the'''
   1802         ''''create' method for more information.'''
   1803     )

File ~\.conda\envs\butterfly\lib\site-packages\pydotplus\graphviz.py:1959, in Dot.create(self, prog, format)
   1957     self.progs = find_graphviz()
   1958     if self.progs is None:
-> 1959         raise InvocationException(
   1960             'GraphViz\'s executables not found')
   1962 if prog not in self.progs:
   1963     raise InvocationException(
   1964         'GraphViz\'s executable "%s" not found' % prog)

InvocationException: GraphViz's executables not found

希望大家能帮我解决这个问题

您好 请在 中尝试这些答案,也许这会有所帮助。或者您可以尝试导入 OpenCV 或 PIL 或 matplotlib 并将变量写入 png 文件。在执行此操作之前安装 OpenCV 库。你可以通过

#Installing OpenCV library
conda install -c anaconda opencv

#importing opencv 
import cv2

#writing a variable as an image but it should be in an array format to achieve this.
cv2.imwrite("graph.png", graph)

我会回答我的问题。

这道题题在文件graphviz中。我可以通过从 graphviz 网站下载包来解决这个问题。如果您从 pip install pydotpluspip install graphviz 下载,您可能会遇到同样的错误。

---------------------------------------------------------------------------
InvocationException                       Traceback (most recent call last)
Input In [46], in <cell line: 14>()
     12 graph = pydotplus.graphviz.graph_from_dot_data(dot_data.getvalue())  
     13 # graph.write_png('file//diabetes.png')
---> 14 Image(graph.create_png())

File ~\.conda\envs\butterfly\lib\site-packages\pydotplus\graphviz.py:1797, in Dot.__init__.<locals>.<lambda>(f, prog)
   1792 # Automatically creates all the methods enabling the creation
   1793 # of output in any of the supported formats.
   1794 for frmt in self.formats:
   1795     self.__setattr__(
   1796         'create_' + frmt,
-> 1797         lambda f=frmt, prog=self.prog: self.create(format=f, prog=prog)
   1798     )
   1799     f = self.__dict__['create_' + frmt]
   1800     f.__doc__ = (
   1801         '''Refer to the docstring accompanying the'''
   1802         ''''create' method for more information.'''
   1803     )

File ~\.conda\envs\butterfly\lib\site-packages\pydotplus\graphviz.py:1959, in Dot.create(self, prog, format)
   1957     self.progs = find_graphviz()
   1958     if self.progs is None:
-> 1959         raise InvocationException(
   1960             'GraphViz\'s executables not found')
   1962 if prog not in self.progs:
   1963     raise InvocationException(
   1964         'GraphViz\'s executable "%s" not found' % prog)

InvocationException: GraphViz's executables not found

问题出在图书馆本身。因此,尝试从 graphviz 网站下载 zip 文件,并将解压文件 bin 指向我的计算机。这个link会帮助你了解细节