I am studying using "100 Python Practice Knock". I think this book is really good because you can study data analysis using data that is close to the actual situation (although there are actually a lot of even worse data ...). In Chapter 5 of this book, there is a scene where scikit-learn is used for analysis using a decision tree. So I created a model, but I tried to visualize the tree structure using graphviz, so I tried it this time.
--Those who are reading 100 Python practice knocks --Those who are looking for a visualization method of decision trees with scikit-learn
Python practice 100 knocks -> Chapter 5 10 knocks to predict customer withdrawal -> Knock 49: Let's check the variables that the model contributes
First, install the main body using homebrew.
brew install graphviz
In addition, install the library for Python using pip (anaconda seems to be able to do it with conda).
pip install graphviz
All you have to do is add the following code. It's very easy.
from sklearn import tree
import graphviz
dot_data = tree.export_graphviz(model, out_file=None)
graph = graphviz.Source(dot_data)
graph
You can create a pdf file with just a few modifications to the last code. In this example, "test.pdf" is created in the current directory.
from sklearn import tree
import graphviz
dot_data = tree.export_graphviz(model, out_file=None)
graph = graphviz.Source(dot_data)
graph.render('test')
I used the export_graphviz function of sklearn.tree to create a decision tree file in DOT language format and run system commands on Jupyter notebook.
from sklearn import tree
import graphviz
with open('test.dot', mode='w') as f:
tree.export_graphviz(model, out_file=f)
!dot -T png test.dot -o test.png
This article was written with reference to the following information. See below for more information. 1.10. Decision Trees (official document) sklearn.tree.export_graphviz (also official documentation) Try the Decision Tree with Python: scikit-learn (I used it as a reference)
Recommended Posts