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
Read More:
- Graphviz Install Error: There is no layout engine support for “dot“
- [Solved] AttributeError: module ‘pandas‘ has no attribute ‘rolling_count‘
- Python environment error, bad interpreter: there is no file or directory
- [Solved] Jupyter Notebook Start Error: Fatal error in launcher: Unable to create process using
- [Solved] jupyter notebook Error: ModuleNotFoundError: No module named jupyter_nbextensions_configurator
- [Python] error in installing jupyter: defaulting to user installation because normal Requirement already satisfied
- [Solved] Python Error: An attempt has been made to start a new process before the current process has finished …
- [Solved] Jupyter runs error: IOPub data rate exceeded
- [Solved] ModuleNotFoundError: No module named ‘requests‘
- [Solved] Python Error: TypeError: write() argument must be str, not bytes
- [Solved] jupyter notebook Error: 500 : Internal Server Error
- ModuleNotFoundError: No module named ‘tensorflow.python’ And the pits encountered after installation
- Python RarFile unpack RAR package Error Rarfile.RarCannotExec: Cannot find Working Tool
- [Solved] Jupyter notebook use pyLDAvis Error: modulenotfounderror: no module named ‘pyLDAvis’‘
- [Solved] MindSpore Error: “operation does not support the type kMetaTypeNone“
- How to Solve PyInstaller Package Error: ModuleNotFoundError: No module named ‘xxxx‘
- [Solved] Networkx Error: Attributeerror: ‘graph’ object has no attribute ‘node’
- [Solved] IDA Start Error: Unexcepted fatal error while intitailizing Python runtime…
- Brew install XXX and display error: [email protected] [How to Solve]
- [Solved] cv2.xfeatures2d.SIFT_create() Error: module ‘cv2.cv2’ has no attribute ‘xfeatures2d’