How to visualise XGBoost tree in Python?

How to visualise XGBoost tree in Python?

How to visualise XGBoost tree in Python?

This recipe helps you visualise XGBoost tree in Python


Recipe Objective

Have you ever tried to plot XGBoost tree in python and visualise it in the form of tree. So here, In this recipe we will be training XGBoost Classifier, predicting the output and plot the graph.

So this is the recipe on how we visualise XGBoost tree in Python

Step 1 - Import the library

from sklearn import datasets from sklearn import metrics from xgboost import XGBClassifier, plot_tree from sklearn.model_selection import train_test_split import matplotlib.pyplot as plt'ggplot')

We have imported all the modules that would be needed like metrics, datasets, XGBClassifier , plot_tree etc. We will see the use of each modules step by step further.

Step 2 - Setting up the Data for Classifier

We have imported inbuilt breast_cancer dataset from the module datasets and stored the data in X and the target in y. We have also used train_test_split to split the dataset into two parts such that 30% of data is in test and rest in train. dataset = datasets.load_breast_cancer() X =; y = X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.30)

Step 3 - Training XGBClassifier and Predicting the output

We have made an object for the model and fitted the train data. Then we have used the test data to test the model by predicting the output from the model for test data. model_XGB = XGBClassifier(), y_train) print(model_XGB) expected_y = y_test predicted_y = model_XGB.predict(X_test)

Step 4 - Calculating the Scores

Now we are calcutaing other scores for the model using classification_report and confusion matrix by passing expected and predicted values of target of test set. print(metrics.classification_report(expected_y, predicted_y, target_names=dataset.target_names)) print(metrics.confusion_matrix(expected_y, predicted_y))

Step 5 - Ploting the tree

We are ploting the tree for XGBClassifier by passing the required parameters from plot_tree. plot_tree(model_XGB); plot_tree(model_XGB, num_trees=4); plot_tree(model_XGB, num_trees=0, rankdir='LR'); So the final output comes as:

XGBClassifier(base_score=0.5, booster='gbtree', colsample_bylevel=1,
       colsample_bynode=1, colsample_bytree=1, gamma=0, learning_rate=0.1,
       max_delta_step=0, max_depth=3, min_child_weight=1, missing=None,
       n_estimators=100, n_jobs=1, nthread=None,
       objective='binary:logistic', random_state=0, reg_alpha=0,
       reg_lambda=1, scale_pos_weight=1, seed=None, silent=None,
       subsample=1, verbosity=1)


              precision    recall  f1-score   support

   malignant       0.99      0.94      0.96        70
      benign       0.96      0.99      0.98       101

   micro avg       0.97      0.97      0.97       171
   macro avg       0.97      0.97      0.97       171
weighted avg       0.97      0.97      0.97       171

[[ 66   4]
 [  1 100]]

Relevant Projects

Data Science Project - Instacart Market Basket Analysis
Data Science Project - Build a recommendation engine which will predict the products to be purchased by an Instacart consumer again.

Natural language processing Chatbot application using NLTK for text classification
In this NLP AI application, we build the core conversational engine for a chatbot. We use the popular NLTK text classification library to achieve this.

Mercari Price Suggestion Challenge Data Science Project
Data Science Project in Python- Build a machine learning algorithm that automatically suggests the right product prices.

Zillow’s Home Value Prediction (Zestimate)
Data Science Project in R -Build a machine learning algorithm to predict the future sale prices of homes.

Build an Image Classifier for Plant Species Identification
In this machine learning project, we will use binary leaf images and extracted features, including shape, margin, and texture to accurately identify plant species using different benchmark classification techniques.

Identifying Product Bundles from Sales Data Using R Language
In this data science project in R, we are going to talk about subjective segmentation which is a clustering technique to find out product bundles in sales data.

Walmart Sales Forecasting Data Science Project
Data Science Project in R-Predict the sales for each department using historical markdown data from the Walmart dataset containing data of 45 Walmart stores.

Predict Churn for a Telecom company using Logistic Regression
Machine Learning Project in R- Predict the customer churn of telecom sector and find out the key drivers that lead to churn. Learn how the logistic regression model using R can be used to identify the customer churn in telecom dataset.

Customer Market Basket Analysis using Apriori and Fpgrowth algorithms
In this data science project, you will learn how to perform market basket analysis with the application of Apriori and FP growth algorithms based on the concept of association rule learning.

Data Science Project-TalkingData AdTracking Fraud Detection
Machine Learning Project in R-Detect fraudulent click traffic for mobile app ads using R data science programming language.