Recipe: How to visualise XGBoost tree in Python?
MACHINE LEARNING RECIPES

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()
    print(format('Hoe to visualise XGBoost tree in Python','*^82'))
    import warnings
    warnings.filterwarnings("ignore")

    # 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

    plt.style.use('ggplot')

    # load the iris datasets
    dataset = datasets.load_wine()
    X = dataset.data; y = dataset.target
    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()
    model.fit(X_train, 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,
                   target_names=dataset.target_names))
    print(); print(metrics.confusion_matrix(expected_y, predicted_y))

    plot_tree(model); plt.show()
    plot_tree(model, num_trees=4); plt.show()
    plot_tree(model, num_trees=0, rankdir='LR'); plt.show()
Snippet_186()


Stuck at work?
Can't find the recipe you are looking for. Let us know and we will find an expert to create the recipe for you. Click here
Companies using this Recipe