Plot confusion matrix sweet#

plots a confusion matrix using the ‘yellowbrick’ package.

# Author: L.Kouadio
# Licence: BSD-3-clause
Import the required models and fetch an extreme gradient boosting

for instance then plot the confusion metric

import matplotlib.pyplot as plt
plt.style.use ('classic')
from watex.datasets import fetch_data
from watex.exlib.sklearn import train_test_split
from watex.models import pModels
from watex.utils.plotutils import plot_yb_confusion_matrix
# split the  data . Note that fetch_data output X and y
X, Xt, y, yt  = train_test_split (* fetch_data ('bagoue analysed'), test_size =.25  )
# train the model with the best estimator
pmo = pModels (model ='xgboost' )
pmo.fit(X, y )
print(pmo.estimator_ ) # pmo.XGB.best_estimator_
XGBClassifier(base_score=None, booster='gbtree', callbacks=None,
              colsample_bylevel=None, colsample_bynode=None,
              colsample_bytree=None, device=None, early_stopping_rounds=None,
              enable_categorical=False, eval_metric=None, feature_types=None,
              gamma=1.5, grow_policy=None, importance_type=None,
              interaction_constraints=None, learning_rate=0.07, max_bin=None,
              max_cat_threshold=None, max_cat_to_onehot=None,
              max_delta_step=None, max_depth=2, max_leaves=None,
              min_child_weight=None, missing=nan, monotone_constraints=None,
              multi_strategy=None, n_estimators=300, n_jobs=None,
              num_parallel_tree=None, objective='multi:softprob', ...)

Predict the score using under the hood the best estimator for adaboost classifier

ypred = pmo.predict(Xt)

# now plot the score
plot_yb_confusion_matrix (pmo.XGB.best_estimator_, Xt, yt  )
XGBClassifier Confusion Matrix
/home/docs/checkouts/readthedocs.org/user_builds/watex/envs/0.3.3/lib/python3.10/site-packages/yellowbrick/classifier/base.py:232: YellowbrickWarning:

could not determine class_counts_ from previously fitted classifier
ConfusionMatrix(ax=<AxesSubplot:title={'center':'XGBClassifier Confusion Matrix'}, xlabel='Predicted Class', ylabel='True Class'>,
                cmap=<matplotlib.colors.ListedColormap object at 0x7f0b176e87f0>,
                estimator=XGBClassifier(base_score=None, booster='gbtree',
                                        callbacks=None, colsample_bylevel=None,
                                        colsample_bynode=None,
                                        colsample_bytree=None, device=None,
                                        early_stoppin...
                                        gamma=1.5, grow_policy=None,
                                        importance_type=None,
                                        interaction_constraints=None,
                                        learning_rate=0.07, max_bin=None,
                                        max_cat_threshold=None,
                                        max_cat_to_onehot=None,
                                        max_delta_step=None, max_depth=2,
                                        max_leaves=None, min_child_weight=None,
                                        missing=nan, monotone_constraints=None,
                                        multi_strategy=None, n_estimators=300,
                                        n_jobs=None, num_parallel_tree=None,
                                        objective='multi:softprob', ...))
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.


Total running time of the script: (0 minutes 0.298 seconds)

Gallery generated by Sphinx-Gallery