Decision tree code
from sklearn.datasets import load_iris
import pydotplus
from IPython.display import Image
from sklearn import tree
#Training Model
iris=load_iris()
clf=tree.DecisionTreeClassifier()
clf=clf.fit(iris.data,iris.target)
#Drawing
dot_data=tree.export_graphviz(decision_tree=clf,
out_file=None,
rounded=True,
filled=True,
feature_names=iris.feature_names)
graph=pydotplus.graph_from_dot_data(dot_data)
Image(graph.create_png())
Installation
Install pydotplus
pip install pydotplus
Running error: graphviz’s executables not found
Installing graphviz
pip install graphviz
Graphviz’s executables not found
After consulting the data, graphviz belongs to an independent software, which needs to be downloaded and installed on the official website
Website: http://www.graphviz.org/Download
Add environment variable
There are two methods, and the second one is recommended for personal test
Method 1: add the path of graphviz’s bin directory in environment variable.
Method 2: OS method
CMD
python
Enter Python environment
imoprt os
os.environ["PATH"]+= os.pathsep+"C:/Program Files/Graphviz/bin" # change to your path of Graphviz
Done!
Operation results
If it is used in jupyter notebook like me, you need to restart jupyter notebook