[Solved] pydotplus generate iris.pdf error: InvocationException: GraphViz’s executables not found

error: InvocationException: GraphViz’s executables not found The
source code is as follows

from itertools import product

import numpy as np
import matplotlib.pyplot as plt

from sklearn import datasets
from sklearn.tree import DecisionTreeClassifier


# Still using the iris data that comes with it
iris = datasets.load_iris()
X = iris.data[:, [0, 2]]
y = iris.target

# Training the model, limiting the maximum depth of the tree to 4
clf = DecisionTreeClassifier(max_depth=4)
#Fitting the model
clf.fit(X, y)


# draw
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, 0.1),
                     np.arange(y_min, y_max, 0.1))

Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)

plt.contourf(xx, yy, Z, alpha=0.4)
plt.scatter(X[:, 0], X[:, 1], c=y, alpha=0.8)
plt.show()

There is no problem up to here, then start generating the image of the spanning tree, here is the code

from IPython.display import Image  
from sklearn import tree
import pydotplus 
dot_data = tree.export_graphviz(clf, out_file=None, 
                         feature_names=iris.feature_names,  
                         class_names=iris.target_names,  
                         filled=True, rounded=True,  
                         special_characters=True)  
graph = pydotplus.graph_from_dot_data(dot_data)  
Image(graph.create_png())

I started to report errors . I
InvocationException: GraphViz's executables not found
learned through Baidu that the environment variables of graphviz were not configured properly,
but I didn’t know where my graphviz was installed,
so I used everything (a very useful software for finding files) to find my graphviz in the bin file. The location
and then edit the environment variables
and finally successfully run the code

Similar Posts:

Leave a Reply

Your email address will not be published. Required fields are marked *