{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# A better way to visualize Decision Trees with the dtreeviz library\n", "\n", "The notebook can contains the code for the accompanying blogpost titled **[A better way to visualize Decision Trees with the dtreeviz library](https://towardsdatascience.com/a-better-way-to-visualize-decision-trees-with-the-dtreeviz-library-758994cdf05e?sk=ad5fcdf665e07388a829bb5320be9a6f)** by [Parul Pandey](https://www.linkedin.com/in/parulpandeyindia/)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Installation\n", "\n", "```\n", "#conda\n", "conda uninstall python-graphviz\n", "conda uninstall graphviz\n", "\n", "#pip\n", "pip install dtreeviz # install dtreeviz for sklearn\n", "pip install dtreeviz[xgboost] # install XGBoost related dependency\n", "pip install dtreeviz[pyspark] # install pyspark related dependency\n", "pip install dtreeviz[lightgbm] # install LightGBM related dependency\n", "This should also pull in the graphviz Python library (>=0.9), which we are using for platform specific stuff.\n", "\n", "```\n", "For details see: https://github.com/parrt/dtreeviz" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "\n", "from sklearn import tree\n", "from dtreeviz.trees import *\n", "import graphviz \n", "\n", "import warnings\n", "warnings.filterwarnings(\"ignore\") \n" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " | fixed acidity | \n", "volatile acidity | \n", "citric acid | \n", "residual sugar | \n", "chlorides | \n", "free sulfur dioxide | \n", "total sulfur dioxide | \n", "density | \n", "pH | \n", "sulphates | \n", "alcohol | \n", "quality | \n", "
---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | \n", "7.4 | \n", "0.70 | \n", "0.00 | \n", "1.9 | \n", "0.076 | \n", "11.0 | \n", "34.0 | \n", "0.9978 | \n", "3.51 | \n", "0.56 | \n", "9.4 | \n", "5 | \n", "
1 | \n", "7.8 | \n", "0.88 | \n", "0.00 | \n", "2.6 | \n", "0.098 | \n", "25.0 | \n", "67.0 | \n", "0.9968 | \n", "3.20 | \n", "0.68 | \n", "9.8 | \n", "5 | \n", "
2 | \n", "7.8 | \n", "0.76 | \n", "0.04 | \n", "2.3 | \n", "0.092 | \n", "15.0 | \n", "54.0 | \n", "0.9970 | \n", "3.26 | \n", "0.65 | \n", "9.8 | \n", "5 | \n", "
3 | \n", "11.2 | \n", "0.28 | \n", "0.56 | \n", "1.9 | \n", "0.075 | \n", "17.0 | \n", "60.0 | \n", "0.9980 | \n", "3.16 | \n", "0.58 | \n", "9.8 | \n", "6 | \n", "
4 | \n", "7.4 | \n", "0.70 | \n", "0.00 | \n", "1.9 | \n", "0.076 | \n", "11.0 | \n", "34.0 | \n", "0.9978 | \n", "3.51 | \n", "0.56 | \n", "9.4 | \n", "5 | \n", "