Why scikit-learn isnt the Best for Visualizing Decision Trees: Meet dtreeviz
Source: tai_blog

Why scikit-learn Isnt the Best for Visualizing Decision Trees: Meet dtreevizDecision Trees  also known as CART (Classification and Regression Trees)  are undoubtedly one of the most intuitive algorithms in the machine learning space  thanks to their simplicity. Unlike neural networks or SVMs  where you have to invest considerable time to understand the underlying processes  decision trees are essentially a series of if-else statements stacked together to guide you toward a possible outcome. Sure  theres some math involved in determining those conditions  but its not too overwhelming.   Its Easy  ButYes  decision trees are straightforward  but theres a catch. The problem doesnt lie with the algorithm itself  but with the tools often used to visualize it  specifically  scikit-learn. The visualizations produced by scikit-learn can turn you off from decision trees altogether. Why am I saying this? Well  lets dive into an example so you can see for yourself.   Visualizing with scikit-learnTo showcase the limitations  well use the famous Penguin dataset  which is readily available in seaborn.   Getting StartedFirst things first  we need to import the necessary libraries to get everything rolling. Lets get the formalities out of the way:   Here  you can see that weve imported all the required libraries. Now  lets load the dataset  perform label encoding  and begin training.   Now that the model is trained  its time to visualize the decision tree using scikit-learn. Brace yourself for disappointment:   Take a look at this image. Does it make any sense at first glance? Probably not. It feels like staring at a friend whos trying to say something but just cant get the words out  theres an awkward pause  and youre left confused.   So  Whats the Alternative?How can you fall back in love with decision trees? Simple! By using the dtreeviz library. Weve spent enough time with scikit-learns confusing output  lets move on to something better.   Get Ready with the PrerequisitesFirst  lets install the dtreeviz library using the ever-reliable pip command:   pip install dtreevizOnce installed  import the necessary packages:   Now that weve got everything set up  lets jump right into the fun part!   The Fun Begins with dtreevizAlright  now its time to see dtreeviz in action:   And boom! The visualization is so simple  even penguins could understand it (just kidding  please dont try to teach real penguins decision trees!).   Still  lets break down this visualization to ensure we understand it better than penguins do. Youll notice there are histograms and pie charts. The topmost histogram shows the root node and how the decision was made. Based on whether the flipper length is above or below a certain threshold  the tree branches into the next set of nodes: bill length and bill depth. These nodes further split based on comparisons  resulting in three histograms. The leaf nodes are represented by pie charts  classifying the penguins into Adlie  Chinstrap  and Gentoo species.   Now That Youve Seen the Basics  Lets Customize ItNot a fan of histograms? No problem! Lets remove them by setting the fancy parameter of viz_model to False. Its as simple as that:   And lets say you want to see which path was followed during a prediction. Lets tweak the code a bit by adding datapoint to the argument for which you want to see the prediction path.   Now  run the updated code:   As you can see  the orange line highlights the path that was followed all the way to the leaf node  making it crystal clear how the decision was made. We can even tweak more parameters to understand exactly why the algorithm classified the penguin as Adlie.   Lets start with the data used for this prediction. Look at the data instance below; it contains all the feature values from the 10th row. Well use this to see which features influenced the decision:   You can use the datapoint as an argument in the explain_prediction_path method  like so:   Output for the above code shows which nodes were involved. But just knowing the nodes isnt satisfying enough  right? You probably also want to know how much those nodes influenced the prediction. The method below perfectly illustrates the importance of each feature:   As expected  flipper length and bill length were the key features that determined the penguins classification as Adlie.   Enough with Penguins  Lets Tackle a Regression ProblemWeve spent enough time with the penguins (no animals were harmed in the making of this blog  by the way). Now  lets move on to a regression problem to see how dtreeviz handles it.   For this example  well use a simple dataset I created about students study hours.   dataset = pd.read_csv('studyhours.csv') features_reg = [Hours_Studied] target_reg = Marks tree_regressor = DecisionTreeRegressor(max_depth=3  random_state=2  criterion=absolute_error) tree_regressor.fit(dataset[features_reg].values  dataset[target_reg].values)After training the data  lets visualize the decision tree using our trusty dtreeviz:   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) viz_rmodel.view()To change the orientation of the tree  lets add orientation=LR as a parameter:   viz_rmodel.view(orientation=LR)Now lets use the datapoint below to see how the decision is made.   viz_rmodel.view(x = dataset[features_reg].iloc[10])See how intuitive that is? You can easily understand the relationships and decisions made by the model.   Wrapping It UpIn this blog  we explored why scikit-learn might not be the best choice for visualizing decision trees and how dtreeviz offers a much more user-friendly alternative. We walked through visualizing both a classification and a regression problem  demonstrating how dtreeviz can make your machine learning models not only easier to interpret but also more enjoyable to work with.   So  whats next? Why not try visualizing some other datasets  like the Iris or Wine datasets  and share your findings? Drop your Kaggle links in the comments below. Until next time  happy visualizing!