How to visualise XGBoost tree in Python?

How to visualise XGBoost tree in Python?

This recipe helps you visualise XGBoost tree in Python

In [ ]:
## How to visualise XGBoost tree in Python
## DataSet: skleran.datasets.load_breast_cancer()
def Snippet_186():
    print(format('Hoe to visualise XGBoost tree in Python','*^82'))
    import warnings

    # load libraries
    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')

    # load the iris datasets
    dataset = datasets.load_wine()
    X =; y =
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25)

    # fit a ensemble.AdaBoostClassifier() model to the data
    model = XGBClassifier(), y_train)
    print(); print(model)
    # make predictions
    expected_y  = y_test
    predicted_y = model.predict(X_test)
    # summarize the fit of the model
    print(); print('XGBClassifier: ')
    print(); print(metrics.classification_report(expected_y, predicted_y,
    print(); print(metrics.confusion_matrix(expected_y, predicted_y))

    plot_tree(model, num_trees=4);
    plot_tree(model, num_trees=0, rankdir='LR');