Last Updated on August 21, 2020

Imbalanced classification are those prediction tasks where the distribution of examples across class labels is not equal.

Most imbalanced classification examples focus on binary classification tasks, yet many of the tools and techniques for imbalanced classification also directly support multi-class classification problems.

In this tutorial, you will discover how to use the tools of imbalanced classification with a multi-class dataset.

After completing this tutorial, you will know:

  • About the glass identification standard imbalanced multi-class prediction problem.
  • How to use SMOTE oversampling for imbalanced multi-class classification.
  • How to use cost-sensitive learning for imbalanced multi-class classification.

Kick-start your project with my new book Imbalanced Classification with Python, including step-by-step tutorials and the Python source code files for all examples.

Let’s get started.

Multi-Class Imbalanced Classification

Multi-Class Imbalanced Classification
Photo by istolethetv, some rights reserved.

Tutorial Overview

This tutorial is divided into three parts; they are:

  • Glass Multi-Class Classification Dataset
  • SMOTE Oversampling for Multi-Class Classification
  • Cost-Sensitive Learning for Multi-Class Classification
  • Glass Multi-Class Classification Dataset

    In this tutorial, we will focus on the standard imbalanced multi-class classification problem referred to as “Glass Identification” or simply “glass.”

    The dataset describes the chemical properties of glass and involves classifying samples of glass using their chemical properties as one of six classes. The dataset was credited to Vina Spiehler in 1987.

    Ignoring the sample identification number, there are nine input variables that summarize the properties of the glass dataset; they are:

    • RI: Refractive Index
    • Na: Sodium
    • Mg: Magnesium
    • Al: Aluminum
    • Si: Silicon
    • K: Potassium
    • Ca: Calcium
    • Ba: Barium
    • Fe: Iron

    The chemical compositions are measured as the weight percent in corresponding oxide.

    There are seven types of glass listed; they are:

    • Class 1: building windows (float processed)
    • Class 2: building windows (non-float processed)
    • Class 3: vehicle windows (float processed)
    • Class 4: vehicle windows (non-float processed)
    • Class 5: containers
    • Class 6: tableware
    • Class 7: headlamps

    Float glass refers to the process used to make the glass.

    There are 214 observations in the dataset and the number of observations in each class is imbalanced. Note that there are no examples for class 4 (non-float processed vehicle windows) in the dataset.

    • Class 1: 70 examples
    • Class 2: 76 examples
    • Class 3: 17 examples
    • Class 4: 0 examples
    • Class 5: 13 examples
    • Class 6: 9 examples
    • Class 7: 29 examples

    Although there are minority classes, all classes are equally important in this prediction problem.

    The dataset can be divided into window glass (classes 1-4) and non-window glass (classes 5-7). There are 163 examples of window glass and 51 examples of non-window glass.

    • Window Glass: 163 examples
    • Non-Window Glass: 51 examples

    Another division of the observations would be between float processed glass and non-float processed glass, in the case of window glass only. This division is more balanced.

    • Float Glass: 87 examples
    • Non-Float Glass: 76 examples

    You can learn more about the dataset here:

    No need to download the dataset; we will download it automatically as part of the worked examples.

    Below is a sample of the first few rows of the data.


    We can see that all inputs are numeric and the target variable in the final column is the integer encoded class label.

    You can learn more about how to work through this dataset as part of a project in the tutorial:

    Now that we are familiar with the glass multi-class classification dataset, let’s explore how we can use standard imbalanced classification tools with it.



    Want to Get Started With Imbalance Classification?

    Take my free 7-day email crash course now (with sample code).

    Click to sign-up and also get a free PDF Ebook version of the course.

    Download Your FREE Mini-Course


    SMOTE Oversampling for Multi-Class Classification

    Oversampling refers to copying or synthesizing new examples of the minority classes so that the number of examples in the minority class better resembles or matches the number of examples in the majority classes.

    Perhaps the most widely used approach to synthesizing new examples is called the Synthetic Minority Oversampling TEchnique, or SMOTE for short. This technique was described by Nitesh Chawla, et al. in their 2002 paper named for the technique titled “SMOTE: Synthetic Minority Over-sampling Technique.”

    You can learn more about SMOTE in the tutorial:

    The imbalanced-learn library provides an implementation of SMOTE that we can use that is compatible with the popular scikit-learn library.

    First, the library must be installed. We can install it using pip as follows:

    sudo pip install imbalanced-learn

    We can confirm that the installation was successful by printing the version of the installed library:


    Running the example will print the version number of the installed library; for example:


    Before we apply SMOTE, let’s first load the dataset and confirm the number of examples in each class.


    Running the example first downloads the dataset and splits it into train and test sets.

    The number of rows in each class is then reported, confirming that some classes, such as 0 and 1, have many more examples (more than 70) than other classes, such as 3 and 4 (less than 15).


    A bar chart is created providing a visualization of the class breakdown of the dataset.

    This gives a clearer idea that classes 0 and 1 have many more examples than classes 2, 3, 4 and 5.

    Histogram of Examples in Each Class in the Glass Multi-Class Classification Dataset

    Histogram of Examples in Each Class in the Glass Multi-Class Classification Dataset

    Next, we can apply SMOTE to oversample the dataset.

    By default, SMOTE will oversample all classes to have the same number of examples as the class with the most examples.

    In this case, class 1 has the most examples with 76, therefore, SMOTE will oversample all classes to have 76 examples.

    The complete example of oversampling the glass dataset with SMOTE is listed below.


    Running the example first loads the dataset and applies SMOTE to it.

    The distribution of examples in each class is then reported, confirming that each class now has 76 examples, as we expected.


    A bar chart of the class distribution is also created, providing a strong visual indication that all classes now have the same number of examples.

    Histogram of Examples in Each Class in the Glass Multi-Class Classification Dataset After Default SMOTE Oversampling

    Histogram of Examples in Each Class in the Glass Multi-Class Classification Dataset After Default SMOTE Oversampling

    Instead of using the default strategy of SMOTE to oversample all classes to the number of examples in the majority class, we could instead specify the number of examples to oversample in each class.

    For example, we could oversample to 100 examples in classes 0 and 1 and 200 examples in remaining classes. This can be achieved by creating a dictionary that maps class labels to the number of desired examples in each class, then specifying this via the “sampling_strategy” argument to the SMOTE class.


    Tying this together, the complete example of using a custom oversampling strategy for SMOTE is listed below.


    Running the example creates the desired sampling and summarizes the effect on the dataset, confirming the intended result.


    Note: you may see warnings that can be safely ignored for the purposes of this example, such as:


    A bar chart of the class distribution is also created confirming the specified class distribution after data sampling.

    Histogram of Examples in Each Class in the Glass Multi-Class Classification Dataset After Custom SMOTE Oversampling

    Histogram of Examples in Each Class in the Glass Multi-Class Classification Dataset After Custom SMOTE Oversampling

    Note: when using data sampling like SMOTE, it must only be applied to the training dataset, not the entire dataset. I recommend using a Pipeline to ensure that the SMOTE method is correctly used when evaluating models and making predictions with models.

    You can see an example of the correct usage of SMOTE in a Pipeline in this tutorial:

    Cost-Sensitive Learning for Multi-Class Classification

    Most machine learning algorithms assume that all classes have an equal number of examples.

    This is not the case in multi-class imbalanced classification. Algorithms can be modified to change the way learning is performed to bias towards those classes that have fewer examples in the training dataset. This is generally called cost-sensitive learning.

    For more on cost-sensitive learning, see the tutorial:

    The RandomForestClassifier class in scikit-learn supports cost-sensitive learning via the “class_weight” argument.

    By default, the random forest class assigns equal weight to each class.

    We can evaluate the classification accuracy of the default random forest class weighting on the glass imbalanced multi-class classification dataset.

    The complete example is listed below.


    Running the example evaluates the default random forest algorithm with 1,000 trees on the glass dataset using repeated stratified k-fold cross-validation.

    The mean and standard deviation classification accuracy are reported at the end of the run.

    Note: Your results may vary given the stochastic nature of the algorithm or evaluation procedure, or differences in numerical precision. Consider running the example a few times and compare the average outcome.

    In this case, we can see that the default model achieved a classification accuracy of about 79.6 percent.


    We can specify the “class_weight” argument to the value “balanced” that will automatically calculates a class weighting that will ensure each class gets an equal weighting during the training of the model.


    Tying this together, the complete example is listed below.


    Running the example reports the mean and standard deviation classification accuracy of the cost-sensitive version of random forest on the glass dataset.

    Note: Your results may vary given the stochastic nature of the algorithm or evaluation procedure, or differences in numerical precision. Consider running the example a few times and compare the average outcome.

    In this case, we can see that the default model achieved a lift in classification accuracy over the cost-insensitive version of the algorithm, with 80.2 percent classification accuracy vs. 79.6 percent.


    The “class_weight” argument takes a dictionary of class labels mapped to a class weighting value.

    We can use this to specify a custom weighting, such as a default weighting for classes 0 and 1.0 that have many examples and a double class weighting of 2.0 for the other classes.


    Tying this together, the complete example of using a custom class weighting for cost-sensitive learning on the glass multi-class imbalanced classification problem is listed below.


    Running the example reports the mean and standard deviation classification accuracy of the cost-sensitive version of random forest on the glass dataset with custom weights.

    Note: Your results may vary given the stochastic nature of the algorithm or evaluation procedure, or differences in numerical precision. Consider running the example a few times and compare the average outcome.

    In this case, we can see that we achieved a further lift in accuracy from about 80.2 percent with balanced class weighting to 80.8 percent with a more biased class weighting.


    Further Reading

    This section provides more resources on the topic if you are looking to go deeper.

    Related Tutorials
    APIs

    Summary

    In this tutorial, you discovered how to use the tools of imbalanced classification with a multi-class dataset.

    Specifically, you learned:

    • About the glass identification standard imbalanced multi-class prediction problem.
    • How to use SMOTE oversampling for imbalanced multi-class classification.
    • How to use cost-sensitive learning for imbalanced multi-class classification.

    Do you have any questions?
    Ask your questions in the comments below and I will do my best to answer.

    Get a Handle on Imbalanced Classification!

    Imbalanced Classification with Python

    Develop Imbalanced Learning Models in Minutes

    …with just a few lines of python code

    Discover how in my new Ebook:
    Imbalanced Classification with Python

    It provides self-study tutorials and end-to-end projects on:
    Performance Metrics, Undersampling Methods, SMOTE, Threshold Moving, Probability Calibration, Cost-Sensitive Algorithms

    and much more…

    Bring Imbalanced Classification Methods to Your Machine Learning Projects

    See What’s Inside

    Covid Abruzzo Basilicata Calabria Campania Emilia Romagna Friuli Venezia Giulia Lazio Liguria Lombardia Marche Molise Piemonte Puglia Sardegna Sicilia Toscana Trentino Alto Adige Umbria Valle d’Aosta Veneto Italia Agrigento Alessandria Ancona Aosta Arezzo Ascoli Piceno Asti Avellino Bari Barletta-Andria-Trani Belluno Benevento Bergamo Biella Bologna Bolzano Brescia Brindisi Cagliari Caltanissetta Campobasso Carbonia-Iglesias Caserta Catania Catanzaro Chieti Como Cosenza Cremona Crotone Cuneo Enna Fermo Ferrara Firenze Foggia Forlì-Cesena Frosinone Genova Gorizia Grosseto Imperia Isernia La Spezia L’Aquila Latina Lecce Lecco Livorno Lodi Lucca Macerata Mantova Massa-Carrara Matera Messina Milano Modena Monza e della Brianza Napoli Novara Nuoro Olbia-Tempio Oristano Padova Palermo Parma Pavia Perugia Pesaro e Urbino Pescara Piacenza Pisa Pistoia Pordenone Potenza Prato Ragusa Ravenna Reggio Calabria Reggio Emilia Rieti Rimini Roma Rovigo Salerno Medio Campidano Sassari Savona Siena Siracusa Sondrio Taranto Teramo Terni Torino Ogliastra Trapani Trento Treviso Trieste Udine Varese Venezia Verbano-Cusio-Ossola Vercelli Verona Vibo Valentia Vicenza Viterbo