[PYTHON] Visualize scikit-learn decision trees with Plotly's Treemap

this

image.png

Do this: angel:

image.png

manner

Visualize with plot_tree

First, create a decision tree normally and visualize it with plot_tree.

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.datasets import load_breast_cancer
from sklearn.tree import DecisionTreeClassifier, plot_tree

data = load_breast_cancer()
X, y = data['data'], data['target']
feature_names = data['feature_names']

model = DecisionTreeClassifier(criterion='entropy').fit(X,y)

plt.figure(figsize=(12, 4), dpi=200)
plot_tree(model, feature_names=feature_names, filled=True)
plt.show()

image.png

it is normal.

Visualize with Treemap

Next, let's disassemble the decision tree we made and visualize it with Treemap.

import plotly.graph_objects as go

labels = [''] * model.tree_.node_count
parents = [''] * model.tree_.node_count
labels[0] = 'root'
for i, (f, t, l, r) in enumerate(zip(
    model.tree_.feature,
    model.tree_.threshold,
    model.tree_.children_left,
    model.tree_.children_right,
)):
    if l != r:
        labels[l] = f'{feature_names[f]} <= {t:g}'
        labels[r] = f'{feature_names[f]} > {t:g}'
        parents[l] = parents[r] = labels[i]

fig = go.Figure(go.Treemap(
    branchvalues='total',
    labels=labels,
    parents=parents,
    values=model.tree_.n_node_samples,
    textinfo='label+value+percent root',
    marker=dict(colors=model.tree_.impurity),
    customdata=list(map(str, model.tree_.value)),
    hovertemplate='''
<b>%{label}</b><br>
impurity: %{color}<br>
samples: %{value} (%{percentRoot:%.2f})<br>
value: %{customdata}'''
))
fig.show()

image.png

Nodes that are crushed and invisible can be seen by clicking on the sector.

image.png

the end

Unlike plot_tree, you can't color each class, so it can be difficult to use without binary classification or regression: sweat_smile:

References

Recommended Posts

Visualize scikit-learn decision trees with Plotly's Treemap
Visualize decision trees with jupyter notebook
Visualize the results of decision trees performed with Python scikit-learn
Creating a decision tree with scikit-learn
Predicting the future with machine learning-Predicting future stock prices with scikit-learn decision trees
I tried to visualize all decision trees of random forest with SVG
Isomap with Scikit-learn
DBSCAN with scikit-learn
Clustering with scikit-learn (1)
Clustering with scikit-learn (2)
PCA with Scikit-learn
kmeans ++ with scikit-learn
Cross Validation with scikit-learn
Quickly visualize with Pandas
Multi-class SVM with scikit-learn
Visualize data with Streamlit
Clustering with scikit-learn + DBSCAN
Learn with chemoinformatics scikit-learn
Visualize claims with AI
Visualize 2019 nem with WordCloud
DBSCAN (clustering) with scikit-learn
Install scikit.learn with pip
Calculate tf-idf with scikit-learn
[Machine learning] Understanding decision trees from both scikit-learn and mathematics