Multiclass Classification Tutorial
Installing PyCaret
The first step to get started with PyCaret is to install pycaret. Installation is easy and will only take a few minutes. Follow the instructions below:
Installing PyCaret in Local Jupyter Notebook
pip install pycaret
Installing PyCaret on Google Colab or Azure Notebooks
!pip install pycaret
Pre-Requisites
- Python 3.6 or greater
- PyCaret 2.0 or greater
- Internet connection to load data from pycaret's repository
- Basic Knowledge of Multiclass Classification
What is Multiclass Classification?
Multiclass classification is a supervised machine learning technique where the goal is to classify instances into one of three or more classes. (Classifying instances into one of two classes is called Binary Classification). Multiclass classification should not be confused with multi-label classification, where multiple labels are to be predicted for each instance.
Overview of the Classification Module in PyCaret
PyCaret's classification module (pycaret.classification) is a supervised machine learning module which is used for classifying the elements into binary or multinomial groups based on various techniques and algorithms.
The PyCaret classification module can be used for Binary or Multi-class classification problems. It has over 18 algorithms and 14 plots to analyze the performance of models. Be it hyper-parameter tuning, ensembling or advanced techniques like stacking, PyCaret's classification module has it all.
Dataset for the Tutorial
For this tutorial we will use the Iris Dataset from UCI. This is perhaps the best known database to be found in the pattern recognition literature. The data set contains 3 classes of 50 instances each, where each class refers to a type of iris plant. Short descriptions of each column are as follows:
- sepal_length: Length of Sepal
- sepal_width: Width of Sepal
- petal_length: Length of Petal
- petal_width: Width of Petal
- species: One of three class (Setosa, Versicolour, Virginica)
Target Column
Dataset Acknowledgement:
Creator: R.A. Fisher, Donor : Michael Marshall (MARSHALL%PLU '@' io.arc.nasa.gov)
The original dataset and data dictionary can be found here.
Getting the Data
You can download the data from the original source found here and load it using pandas (Learn How) or you can use PyCaret's data respository to load the data using the get_data() function (This will require an internet connection).
from pycaret.datasets import get_data dataset = get_data('iris')
| sepal_length | sepal_width | petal_length | petal_width | species | |
|---|---|---|---|---|---|
| 0 | 5.1 | 3.5 | 1.4 | 0.2 | Iris-setosa |
| 1 | 4.9 | 3.0 | 1.4 | 0.2 | Iris-setosa |
| 2 | 4.7 | 3.2 | 1.3 | 0.2 | Iris-setosa |
| 3 | 4.6 | 3.1 | 1.5 | 0.2 | Iris-setosa |
| 4 | 5.0 | 3.6 | 1.4 | 0.2 | Iris-setosa |
Setting up the Environment in PyCaret
from pycaret.classification import * exp_mclf101 = setup(data = data, target = 'species', session_id=123)
| Description | Value | |
|---|---|---|
| 0 | session_id | 123 |
| 1 | Target Type | Multiclass |
| 2 | Label Encoded | Iris-setosa: 0, Iris-versicolor: 1, Iris-virginica: 2 |
| 3 | Original Data | (135, 5) |
| 4 | Missing Values | False |
| 5 | Numeric Features | 4 |
| 6 | Categorical Features | 0 |
| 7 | Ordinal Features | False |
| 8 | High Cardinality Features | False |
| 9 | High Cardinality Method | None |
| 10 | Sampled Data | (135, 5) |
| 11 | Transformed Train Set | (94, 4) |
| 12 | Transformed Test Set | (41, 4) |
| 13 | Numeric Imputer | mean |
| 14 | Categorical Imputer | constant |
| 15 | Normalize | False |
| 16 | Normalize Method | None |
| 17 | Transformation | False |
| 18 | Transformation Method | None |
| 19 | PCA | False |
| 20 | PCA Method | None |
| 21 | PCA Components | None |
| 22 | Ignore Low Variance | False |
| 23 | Combine Rare Levels | False |
| 24 | Rare Level Threshold | None |
| 25 | Numeric Binning | False |
| 26 | Remove Outliers | False |
| 27 | Outliers Threshold | None |
| 28 | Remove Multicollinearity | False |
| 29 | Multicollinearity Threshold | None |
| 30 | Clustering | False |
| 31 | Clustering Iteration | None |
| 32 | Polynomial Features | False |
| 33 | Polynomial Degree | None |
| 34 | Trignometry Features | False |
| 35 | Polynomial Threshold | None |
| 36 | Group Features | False |
| 37 | Feature Selection | False |
| 38 | Features Selection Threshold | None |
| 39 | Feature Interaction | False |
| 40 | Feature Ratio | False |
| 41 | Interaction Threshold | None |
| 42 | Fix Imbalance | False |
| 43 | Fix Imbalance Method | SMOTE |
Comparing All Models
Comparing all models to evaluate performance is the recommended starting point for modeling once the setup is completed (unless you exactly know what kind of model you need, which is often not the case). This function trains all models in the model library and scores them using stratified cross validation for metric evaluation. The output prints a score grid that shows average Accuracy, Recall, Precision, F1, Kappa, and MCC accross the folds (10 by default) along with training times.
best = compare_models()
| Model | Accuracy | AUC | Recall | Prec. | F1 | Kappa | MCC | TT (Sec) | |
|---|---|---|---|---|---|---|---|---|---|
| 0 | Naive Bayes | 0.9567 | 0.0000 | 0.9556 | 0.9619 | 0.9561 | 0.9348 | 0.9378 | 0.0076 |
| 1 | K Neighbors Classifier | 0.9467 | 0.0000 | 0.9444 | 0.9633 | 0.9430 | 0.9197 | 0.9295 | 0.0077 |
| 2 | Extreme Gradient Boosting | 0.9467 | 0.0000 | 0.9444 | 0.9633 | 0.9430 | 0.9197 | 0.9295 | 0.0521 |
| 3 | Light Gradient Boosting Machine | 0.9467 | 0.0000 | 0.9444 | 0.9600 | 0.9451 | 0.9197 | 0.9271 | 0.0606 |
| 4 | Random Forest Classifier | 0.9456 | 0.0000 | 0.9444 | 0.9536 | 0.9446 | 0.9182 | 0.9227 | 0.2123 |
| 5 | Quadratic Discriminant Analysis | 0.9456 | 0.0000 | 0.9444 | 0.9536 | 0.9446 | 0.9182 | 0.9227 | 0.0071 |
| 6 | Extra Trees Classifier | 0.9356 | 0.0000 | 0.9333 | 0.9433 | 0.9337 | 0.9030 | 0.9082 | 0.3872 |
| 7 | CatBoost Classifier | 0.9356 | 0.0000 | 0.9333 | 0.9433 | 0.9337 | 0.9030 | 0.9082 | 2.1596 |
| 8 | Gradient Boosting Classifier | 0.9344 | 0.0000 | 0.9333 | 0.9453 | 0.9332 | 0.9015 | 0.9076 | 0.1215 |
| 9 | Logistic Regression | 0.9244 | 0.0000 | 0.9222 | 0.9345 | 0.9221 | 0.8861 | 0.8929 | 0.0213 |
| 10 | Ada Boost Classifier | 0.9244 | 0.0000 | 0.9222 | 0.9350 | 0.9223 | 0.8864 | 0.8931 | 0.1121 |
| 11 | Decision Tree Classifier | 0.9233 | 0.0000 | 0.9222 | 0.9342 | 0.9211 | 0.8848 | 0.8919 | 0.0082 |
| 12 | Linear Discriminant Analysis | 0.8611 | 0.0000 | 0.8667 | 0.8520 | 0.8482 | 0.7944 | 0.8130 | 0.0081 |
| 13 | Ridge Classifier | 0.8289 | 0.0000 | 0.8361 | 0.8251 | 0.8147 | 0.7461 | 0.7686 | 0.0098 |
| 14 | SVM - Linear Kernel | 0.7233 | 0.0000 | 0.7222 | 0.6520 | 0.6502 | 0.5884 | 0.6439 | 0.0093 |
Create a Model
create_model is the most granular function in PyCaret and is often the foundation behind most of the PyCaret functionalities. As the name suggests this function trains and evaluates a model using cross validation that can be set with fold parameter. The output prints a score grid that shows Accuracy, Recall, Precision, F1, Kappa and MCC by fold.
For the remaining part of this tutorial, we will work with the below models as our candidate models. The selections are for illustration purposes only and do not necessarily mean they are the top performing or ideal for this type of data.
- Decision Tree Classifier ('dt')
- K Neighbors Classifier ('knn')
- Logistic Regression ('lr')
There are 18 classifiers available in the model library of PyCaret. Please view the create_model() docstring for the list of all available models.
Decision Tree Classifier
dt = create_model('dt')
| Accuracy | AUC | Recall | Prec. | F1 | Kappa | MCC | |
|---|---|---|---|---|---|---|---|
| 0 | 1.0000 | 0.0000 | 1.0000 | 1.0000 | 1.0000 | 1.0000 | 1.0000 |
| 1 | 1.0000 | 0.0000 | 1.0000 | 1.0000 | 1.0000 | 1.0000 | 1.0000 |
| 2 | 1.0000 | 0.0000 | 1.0000 | 1.0000 | 1.0000 | 1.0000 | 1.0000 |
| 3 | 0.9000 | 0.0000 | 0.8889 | 0.9250 | 0.8971 | 0.8485 | 0.8616 |
| 4 | 0.8889 | 0.0000 | 0.8889 | 0.9167 | 0.8857 | 0.8333 | 0.8492 |
| 5 | 1.0000 | 0.0000 | 1.0000 | 1.0000 | 1.0000 | 1.0000 | 1.0000 |
| 6 | 0.8889 | 0.0000 | 0.8889 | 0.9167 | 0.8857 | 0.8333 | 0.8492 |
| 7 | 0.8889 | 0.0000 | 0.8889 | 0.9167 | 0.8857 | 0.8333 | 0.8492 |
| 8 | 1.0000 | 0.0000 | 1.0000 | 1.0000 | 1.0000 | 1.0000 | 1.0000 |
| 9 | 0.6667 | 0.0000 | 0.6667 | 0.6667 | 0.6571 | 0.5000 | 0.5095 |
| Mean | 0.9233 | 0.0000 | 0.9222 | 0.9342 | 0.9211 | 0.8848 | 0.8919 |
| SD | 0.0997 | 0.0000 | 0.1000 | 0.0971 | 0.1025 | 0.1496 | 0.1453 |
Neighbors Classifier
knn = create_model('knn')
| Accuracy | AUC | Recall | Prec. | F1 | Kappa | MCC | |
|---|---|---|---|---|---|---|---|
| 0 | 1.0000 | 0.0000 | 1.0000 | 1.0000 | 1.0000 | 1.0000 | 1.0000 |
| 1 | 1.0000 | 0.0000 | 1.0000 | 1.0000 | 1.0000 | 1.0000 | 1.0000 |
| 2 | 0.9000 | 0.0000 | 0.8889 | 0.9250 | 0.8971 | 0.8485 | 0.8616 |
| 3 | 0.9000 | 0.0000 | 0.8889 | 0.9250 | 0.8971 | 0.8485 | 0.8616 |
| 4 | 1.0000 | 0.0000 | 1.0000 | 1.0000 | 1.0000 | 1.0000 | 1.0000 |
| 5 | 1.0000 | 0.0000 | 1.0000 | 1.0000 | 1.0000 | 1.0000 | 1.0000 |
| 6 | 0.8889 | 0.0000 | 0.8889 | 0.9167 | 0.8857 | 0.8333 | 0.8492 |
| 7 | 1.0000 | 0.0000 | 1.0000 | 1.0000 | 1.0000 | 1.0000 | 1.0000 |
| 8 | 1.0000 | 0.0000 | 1.0000 | 1.0000 | 1.0000 | 1.0000 | 1.0000 |
| 9 | 0.7778 | 0.0000 | 0.7778 | 0.8667 | 0.7500 | 0.6667 | 0.7223 |
| Mean | 0.9467 | 0.0000 | 0.9444 | 0.9633 | 0.9430 | 0.9197 | 0.9295 |
| SD | 0.0730 | 0.0000 | 0.0745 | 0.0475 | 0.0801 | 0.1097 | 0.0940 |
Logistic Regression
lr = create_model('lr')
| Accuracy | AUC | Recall | Prec. | F1 | Kappa | MCC | |
|---|---|---|---|---|---|---|---|
| 0 | 0.9000 | 0.0000 | 0.8889 | 0.9200 | 0.8956 | 0.8462 | 0.8598 |
| 1 | 1.0000 | 0.0000 | 1.0000 | 1.0000 | 1.0000 | 1.0000 | 1.0000 |
| 2 | 1.0000 | 0.0000 | 1.0000 | 1.0000 | 1.0000 | 1.0000 | 1.0000 |
| 3 | 0.9000 | 0.0000 | 0.8889 | 0.9250 | 0.8971 | 0.8485 | 0.8616 |
| 4 | 1.0000 | 0.0000 | 1.0000 | 1.0000 | 1.0000 | 1.0000 | 1.0000 |
| 5 | 1.0000 | 0.0000 | 1.0000 | 1.0000 | 1.0000 | 1.0000 | 1.0000 |
| 6 | 0.8889 | 0.0000 | 0.8889 | 0.9167 | 0.8857 | 0.8333 | 0.8492 |
| 7 | 0.6667 | 0.0000 | 0.6667 | 0.6667 | 0.6571 | 0.5000 | 0.5095 |
| 8 | 1.0000 | 0.0000 | 1.0000 | 1.0000 | 1.0000 | 1.0000 | 1.0000 |
| 9 | 0.8889 | 0.0000 | 0.8889 | 0.9167 | 0.8857 | 0.8333 | 0.8492 |
| Mean | 0.9244 | 0.0000 | 0.9222 | 0.9345 | 0.9221 | 0.8861 | 0.8929 |
| SD | 0.0994 | 0.0000 | 0.1000 | 0.0970 | 0.1022 | 0.1492 | 0.1450 |
Tune a Model
When a model is created using the create_model() function it uses the default hyperparameters to train the model. In order to tune hyperparameters, the tune_model() function is used. This function automatically tunes the hyperparameters of a model using Random Grid Search on a pre-defined search space. The output prints a score grid that shows Accuracy, AUC, Recall, Precision, F1, Kappa, and MCC by fold for the best model. To use the custom search grid, you can pass custom_grid parameter in the tune_model function (see 9.2 KNN tuning below).
Decision Tree Classifier
tuned_dt = tune_model(dt)
| Accuracy | AUC | Recall | Prec. | F1 | Kappa | MCC | |
|---|---|---|---|---|---|---|---|
| 0 | 1.0000 | 0.0000 | 1.0000 | 1.0000 | 1.0000 | 1.0000 | 1.0000 |
| 1 | 1.0000 | 0.0000 | 1.0000 | 1.0000 | 1.0000 | 1.0000 | 1.0000 |
| 2 | 1.0000 | 0.0000 | 1.0000 | 1.0000 | 1.0000 | 1.0000 | 1.0000 |
| 3 | 0.9000 | 0.0000 | 0.8889 | 0.9250 | 0.8971 | 0.8485 | 0.8616 |
| 4 | 0.8889 | 0.0000 | 0.8889 | 0.9167 | 0.8857 | 0.8333 | 0.8492 |
| 5 | 1.0000 | 0.0000 | 1.0000 | 1.0000 | 1.0000 | 1.0000 | 1.0000 |
| 6 | 0.8889 | 0.0000 | 0.8889 | 0.9167 | 0.8857 | 0.8333 | 0.8492 |
| 7 | 0.8889 | 0.0000 | 0.8889 | 0.9167 | 0.8857 | 0.8333 | 0.8492 |
| 8 | 1.0000 | 0.0000 | 1.0000 | 1.0000 | 1.0000 | 1.0000 | 1.0000 |
| 9 | 0.7778 | 0.0000 | 0.7778 | 0.7778 | 0.7778 | 0.6667 | 0.6667 |
| Mean | 0.9344 | 0.0000 | 0.9333 | 0.9453 | 0.9332 | 0.9015 | 0.9076 |
| SD | 0.0731 | 0.0000 | 0.0737 | 0.0677 | 0.0738 | 0.1097 | 0.1063 |
K Neighbors Classifier
import numpy as np
tuned_knn = tune_model(knn, custom_grid = {'n_neighbors' : np.arange(0,50,1)})
| Accuracy | AUC | Recall | Prec. | F1 | Kappa | MCC | |
|---|---|---|---|---|---|---|---|
| 0 | 1.0000 | 0.0000 | 1.0000 | 1.0000 | 1.0000 | 1.0000 | 1.0000 |
| 1 | 1.0000 | 0.0000 | 1.0000 | 1.0000 | 1.0000 | 1.0000 | 1.0000 |
| 2 | 0.9000 | 0.0000 | 0.8889 | 0.9250 | 0.8971 | 0.8485 | 0.8616 |
| 3 | 0.9000 | 0.0000 | 0.8889 | 0.9250 | 0.8971 | 0.8485 | 0.8616 |
| 4 | 0.8889 | 0.0000 | 0.8889 | 0.9167 | 0.8857 | 0.8333 | 0.8492 |
| 5 | 1.0000 | 0.0000 | 1.0000 | 1.0000 | 1.0000 | 1.0000 | 1.0000 |
| 6 | 1.0000 | 0.0000 | 1.0000 | 1.0000 | 1.0000 | 1.0000 | 1.0000 |
| 7 | 0.8889 | 0.0000 | 0.8889 | 0.9167 | 0.8857 | 0.8333 | 0.8492 |
| 8 | 1.0000 | 0.0000 | 1.0000 | 1.0000 | 1.0000 | 1.0000 | 1.0000 |
| 9 | 0.7778 | 0.0000 | 0.7778 | 0.8667 | 0.7500 | 0.6667 | 0.7223 |
| Mean | 0.9356 | 0.0000 | 0.9333 | 0.9550 | 0.9316 | 0.9030 | 0.9144 |
| SD | 0.0725 | 0.0000 | 0.0737 | 0.0476 | 0.0793 | 0.1089 | 0.0936 |
Logistic Regression
tuned_lr = tune_model(lr)
| Accuracy | AUC | Recall | Prec. | F1 | Kappa | MCC | |
|---|---|---|---|---|---|---|---|
| 0 | 1.0000 | 0.0000 | 1.0000 | 1.0000 | 1.0000 | 1.0000 | 1.0000 |
| 1 | 1.0000 | 0.0000 | 1.0000 | 1.0000 | 1.0000 | 1.0000 | 1.0000 |
| 2 | 1.0000 | 0.0000 | 1.0000 | 1.0000 | 1.0000 | 1.0000 | 1.0000 |
| 3 | 0.9000 | 0.0000 | 0.8889 | 0.9250 | 0.8971 | 0.8485 | 0.8616 |
| 4 | 1.0000 | 0.0000 | 1.0000 | 1.0000 | 1.0000 | 1.0000 | 1.0000 |
| 5 | 1.0000 | 0.0000 | 1.0000 | 1.0000 | 1.0000 | 1.0000 | 1.0000 |
| 6 | 0.8889 | 0.0000 | 0.8889 | 0.9167 | 0.8857 | 0.8333 | 0.8492 |
| 7 | 0.8889 | 0.0000 | 0.8889 | 0.9167 | 0.8857 | 0.8333 | 0.8492 |
| 8 | 1.0000 | 0.0000 | 1.0000 | 1.0000 | 1.0000 | 1.0000 | 1.0000 |
| 9 | 0.7778 | 0.0000 | 0.7778 | 0.7778 | 0.7778 | 0.6667 | 0.6667 |
| Mean | 0.9456 | 0.0000 | 0.9444 | 0.9536 | 0.9446 | 0.9182 | 0.9227 |
| SD | 0.0738 | 0.0000 | 0.0745 | 0.0688 | 0.0745 | 0.1108 | 0.1077 |
Plot a Model
Before model finalization, the plot_model() function can be used to analyze the performance across different aspects such as AUC, confusion_matrix, decision boundary etc. This function takes a trained model object and returns a plot based on the test / hold-out set.
There are 15 different plots available, please see the plot_model() docstring for the list of available plots.
Confusion Matrix
plot_model(tuned_knn, plot = 'confusion_matrix')
Classification Report
plot_model(tuned_knn, plot = 'class_report')
Decision Boundary Plot
plot_model(tuned_knn, plot='boundary')
Prediction Error Plot
plot_model(tuned_knn, plot = 'error')
Predict on test / hold-out Sample
predict_model(tuned_knn);
| Model | Accuracy | AUC | Recall | Prec. | F1 | Kappa | MCC | |
|---|---|---|---|---|---|---|---|---|
| 0 | One Vs Rest Classifier | 0.9512 | 0 | 0.9505 | 0.9512 | 0.9512 | 0.9268 | 0.9268 |