Visualize an ensemble classifier

This example illustrates how to quickly visualize an imbalanced_ensemble.ensemble classifier with the imbalanced_ensemble.visualizer module.

This example uses:

# Authors: Zhining Liu <zhining.liu@outlook.com>
# License: MIT
print(__doc__)

# Import imbalanced_ensemble
import imbalanced_ensemble as imbens

# Import utilities
import sklearn
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split

RANDOM_STATE = 42

# sphinx_gallery_thumbnail_number = 2

Prepare data

Make a toy 3-class imbalanced classification task.

# make dataset
X, y = make_classification(n_classes=3, class_sep=2,
    weights=[0.1, 0.3, 0.6], n_informative=3, n_redundant=1, flip_y=0,
    n_features=20, n_clusters_per_class=2, n_samples=2000, random_state=0)

# train valid split
X_train, X_valid, y_train, y_valid = train_test_split(
    X, y, test_size=0.5, stratify=y, random_state=RANDOM_STATE)

Train an ensemble classifier

Take SelfPacedEnsembleClassifier as example

# Initialize and train an SPE classifier
clf = imbens.ensemble.SelfPacedEnsembleClassifier(
    random_state=RANDOM_STATE
).fit(X_train, y_train)

# Store the fitted SelfPacedEnsembleClassifier
fitted_ensembles = {'SPE': clf}

Fit an ImbalancedEnsembleVisualizer

# Initialize visualizer
visualizer = imbens.visualizer.ImbalancedEnsembleVisualizer(
    eval_datasets = {
        'training' : (X_train, y_train),
        'validation' : (X_valid, y_valid),
    },
)

# Fit visualizer
visualizer.fit(fitted_ensembles)

Out:

  0%|                                                                                           | 0/50 [00:00<?, ?it/s]
Visualizer evaluating model SPE on dataset  training  ::   0%|                                  | 0/50 [00:00<?, ?it/s]
Visualizer evaluating model SPE on dataset  training  :: 100%|#######################| 50/50 [00:00<00:00, 1365.60it/s]

  0%|                                                                                           | 0/50 [00:00<?, ?it/s]
Visualizer evaluating model SPE on dataset validation ::   0%|                                  | 0/50 [00:00<?, ?it/s]
Visualizer evaluating model SPE on dataset validation :: 100%|#######################| 50/50 [00:00<00:00, 1352.20it/s]
Visualizer computing confusion matrices.. Finished!

<imbalanced_ensemble.visualizer.visualizer.ImbalancedEnsembleVisualizer object at 0x000001E199168EE0>

Plot performance curve

performance w.r.t. number of base estimators

fig, axes = visualizer.performance_lineplot()
Performance Curves

Plot confusion matrix

fig, axes = visualizer.confusion_matrix_heatmap(
    on_datasets=['validation'], # only on validation set
    sup_title=False,
)
plot basic visualize

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

Estimated memory usage: 23 MB

Gallery generated by Sphinx-Gallery