3
4

More than 3 years have passed since last update.

Docker+JupyterLabで決定木モデルの可視化(graphviz/dtreeviz)

Last updated at Posted at 2020-06-21

概要

決定木についての詳細は記載しませんが、決定木によって生成されたモデルの可視化で一般的なのがgraphvizです。これをDocker+JuptyerLabの環境で使おうとしたら微妙にハマった部分があったので、やり方をまとめておきます。またgraphvizではなく、より分かり易い可視化でdtreevizが便利だったのでそちらも記載します。

環境

以下を参考にDockerでJupyterLabが使える状態を前提とします。
Dockerで起動したJupyterLabでvimキーバインドを使う

Dockerfile

Dockerfileに以下追記してビルドし直す事でgraphvizとdtreevizが使えるようになりました。

Dockerfile
RUN pip install pydotplus
RUN pip install dtreeviz
RUN apt-get dist-upgrade
RUN apt-get update
RUN apt-get install -y build-essential graphviz-dev graphviz pkg-config
RUN pip install graphviz

graphvizはPythonのライブラリとLinuxのライブラリで両方入れる必要があり、
Linuxライブラリの方がなかなかうまく入ってくれなかったですが、上記でうまくできました。

Dockerfileの全体についてはこちらにもまとめてあります。
https://github.com/hikarut/Data-Science/blob/master/Dockerfile

モデルの可視化

graphviz

スクリーンショット 2020-06-21 10.34.59.png

  • 1行目:条件式
  • 2行目:ジニ係数
  • 3行目:ノードのサンプル数(サンプル割合)
  • 4行目:クラス毎のサンプル数(サンプル割合)

dtreeviz

スクリーンショット 2020-06-21 10.35.13.png

向きを左→右にする事もできます。
スクリーンショット 2020-06-21 11.02.13.png

ソース
https://github.com/hikarut/Data-Science/blob/master/notebooks/decisionTree/sample.ipynb

参考

3
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4