Skip to main content

Python package treeplot vizualizes a tree based on a randomforest or xgboost model.

Project description

treeplot

Python PyPI Version License Downloads Downloads

  • treeplot is Python package to easily plot the tree derived from models such as decisiontrees, randomforest and xgboost. Developing explainable machine learning models is becoming more important in many domains. The most popular and classical explainable models are still tree based. Think of decision trees or random forest. The tree that is learned can be visualized and then explained. However, it can be a challange to simply plot the tree. Think of configuration issues with dot files, path locations to graphviz, differences across operating systems, differences across editors such as jupyter notebook, colab, spyder etc. This frustration led to this library, an easy way to plot the decision trees 🌲. It works for Random-forest, decission trees, xgboost and gradient boosting models. Under the hood it makes many checks, downloads graphviz, sets the path and then plots the tree.

Have fun!

Functions in treeplot

Treeplot can plot the tree for Random-forest, decission trees, xgboost and gradient boosting models:

  • treeplot.plot() : Generic function to plot the tree of any of the four models with default settings
  • treeplot.plot_tree() : Plot the decission tree model. Parameters can be specified.
  • treeplot.randomforest() : Plot the randomforest model. Parameters can be specified.
  • treeplot.xgboost() : Plot the xgboost model. Parameters can be specified.
  • treeplot.import_example('iris') : Import example dataset

Contents

Installation

  • Install treeplot from PyPI (recommended). treeplot is compatible with Python 3.6+ and runs on Linux, MacOS X and Windows.
  • It is distributed under the MIT license.

Quick Start

pip install treeplot
  • Alternatively, install treeplot from the GitHub source:
git clone https://github.com/erdogant/treeplot.git
cd treeplot
python setup.py install

Import treeplot package

import treeplot

Example RandomForest:

# Load example dataset
X,y = treeplot.import_example()
# Learn model
from sklearn.ensemble import RandomForestClassifier
model = RandomForestClassifier(n_estimators=100, max_depth=2, random_state=0).fit(X, y)
# Make plot
ax = treeplot.plot(model)
# or directly
ax = treeplot.randomforest(model)

# If more parameters needs to be specified, use the exact function:
ax = treeplot.randomforest(model, export='pdf')

Example XGboost:

# Load example dataset
X,y = treeplot.import_example()
# Learn model
from xgboost import XGBClassifier
model = XGBClassifier(n_estimators=100, max_depth=2, random_state=0).fit(X, y)
# Make plot
ax = treeplot.plot(model)
# or directly
ax = treeplot.xgboost(model)

# If more parameters needs to be specified, use the exact function:
ax = treeplot.xgboost(model, plottype='vertical')

Maintainers

Contribute

  • Contributions are welcome.

Licence

See LICENSE for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

treeplot-0.1.12.tar.gz (8.7 kB view details)

Uploaded Source

Built Distribution

treeplot-0.1.12-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

Details for the file treeplot-0.1.12.tar.gz.

File metadata

  • Download URL: treeplot-0.1.12.tar.gz
  • Upload date:
  • Size: 8.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.2.0.post20200511 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.2

File hashes

Hashes for treeplot-0.1.12.tar.gz
Algorithm Hash digest
SHA256 6fefda9dd8c49eb2a110ea57dcc1a02ffb494236236e473af4b4a72d1e0edceb
MD5 7d27cc8b2d1339e6af5d228338ff387f
BLAKE2b-256 5a26a78fa84ada326716c3f167e53bf567cd7d9b8c54b5aceff6b9378929c7c7

See more details on using hashes here.

File details

Details for the file treeplot-0.1.12-py3-none-any.whl.

File metadata

  • Download URL: treeplot-0.1.12-py3-none-any.whl
  • Upload date:
  • Size: 8.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.2.0.post20200511 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.2

File hashes

Hashes for treeplot-0.1.12-py3-none-any.whl
Algorithm Hash digest
SHA256 4ce463a9bab61586f5df7d10f55c9a19cabcd400e7b4cdcb5e66cf8513b35361
MD5 7f39664d2701294e0ce79cbd33b13322
BLAKE2b-256 1c38158f794011efb8b6716af51a4c109765511d4737d5b9905028fc45090d79

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page