### Install pltvid Animation Library Source: https://github.com/parrt/dtreeviz/blob/master/notebooks/classifier-boundary-animations.ipynb Installs the pltvid library, which is used for creating simple animations in the examples. This is a prerequisite for running the animation code. ```python ! pip install --quiet -U pltvid # simple animation support by parrt ``` -------------------------------- ### Install dtreeviz Source: https://github.com/parrt/dtreeviz/blob/master/notebooks/classifier-decision-boundaries.ipynb Installs the dtreeviz library. This is necessary for using the decision boundary visualization functions. ```python import sys if 'google.colab' in sys.modules: !pip install -q dtreeviz ``` -------------------------------- ### Install Graphviz on Ubuntu Source: https://github.com/parrt/dtreeviz/blob/master/README.md Install the graphviz package on Ubuntu 18.04 systems using apt. ```bash sudo apt install graphviz ``` -------------------------------- ### Install dtreeviz with development dependencies Source: https://github.com/parrt/dtreeviz/blob/master/README.md Install the dtreeviz library along with extra dependencies required for development, such as testing and linting. ```bash pip install dtreeviz[dev] ``` -------------------------------- ### Install All dtreeviz Dependencies Source: https://github.com/parrt/dtreeviz/blob/master/README.md Install dtreeviz with all available dependencies. Ensure you are using an Anaconda Prompt on Windows. ```bash pip install dtreeviz[all] # install all related dependencies ``` -------------------------------- ### Install dtreeviz with AI Chat/Explanation Features Source: https://github.com/parrt/dtreeviz/blob/master/README.md Install dtreeviz with AI chat and explanation features. This requires an OpenAI API key to be set as an environment variable. ```bash pip install dtreeviz[ai] # install AI chat/explanation features (requires OpenAI API key) ``` -------------------------------- ### Configure plotting backend and install packages Source: https://github.com/parrt/dtreeviz/blob/master/notebooks/dtreeviz_lightgbm_visualisations.ipynb Configures the matplotlib backend for better visualization quality and installs dtreeviz and lightgbm if running in Google Colab. ```python %config InlineBackend.figure_format = 'retina' # Make visualizations look good #%config InlineBackend.figure_format = 'svg' %matplotlib inline if 'google.colab' in sys.modules: !pip install -q dtreeviz !pip install -q lightgbm ``` -------------------------------- ### Install dtreeviz locally with force update Source: https://github.com/parrt/dtreeviz/blob/master/README.md Force the installation or update of the dtreeviz library to your local egg cache. This is useful during development to ensure you are using the latest changes. ```bash python setup.py install -f ``` -------------------------------- ### Fill Polygon Example Source: https://github.com/parrt/dtreeviz/blob/master/testing/playground.ipynb A simple Matplotlib example demonstrating how to fill a polygon defined by a set of x and y coordinates. ```python x = [0,3,2] y = [0,4,5] plt.fill(x,y,c='blue',alpha=.5,edgecolor='grey',lw=.3) ``` -------------------------------- ### Install dtreeviz with LightGBM Dependency Source: https://github.com/parrt/dtreeviz/blob/master/README.md Install dtreeviz along with dependencies required for LightGBM models. Ensure you are using an Anaconda Prompt on Windows. ```bash pip install dtreeviz[lightgbm] # install LightGBM related dependency ``` -------------------------------- ### Basic dtreeviz Visualization Example Source: https://github.com/parrt/dtreeviz/blob/master/README.md This example demonstrates how to train a scikit-learn decision tree and visualize it using dtreeviz. It shows how to obtain a model adaptor, render the tree, display it in a popup window, and save it as an SVG file. ```python from sklearn.datasets import load_iris from sklearn.tree import DecisionTreeClassifier import dtreeviz iris = load_iris() X = iris.data y = iris.target clf = DecisionTreeClassifier(max_depth=4) clf.fit(X, y) viz_model = dtreeviz.model(clf, X_train=X, y_train=y, feature_names=iris.feature_names, target_name='iris', class_names=iris.target_names) v = viz_model.view() # render as SVG into internal object v.show() # pop up window v.save("/tmp/iris.svg") # optionally save as svg ``` ```python viz_model.view() # in notebook, displays inline ``` -------------------------------- ### Configure plotting backend and install libraries Source: https://github.com/parrt/dtreeviz/blob/master/notebooks/dtreeviz_xgboost_visualisations.ipynb Sets the matplotlib backend for high-resolution plots and installs `dtreeviz` and a specific version of `xgboost` if running in Google Colab. ```python %config InlineBackend.figure_format = 'retina' # Make visualizations look good #%config InlineBackend.figure_format = 'svg' %matplotlib inline if 'google.colab' in sys.modules: !pip install -q dtreeviz !pip install -q xgboost==1.5.0 ``` -------------------------------- ### Configure matplotlib backend and install dtreeviz/pyspark in Colab Source: https://github.com/parrt/dtreeviz/blob/master/notebooks/dtreeviz_spark_visualisations.ipynb Configures the matplotlib backend for better visualization quality and installs required libraries (dtreeviz, pyspark) if running in a Google Colab environment. This setup ensures that visualizations are displayed inline and with high resolution. ```python %config InlineBackend.figure_format = 'retina' # Make visualizations look good #%config InlineBackend.figure_format = 'svg' %matplotlib inline if 'google.colab' in sys.modules: !pip install -q dtreeviz !pip install -q pyspark ``` -------------------------------- ### Install dtreeviz for Sklearn Source: https://github.com/parrt/dtreeviz/blob/master/README.md Install the dtreeviz library for use with scikit-learn models. Ensure you are using an Anaconda Prompt on Windows. ```bash pip install dtreeviz # install dtreeviz for sklearn ``` -------------------------------- ### Verify Graphviz Dot Binary Source: https://github.com/parrt/dtreeviz/blob/master/README.md Verify that the graphviz 'dot' binary is installed and accessible. Running this command should not produce an error. ```bash dot -Tsvg ``` -------------------------------- ### Install dtreeviz with PySpark Dependency Source: https://github.com/parrt/dtreeviz/blob/master/README.md Install dtreeviz along with dependencies required for PySpark models. Ensure you are using an Anaconda Prompt on Windows. ```bash pip install dtreeviz[pyspark] # install pyspark related dependency ``` -------------------------------- ### Install dtreeviz and TensorFlow Decision Forests Source: https://github.com/parrt/dtreeviz/blob/master/notebooks/dtreeviz_tensorflow_visualisations.ipynb Installs the necessary libraries for dtreeviz and TensorFlow Decision Forests, particularly useful in Google Colab environments. Ensures visualizations render with high fidelity. ```python import sys %config InlineBackend.figure_format = 'retina' # Make visualizations look good #%config InlineBackend.figure_format = 'svg' %matplotlib inline if 'google.colab' in sys.modules: !pip install -q dtreeviz !pip install -q tensorflow_decision_forests ``` -------------------------------- ### Install dtreeviz with XGBoost Dependency Source: https://github.com/parrt/dtreeviz/blob/master/README.md Install dtreeviz along with dependencies required for XGBoost models. Ensure you are using an Anaconda Prompt on Windows. ```bash pip install dtreeviz[xgboost] # install XGBoost related dependency ``` -------------------------------- ### Verify graphviz version Source: https://github.com/parrt/dtreeviz/blob/master/README.md Confirm that the graphviz installation is recognized by the system by checking its version. Use a capital '-V' for version check. ```bash dot -V ``` -------------------------------- ### Install dtreeviz in Google Colab Source: https://github.com/parrt/dtreeviz/blob/master/notebooks/dtreeviz_sklearn_pipeline_visualisations.ipynb Installs the dtreeviz library quietly if running in a Google Colab environment. Ensures visualizations look good by setting the inline backend format to 'retina'. ```python %config InlineBackend.figure_format = 'retina' # Make visualizations look good #%config InlineBackend.figure_format = 'svg' %matplotlib inline if 'google.colab' in sys.modules: !pip install -q dtreeviz ``` -------------------------------- ### Bivariate Tree Color Customization Setup Source: https://github.com/parrt/dtreeviz/blob/master/notebooks/colors.ipynb Sets up data and a decision tree classifier for bivariate visualization. This code prepares the data and trains a model to be used with dtreeviz for color customization. ```python features=[4,3] X_train = know.drop('UNS', axis=1) y_train = know['UNS'] X_train = X_train.values[:, features] dtс_bivar = DecisionTreeClassifier(max_depth=3) dtс_bivar.fit(X_train, y_train) def change_ctreeviz_bivar(colors): viz_model = dtreeviz.model(dtc_bivar, X_train, y_train, feature_names=['PEG','LPR'], target_name="Knowledge", class_names=class_names) ``` -------------------------------- ### Train a Spark Decision Tree Classifier Source: https://github.com/parrt/dtreeviz/blob/master/notebooks/dtreeviz_spark_visualisations.ipynb This snippet shows the setup for training a DecisionTreeClassifier in Spark using a pipeline. Ensure all necessary Spark MLlib components and data are imported and prepared. ```python features = ["Pclass", "Sex_label", "Embarked_label", "Age", "SibSp", "Parch", "Fare"] target = "Survived" vector_assembler = VectorAssembler(inputCols=features, outputCol="features") decision_tree = DecisionTreeClassifier(featuresCol="features", labelCol="Survived", maxDepth=4, seed=1234) pipeline = Pipeline(stages=[sex_label_indexer, embarked_label_indexer, age_imputer, vector_assembler, decision_tree]) model = pipeline.fit(data) ``` -------------------------------- ### Visualize Univariate Classification Tree for Knowledge Dataset Source: https://github.com/parrt/dtreeviz/blob/master/testing/playground.ipynb Generates a univariate classification tree visualization for a custom knowledge dataset. This example demonstrates mapping categorical string labels to numerical indices before visualization. Requires pandas, matplotlib, and dtreeviz. ```python know = pd.read_csv("data/knowledge.csv") class_names = ['very_low', 'Low', 'Middle', 'High'] know['UNS'] = know['UNS'].map({n: i for i, n in enumerate(class_names)}) max_depth=3 x_train = know.PEG y_train = know['UNS'] figsize = (6,1.5) fig, ax = plt.subplots(1, 1, figsize=figsize) ctreeviz_univar(ax, x_train, y_train, max_depth=max_depth, feature_name = 'PEG', target_name='variety', class_names=class_names, nbins=40, gtype='strip') plt.tight_layout() plt.savefig(f"/tmp/knowlege-classtree-depth-{max_depth}.svg", bbox_inches=0, pad_inches=0) plt.show() ``` -------------------------------- ### Generate SVG from DOT file Source: https://github.com/parrt/dtreeviz/blob/master/README.md This command-line instruction demonstrates how to convert a simple DOT language file ('t.dot') into an SVG image ('t.svg') using the 'dot' executable. It's a fundamental test for graphviz installation. ```bash dot -Tsvg -o t.svg t.dot ``` -------------------------------- ### Get Descriptive Statistics for a Node's Samples Source: https://context7.com/parrt/dtreeviz/llms.txt Returns a pandas describe() table for all training samples reaching a specific node in a classifier. Useful for understanding split criteria and subpopulations. Requires fitting a DecisionTreeClassifier. ```python from sklearn.datasets import load_breast_cancer from sklearn.tree import DecisionTreeClassifier import dtreeviz bc = load_breast_cancer() clf = DecisionTreeClassifier(max_depth=3).fit(bc.data, bc.target) viz = dtreeviz.model(clf, X_train=bc.data, y_train=bc.target, feature_names=list(bc.feature_names), target_name="diagnosis", class_names=["malignant", "benign"]) # Inspect the root node (node 0) stats_df = viz.node_stats(node_id=0) # Inspect a specific leaf stats_df = viz.node_stats(node_id=5) # Returns a DataFrame with count, mean, std, min, 25%, 50%, 75%, max for each feature ``` -------------------------------- ### Remove Anaconda Graphviz Installation Source: https://github.com/parrt/dtreeviz/blob/master/README.md Remove graphviz-related packages from your Anaconda installation to avoid conflicts with pip-installed versions. ```bash conda uninstall python-graphviz ``` ```bash conda uninstall graphviz ``` -------------------------------- ### Build and Upload dtreeviz to PyPI Source: https://github.com/parrt/dtreeviz/blob/master/releasing.txt Use the 'build' package to create distribution archives and 'twine' to upload them to the Python Package Index. Ensure you have the correct version numbers in the filenames. ```bash python -m build ``` ```bash twine upload dist/dtreeviz-2.3.2.tar.gz dist/dtreeviz-2.3.2-py3-none-any.whl ``` -------------------------------- ### Configure OpenAI API key for AI features Source: https://github.com/parrt/dtreeviz/blob/master/notebooks/dtreeviz_sklearn_AI_visualisations.ipynb Sets up the OpenAI API key, prioritizing environment variables and offering secure input via getpass if not found. This is necessary for AI-powered explanations. ```python # OpenAI API key setup for AI-powered explanations and chat functionality # The key is checked in this order: # 1. Already set in environment (e.g., from shell or .env file) # 2. Manual entry below (uncomment and set your key) if not os.environ.get("OPENAI_API_KEY"): # Option 1: Set your API key directly (not recommended for shared notebooks) # os.environ["OPENAI_API_KEY"] = "your-openai-api-key-here" # Option 2: Use getpass for secure input (recommended for notebooks) from getpass import getpass os.environ["OPENAI_API_KEY"] = getpass("Enter your OpenAI API key: ") else: print("✓ OPENAI_API_KEY found in environment") ``` -------------------------------- ### Select Instance for Prediction Path Source: https://github.com/parrt/dtreeviz/blob/master/notebooks/dtreeviz_tensorflow_visualisations.ipynb Select a specific instance from the dataset to demonstrate prediction path explanations. ```python x = dataset[features].iloc[10] x ``` -------------------------------- ### Initialize SparkSession Source: https://github.com/parrt/dtreeviz/blob/master/notebooks/dtreeviz_spark_visualisations.ipynb Creates a SparkSession with a local master and two threads, named 'dtreeviz_sparkml'. This is the entry point for any Spark functionality. ```python spark = SparkSession.builder \ .master("local[2]") \ .appName("dtreeviz_sparkml") \ .getOrCreate() ``` -------------------------------- ### Prepare and Train LightGBM Regressor Model Source: https://github.com/parrt/dtreeviz/blob/master/notebooks/dtreeviz_lightgbm_visualisations.ipynb Splits data, prepares LightGBM datasets, and trains a regression model with specified parameters. Ensure 'dataset' and 'lgb' are imported and available. ```python features_reg = ["Pclass", "Fare", "Survived", "Sex_label", "Cabin_label", "Embarked_label"] target_reg = "Age" X_train_reg, X_test_reg, y_train_reg, y_test_reg = train_test_split(dataset[features_reg], dataset[target_reg], test_size=0.2, random_state=42) categorical_feature=["Pclass", "Survived", "Sex_label"] train_data_reg = lgb.Dataset(data=X_train_reg, label=y_train_reg) valid_data_reg = lgb.Dataset(data=X_test_reg, label=y_test_reg) lgbm_params = { 'num_tree':10, 'boosting': 'dart', # dart (drop out trees) often performs better 'application': 'regression_l1', 'learning_rate': 0.05, # Learning rate, controls size of a gradient descent step 'min_data_in_leaf': 20, # Data set is quite small so reduce this a bit 'feature_fraction': 0.7, # Proportion of features in each boost, controls overfitting 'num_leaves': 41, # Controls size of tree since LGBM uses leaf wise splits 'drop_rate': 0.15, 'max_depth':4, "seed":1212} lgbm_reg_model = lgb.train(lgbm_params, train_data_reg, valid_sets=[train_data_reg, valid_data_reg], categorical_feature=categorical_feature, verbose_eval=False) ``` -------------------------------- ### Import core ML and visualization libraries Source: https://github.com/parrt/dtreeviz/blob/master/notebooks/dtreeviz_lightgbm_visualisations.ipynb Imports libraries for numerical operations, data manipulation, LightGBM modeling, train-test splitting, plotting, and dtreeviz visualization. ```python import numpy as np import pandas as pd import lightgbm as lgb from sklearn.model_selection import train_test_split import matplotlib.pyplot as plt import dtreeviz random_state = 1234 # get reproducible trees ``` -------------------------------- ### Install dtreeviz with TensorFlow Decision Forests Dependency Source: https://github.com/parrt/dtreeviz/blob/master/README.md Install dtreeviz along with dependencies required for TensorFlow Decision Forests. Ensure you are using an Anaconda Prompt on Windows. ```bash pip install dtreeviz[tensorflow_decision_forests] # install tensorflow_decision_forests related dependency ``` -------------------------------- ### Initialize dtreeviz Model Adaptor for LightGBM Source: https://github.com/parrt/dtreeviz/blob/master/notebooks/dtreeviz_lightgbm_visualisations.ipynb Initializes the dtreeviz model adaptor for a LightGBM regressor. Requires the trained model, tree index, training data (features and target), and feature/target names. ```python viz_rmodel = dtreeviz.model(model=lgbm_reg_model, tree_index=1, X_train=dataset[features_reg], y_train=dataset[target_reg], feature_names=features_reg, target_name=target_reg) ``` -------------------------------- ### Prepare and Train LightGBM Classifier Source: https://github.com/parrt/dtreeviz/blob/master/notebooks/dtreeviz_lightgbm_visualisations.ipynb Use this code to split data, create LightGBM datasets, and train a binary classifier. Ensure 'dataset', 'train_test_split', and 'lgb' are imported. ```python features = ["Pclass", "Sex_label", "Embarked_label", "Age", "Cabin_label", "Fare"] target = "Survived" X_train, X_test, y_train, y_test = train_test_split(dataset[features], dataset[target], test_size=0.2, random_state=42) train_data = lgb.Dataset(data=X_train, label=y_train, feature_name=features, categorical_feature=["Sex_label", "Pclass"]) valid_data = lgb.Dataset(data=X_test, label=y_test, feature_name=features, categorical_feature=["Sex_label", "Pclass"]) lgbm_params = { 'boosting': 'dart', # dart (drop out trees) often performs better 'application': 'binary', # Binary classification 'learning_rate': 0.05, # Learning rate, controls size of a gradient descent step 'min_data_in_leaf': 2, # Data set is quite small so reduce this a bit 'feature_pre_filter': False, 'feature_fraction': 0.7, # Proportion of features in each boost, controls overfitting 'num_leaves': 41, # Controls size of tree since LGBM uses leaf wise splits 'drop_rate': 0.15, 'max_depth':4, "seed":1212} lgbm_model = lgb.train(lgbm_params, train_data, valid_sets=[train_data, valid_data], verbose_eval=False) ``` -------------------------------- ### Import ML and dtreeviz libraries Source: https://github.com/parrt/dtreeviz/blob/master/notebooks/dtreeviz_sklearn_AI_visualisations.ipynb Imports pandas for data manipulation, scikit-learn for decision trees, and dtreeviz for visualization and AI explanations. ```python import pandas as pd from sklearn.tree import DecisionTreeClassifier, DecisionTreeRegressor import dtreeviz from dtreeviz import ai_explanation random_state = 1234 # get reproducible trees ``` -------------------------------- ### Get node statistics Source: https://github.com/parrt/dtreeviz/blob/master/notebooks/dtreeviz_sklearn_visualisations.ipynb Retrieve detailed statistics for a specific node in the decision tree, identified by its node ID. ```python viz_model.node_stats(node_id=6) ``` -------------------------------- ### Visualize Tree with Specific Instance Source: https://github.com/parrt/dtreeviz/blob/master/notebooks/dtreeviz_lightgbm_visualisations.ipynb Visualizes the LightGBM tree highlighting the path for a specific instance 'x'. First, select an instance from the training data. ```python x = dataset[features_reg].iloc[10] x ``` ```python viz_rmodel.view(x = x) ``` -------------------------------- ### Get prediction path for an instance Source: https://github.com/parrt/dtreeviz/blob/master/notebooks/dtreeviz_sklearn_visualisations.ipynb Select a specific instance from the dataset to visualize its prediction path through the decision tree. ```python x = dataset[features].iloc[10] x ``` ```python viz_model.view(x=x) ``` ```python viz_model.view(x=x, show_just_path=True) ``` -------------------------------- ### Get Node Statistics Source: https://github.com/parrt/dtreeviz/blob/master/notebooks/dtreeviz_tensorflow_visualisations.ipynb Retrieve detailed statistics for a specific node in the decision tree, identified by its node ID. ```python viz_model.node_stats(node_id=4) ``` -------------------------------- ### Get Node Statistics Source: https://github.com/parrt/dtreeviz/blob/master/notebooks/dtreeviz_xgboost_visualisations.ipynb Obtain detailed statistics for a specific node in the tree by providing the node_id to the node_stats() method. ```python viz_model.node_stats(node_id=10) ``` -------------------------------- ### Prepare data for bivariate decision tree visualization Source: https://github.com/parrt/dtreeviz/blob/master/testing/slides.ipynb Sets up the data from the wine dataset for a bivariate decision tree visualization, selecting 'proline' and 'flavanoids' as features. ```python max_depth = 2 features = [12,6] feature_names=['proline','flavanoids'] figsize = (6, 5) fig, ax = plt.subplots(1, 1, figsize=figsize) X_train = wine.data ``` -------------------------------- ### Import XGBoost and dtreeviz components Source: https://github.com/parrt/dtreeviz/blob/master/notebooks/dtreeviz_xgboost_visualisations.ipynb Imports XGBoost, dtreeviz, graphviz, matplotlib, pandas, and numpy. Sets a random state for reproducible results. ```python import xgboost as xgb from xgboost import plot_importance, plot_tree, plotting import dtreeviz import graphviz import matplotlib.pyplot as plt from matplotlib.pylab import rcParams import pandas as pd import numpy as np random_state = 1234 # get reproducible trees ``` -------------------------------- ### Remove Dot from Anaconda Source: https://github.com/parrt/dtreeviz/blob/master/README.md Remove the 'dot' executable from your Anaconda installation to ensure the correct version (e.g., from Homebrew) is used. ```bash rm ~/anaconda3/bin/dot ``` -------------------------------- ### Initialize dtreeviz Model for Regression Source: https://github.com/parrt/dtreeviz/blob/master/notebooks/dtreeviz_sklearn_AI_visualisations.ipynb Initializes the dtreeviz model object for visualization with regression data. Requires importing dtreeviz. ```python viz_rmodel = dtreeviz.model(dtr_cars, X, y, feature_names=features, target_name='MPG') ``` -------------------------------- ### Initialize dtreeviz Model with AI Chat Source: https://github.com/parrt/dtreeviz/blob/master/notebooks/dtreeviz_sklearn_AI_visualisations.ipynb Initializes a dtreeviz model for visualization, enabling AI chat features for explanations. Specify the trained model, training data, feature names, target name, and AI model details. Ensure the AI model and max history messages are configured as needed. ```python viz_rmodel = dtreeviz.model(model=tree_regressor, X_train=dataset[features_reg], y_train=dataset[target_reg], feature_names=features_reg, target_name=target_reg, ai_chat=True, ai_model="gpt-4.1-mini", max_history_messages=10) ``` -------------------------------- ### Get a Specific Data Instance Source: https://github.com/parrt/dtreeviz/blob/master/notebooks/dtreeviz_spark_visualisations.ipynb Selects a specific row (index 10) from the prepared dataset to use for prediction visualization. ```python x = dataset_reg[features_reg].iloc[10] ``` -------------------------------- ### Prepare data and models for dtreeviz Source: https://github.com/parrt/dtreeviz/blob/master/notebooks/colors.ipynb Loads datasets (Iris, California Housing), trains decision tree classifiers and regressors, and prepares data for 3D visualization. ```python from sklearn.tree import DecisionTreeClassifier, DecisionTreeRegressor import dtreeviz #data for classifier classifier = DecisionTreeClassifier(max_depth=2) iris = load_iris() classifier.fit(iris.data, iris.target) #date for regressor regr = DecisionTreeRegressor(max_depth=2) housing = fetch_california_housing() regr.fit(housing.data, housing.target) #data for bivar_3D dataset_url = "https://raw.githubusercontent.com/parrt/dtreeviz/master/data/cars.csv" df_cars = pd.read_csv(dataset_url) X = df_cars.drop('MPG', axis=1) y = df_cars['MPG'] features = [2, 1] X = X.values[:,features] ``` -------------------------------- ### Check Spark version Source: https://github.com/parrt/dtreeviz/blob/master/notebooks/dtreeviz_spark_visualisations.ipynb Prints the major version of the installed PySpark library. This is useful for ensuring compatibility with dtreeviz or other Spark-dependent libraries. ```python spark_version = int(pyspark.__version__.split(".")[0]) print(f"Tested versions are 2 and 3, yours is {spark_version}.") ``` -------------------------------- ### Load Cars Dataset for Regression Source: https://github.com/parrt/dtreeviz/blob/master/notebooks/dtreeviz_sklearn_AI_visualisations.ipynb Loads the Cars dataset from a URL for use in decision tree regression examples. Requires pandas and DecisionTreeRegressor. ```python import pandas as pd from sklearn.tree import DecisionTreeRegressor dataset_url = "https://raw.githubusercontent.com/parrt/dtreeviz/master/data/cars.csv" df_cars = pd.read_csv(dataset_url) X = df_cars.drop('MPG', axis=1) y = df_cars['MPG'] features = list(X.columns) ``` -------------------------------- ### Load Iris Dataset for Classification Source: https://github.com/parrt/dtreeviz/blob/master/notebooks/dtreeviz_sklearn_AI_visualisations.ipynb Loads the Iris dataset from scikit-learn for use in decision tree classification examples. Requires importing DecisionTreeClassifier. ```python from sklearn.datasets import load_iris iris = load_iris() features = list(iris.feature_names) class_names = iris.target_names X = iris.data y = iris.target ``` -------------------------------- ### Get a Specific Data Instance Source: https://github.com/parrt/dtreeviz/blob/master/notebooks/dtreeviz_xgboost_visualisations.ipynb Selects a specific instance (row) from the training dataset for detailed inspection. This instance can then be used to visualize its prediction path. ```python x = dataset[features_reg].iloc[10] x ``` -------------------------------- ### Prepare wine dataset and visualize feature relationship Source: https://github.com/parrt/dtreeviz/blob/master/testing/slides.ipynb Loads the wine dataset and visualizes the relationship between 'Proline' and 'Flavanoid' features. Saves the plot as an SVG. ```python from sklearn.datasets import load_wine fig = plt.figure() ax = fig.gca() wine = load_wine() class_values = [0,1,2] print(wine.feature_names) X = wine.data[:,[12,6]] y = wine.target X_hist = [X[y == cl] for cl in class_values] color_values = color_blind_friendly_colors[3] colors = {v: color_values[i] for i, v in enumerate(class_values)} for i, h in enumerate(X_hist): ax.scatter(h[:,0], h[:,1], alpha=1, marker='o', s=25, c=colors[i], edgecolors='grey', lw=.3) #plt.scatter(X,y,marker='o', alpha=.4, c='#4575b4', edgecolor=GREY, lw=.3) plt.xlabel("Profline", fontsize=14) plt.ylabel("Flavanoid", fontsize=14) plt.tight_layout() plt.savefig("/tmp/wine-prol-flav.svg", bbox_inches=0, pad_inches=0) ``` -------------------------------- ### Get Node Statistics Source: https://github.com/parrt/dtreeviz/blob/master/notebooks/dtreeviz_spark_visualisations.ipynb Retrieves and displays statistics (mean, std, min, max, quartiles) for all features within a specific node (node_id=3) of the regressor tree. ```python viz_rmodel.node_stats(node_id=3) ``` -------------------------------- ### Extract parameters from pipeline for dtreeviz Source: https://github.com/parrt/dtreeviz/blob/master/notebooks/dtreeviz_sklearn_pipeline_visualisations.ipynb Uses the dtreeviz.utils.extract_params_from_pipeline function to get the decision tree classifier, training data, and transformed feature names from a scikit-learn pipeline. ```python from dtreeviz.utils import extract_params_from_pipeline tree_classifier, X_train, features_model = extract_params_from_pipeline( pipeline=model, X_train=dataset[features], feature_names=features) ``` -------------------------------- ### Check Dot Binary Path Source: https://github.com/parrt/dtreeviz/blob/master/README.md Check the path of the 'dot' executable to ensure it points to the Homebrew-installed version. ```bash which dot ``` -------------------------------- ### Get Node Statistics Source: https://github.com/parrt/dtreeviz/blob/master/notebooks/dtreeviz_tensorflow_visualisations.ipynb Retrieves and displays statistical information (count, mean, std, min, max, quartiles) for the instances within a specified node of the regression tree. ```python viz_rmodel.node_stats(node_id=6) ``` -------------------------------- ### Prepare car dataset and visualize MPG vs. Weight Source: https://github.com/parrt/dtreeviz/blob/master/testing/slides.ipynb Loads the car dataset, splits it into training and testing sets, and visualizes the relationship between vehicle weight and MPG. Saves the plot as an SVG. ```python import pandas as pd from sklearn.model_selection import train_test_split df_cars = pd.read_csv("data/cars.csv") X = df_cars[['WGT']] y = df_cars['MPG'] X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2) from matplotlib import rcParams rcParams['font.family'] = 'Arial' plt.scatter(X,y,marker='o', alpha=.4, c='#4575b4', edgecolor='grey', lw=.3) plt.xlabel("Vehicle Weight", fontsize=14) plt.ylabel("MPG", fontsize=14) plt.tight_layout() plt.savefig("/tmp/cars-wgt-vs-mpg.svg", bbox_inches=0, pad_inches=0) ``` -------------------------------- ### Load Iris Dataset and Train Decision Tree Source: https://github.com/parrt/dtreeviz/blob/master/play.ipynb Loads the Iris dataset, preprocesses it, and trains a decision tree classifier. This is a common setup for demonstrating classification algorithms. ```python from dtreeviz.trees import * precision = 1 GREY = '#444443' iris = load_iris() data = pd.DataFrame(iris.data) data.columns = iris.feature_names clf = tree.DecisionTreeClassifier(max_depth=2, random_state=666) clf = clf.fit(data, iris.target) ``` -------------------------------- ### Load and Prepare Wine Dataset Source: https://github.com/parrt/dtreeviz/blob/master/notebooks/classifier-boundary-animations.ipynb Loads the wine dataset from scikit-learn and selects two specific features (feature 12 and 6) for visualization. ```python wine = load_wine() X = wine.data X = X[:,[12,6]] y = wine.target ``` -------------------------------- ### Get Node Statistics with dtreeviz Source: https://github.com/parrt/dtreeviz/blob/master/notebooks/dtreeviz_sklearn_AI_visualisations.ipynb Use the `node_stats` method to retrieve detailed statistics for a specific node in the decision tree. This is useful for understanding the data distribution within that node. ```python viz_model.node_stats(node_id=11) ``` -------------------------------- ### Explain Prediction Path for an Instance Source: https://github.com/parrt/dtreeviz/blob/master/notebooks/dtreeviz_xgboost_visualisations.ipynb Prints a textual explanation of the prediction path for a given data instance. This provides a clear, step-by-step breakdown of the decisions made by the tree. ```python print(viz_rmodel.explain_prediction_path(x)) ``` -------------------------------- ### Get Node Statistics Source: https://github.com/parrt/dtreeviz/blob/master/notebooks/dtreeviz_spark_visualisations.ipynb Retrieve detailed statistics for a specific node in the decision tree, identified by its node ID. This includes statistical summaries of the features for instances reaching that node. ```python viz_model.node_stats(node_id=5) ``` -------------------------------- ### Get Node Statistics for LightGBM Tree Source: https://github.com/parrt/dtreeviz/blob/master/notebooks/dtreeviz_lightgbm_visualisations.ipynb Retrieves and displays statistics (mean, std, min, max, etc.) for instances within a specific node (identified by node_id) of the LightGBM tree. ```python viz_rmodel.node_stats(node_id=19) ``` -------------------------------- ### Initialize dtreeviz Adaptor for Spark Regressor Source: https://github.com/parrt/dtreeviz/blob/master/notebooks/dtreeviz_spark_visualisations.ipynb Prepares the dataset by applying necessary transformations and converting it to a Pandas DataFrame. Extracts the trained DecisionTreeRegressor model from the Spark ML Pipeline. ```python dataset_reg = Pipeline(stages=[sex_label_indexer, embarked_label_indexer, age_imputer]) \ .fit(data) \ .transform(data) \ .toPandas()[features_reg + [target_reg]] tree_model_regressor = model_reg.stages[4] ``` -------------------------------- ### Get Node Statistics Source: https://github.com/parrt/dtreeviz/blob/master/notebooks/dtreeviz_xgboost_visualisations.ipynb Retrieves and displays statistics for a specific node in the XGBoost tree, identified by its node ID. This includes feature values and target statistics for instances that reach that node. ```python viz_rmodel.node_stats(node_id=4) ``` -------------------------------- ### Check Dot Binary Symlink Source: https://github.com/parrt/dtreeviz/blob/master/README.md Inspect the symlink for the 'dot' executable to verify its location and version. ```bash ls -l $(which dot) ``` -------------------------------- ### Import necessary libraries Source: https://github.com/parrt/dtreeviz/blob/master/notebooks/dtreeviz_sklearn_pipeline_visualisations.ipynb Imports essential libraries for data manipulation, machine learning, and dtreeviz visualization. ```python import sklearn import graphviz import pandas as pd import dtreeviz ``` -------------------------------- ### Get a string explanation of the prediction path Source: https://github.com/parrt/dtreeviz/blob/master/notebooks/dtreeviz_sklearn_pipeline_visualisations.ipynb Obtain a textual explanation of the comparisons made as an instance traverses the decision tree. This provides a detailed breakdown of the conditions met at each relevant split. ```python print(viz_model.explain_prediction_path(x)) ``` -------------------------------- ### Import necessary libraries Source: https://github.com/parrt/dtreeviz/blob/master/notebooks/dtreeviz_xgboost_visualisations.ipynb Imports core Python libraries for system interaction and configuration. ```python import sys import os ``` -------------------------------- ### Load and Prepare Breast Cancer Dataset Source: https://github.com/parrt/dtreeviz/blob/master/notebooks/classifier-decision-boundaries.ipynb Loads the breast cancer dataset and creates a pandas DataFrame, assigning numerical column names. ```python cancer = load_breast_cancer() df = pd.DataFrame(data=cancer.data) df.columns = [f'f{i}' for i in range(df.shape[1])] df['y'] = cancer.target ``` -------------------------------- ### Get instance feature importance Source: https://github.com/parrt/dtreeviz/blob/master/notebooks/dtreeviz_sklearn_pipeline_visualisations.ipynb Calculate and visualize the feature importance for a specific instance, as determined by the underlying decision tree library. This helps in understanding which features were most influential for that instance's prediction. ```python viz_model.instance_feature_importance(x) ``` -------------------------------- ### Create dtreeviz Model Object Source: https://github.com/parrt/dtreeviz/blob/master/notebooks/dtreeviz_sklearn_visualisations.ipynb Initializes a dtreeviz model object for visualization, passing the trained regressor, features, target, and feature names. ```python viz_rmodel = dtreeviz.model(dtr_cars, X, y, feature_names=features, target_name='MPG') ``` -------------------------------- ### Visualize Decision Boundaries with Custom Scatter Alpha Source: https://github.com/parrt/dtreeviz/blob/master/notebooks/classifier-decision-boundaries.ipynb Trains a RandomForestClassifier on the smiley face dataset using only the 'x2' feature and visualizes the decision boundaries. This example customizes the transparency of the scatter plot markers. ```python df = smiley(n=200) x = df[['x2']].values y = df['class'].astype('int').values rf = RandomForestClassifier(n_estimators=10, min_samples_leaf=10, n_jobs=-1) rf.fit(x, y) decision_boundaries(rf,x,y, feature_names=['x2'], target_name = 'smiley', colors={'scatter_marker_alpha':.2}, figsize=(5,1.5)) ``` -------------------------------- ### Load Iris dataset and train Decision Tree Classifier Source: https://github.com/parrt/dtreeviz/blob/master/notebooks/dtreeviz_sklearn_visualisations.ipynb Load the Iris dataset and train a shallow Decision Tree Classifier for classification tasks. This setup is used for demonstrating feature space partitioning. ```python from sklearn.datasets import load_iris iris = load_iris() features = list(iris.feature_names) class_names = iris.target_names X = iris.data y = iris.target ``` ```python dtc_iris = DecisionTreeClassifier(max_depth=2, min_samples_leaf=1, random_state=666) dtc_iris.fit(X, y) ``` -------------------------------- ### Initialize dtreeviz Model for Classification Source: https://github.com/parrt/dtreeviz/blob/master/notebooks/dtreeviz_sklearn_AI_visualisations.ipynb Initializes the dtreeviz model object for visualization with classification data. Requires importing dtreeviz. ```python import dtreeviz viz_model = dtreeviz.model(dt, X_train=X, y_train=y, feature_names=features, target_name='iris', class_names=class_names) ``` -------------------------------- ### Explain Prediction Path with DTreeVizAPI Source: https://context7.com/parrt/dtreeviz/llms.txt Generates a human-readable string describing the path taken for a specific instance's prediction. Useful for model-driven explanation text. Requires dtreeviz, sklearn, and numpy. ```python from sklearn.datasets import load_breast_cancer from sklearn.tree import DecisionTreeClassifier import dtreeviz, numpy as np bc = load_breast_cancer() clf = DecisionTreeClassifier(max_depth=4).fit(bc.data, bc.target) viz = dtreeviz.model(clf, X_train=bc.data, y_train=bc.target, feature_names=list(bc.feature_names), target_name="diagnosis", class_names=["malignant", "benign"]) sample = bc.data[10] explanation = viz.explain_prediction_path(sample) print(explanation) ``` -------------------------------- ### Select an instance for prediction path analysis Source: https://github.com/parrt/dtreeviz/blob/master/notebooks/dtreeviz_sklearn_pipeline_visualisations.ipynb Select a specific instance from the training data to analyze its prediction path through the decision tree. This is the first step in understanding how the model makes a decision for a given data point. ```python x = X_train.iloc[10] x ``` -------------------------------- ### Create Scatter Plot for Cars Dataset Source: https://github.com/parrt/dtreeviz/blob/master/testing/playground.ipynb Generates a scatter plot of vehicle weight vs. MPG using matplotlib and pandas. Saves the plot as an SVG file. Ensure 'data/cars.csv' is accessible and matplotlib is installed. ```python import pandas as pd from sklearn.model_selection import train_test_split df_cars = pd.read_csv("data/cars.csv") X = df_cars[['WGT']] y = df_cars['MPG'] X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2) from matplotlib import rcParams rcParams['font.family'] = 'Arial' plt.scatter(X,y,marker='o', alpha=.4, c='#4575b4', edgecolor=GREY, lw=.3) plt.xlabel("Vehicle Weight", fontsize=14) plt.ylabel("MPG", fontsize=14) plt.tight_layout() plt.savefig("/tmp/cars-wgt-vs-mpg.svg", bbox_inches=0, pad_inches=0) ``` -------------------------------- ### Create and Train a Regression Model Source: https://github.com/parrt/dtreeviz/blob/master/notebooks/dtreeviz_tensorflow_visualisations.ipynb Sets up features and target for regression, converts a pandas DataFrame to a TensorFlow dataset, and trains a Random Forest model for regression. ```python features_reg = ["Pclass", "Fare", "Sex_label", "Cabin_label", "Embarked_label", "Survived"] target_reg = "Age" dataset_rtf = tf.keras.pd_dataframe_to_tf_dataset(dataset[features_reg + [target_reg]], label = target_reg, task=Task.REGRESSION) model_reg = tf.keras.RandomForestModel(num_trees=4, max_depth=4, random_seed = random_state, bootstrap_training_dataset=False, sampling_with_replacement=False, task=Task.REGRESSION) model_reg.fit(dataset_rtf) ```