[PYTHON] Visualize decision trees with jupyter notebook

Visualize decision trees with jupyter notebook

import numpy as np
import pydotplus
from sklearn.feature_extraction.text import TfidfVectorizer
from IPython import display
from sklearn import tree
from io import StringIO
from operator import itemgetter
%matplotlib inline
%load_ext autoreload
%autoreload 2



input_text=np.array([
    'Physics Mathematics Japanese', #Document 1
    'Bread rice chocolate', #Document 2
    'Building apartment apartment', #Document 3
    'Hokkaido Fukuoka Kanagawa' #Document 4
])

#Document 1 is 1
#Document 2 is 2
#Document 3 is 3
#Document 4 is 4

target_data = np.array([
    1,2,3,4
    ])

#Document tf-Convert to vector of idf
vectorizer=TfidfVectorizer()

input_data=vectorizer.fit_transform(input_text)

np.set_printoptions(precision=2) #Change the number of valid display digits to 2 digits
print(input_data.toarray())

#Generate decision tree model
model = tree.DecisionTreeClassifier(max_depth=5)
model = model.fit(input_data, target_data)

model.predict(input_data)

feature_names = list(map(itemgetter(0),
sorted(vectorizer.vocabulary_.items(), key=itemgetter(1))))

data = StringIO()
tree.export_graphviz(model, out_file=data, feature_names=feature_names)
graph = pydotplus.graph_from_dot_data(data.getvalue())

display.display(display.Image(graph.create_png()))

It looks like this. It feels good to see the Gini count!

スクリーンショット 2017-06-02 14.19.14.png

Recommended Posts

Visualize decision trees with jupyter notebook
Visualize scikit-learn decision trees with Plotly's Treemap
Using Graphviz with Jupyter Notebook
Use pip with Jupyter Notebook
Use Cython with Jupyter Notebook
Play with Jupyter Notebook (IPython Notebook)
Allow external connections with jupyter notebook
Formatting with autopep8 on Jupyter notebook
Visualize the results of decision trees performed with Python scikit-learn
Make a sound with Jupyter notebook
Use markdown with jupyter notebook (with shortcut)
Add more kernels with Jupyter Notebook
Convenient analysis with Pandas + Jupyter notebook
Use nb extensions with Anaconda's Jupyter notebook
Use apache Spark with jupyter notebook (IPython notebook)
I want to blog with Jupyter Notebook
Use Jupyter Lab and Jupyter Notebook with EC2
Try SVM with scikit-learn on Jupyter Notebook
How to use jupyter notebook with ABCI
Linking python and JavaScript with jupyter notebook
[Jupyter Notebook memo] Display kanji with matplotlib
Rich cell output with Jupyter Notebook (IPython)
Jupyter Notebook memo
Introducing Jupyter Notebook
Powerful Jupyter Notebook
Jupyter notebook password
Jupyter Notebook memo
I tried to visualize all decision trees of random forest with SVG
How to debug with Jupyter or iPython Notebook
When Html cannot be output with Jupyter Notebook
Analytical environment construction with Docker (jupyter notebook + PostgreSQL)
Data analysis for improving POG 2 ~ Analysis with jupyter notebook ~
Verify NLC accuracy with Watson Studio's Jupyter Notebook
Enable Jupyter Notebook with conda on remote server
Try using conda virtual environment with Jupyter Notebook
Interactively visualize data with TreasureData, Pandas and Jupyter.
Fill the browser with the width of Jupyter Notebook
Graph drawing with jupyter (ipython notebook) + matplotlib + vagrant
Multiple selections with Jupyter
Candlestick with plotly + Jupyter
Easy Machine Learning with AutoAI (Part 4) Jupyter Notebook Edition
Quickly visualize with Pandas
Get started Jupyter Notebook
Visualize data with Streamlit
Monitor the training model with TensorBord on Jupyter Notebook
Visualize claims with AI
Visualize 2019 nem with WordCloud
Drawing a tree structure with D3.js in Jupyter Notebook
Import specific cells from other notebooks with Jupyter notebook
EC2 provisioning with Vagrant + Jupyter (IPython Notebook) on Docker
3 Jupyter notebook (Python) tricks
Use nim with Jupyter
[Cloud103] # 3 Jupyter Notebook again
Machine learning with Jupyter Notebook in OCI Always Free environment (2019/12/17)
Reflect the virtual environment created with Miniconda in Jupyter notebook
Day 65 (Solution) Jupyter notebook does not work with Kernel Not Conected.
I want to use a virtual environment with jupyter notebook!
The usual way to add a Kernel with Jupyter Notebook
Connect the Jupyter Notebook kernel to Spyder with Jupytext enabled
Write charts in real time with Matplotlib on Jupyter notebook
Try clustering with a mixed Gaussian model on a Jupyter Notebook