[New Book] Click to get The Beginner's Guide to Data Science!
Use the offer code 20offearlybird to get 20% off. Hurry, sale ends soon!

A Gentle Introduction to PyCaret for Machine Learning

PyCaret is a Python open source machine learning library designed to make performing standard tasks in a machine learning project easy.

It is a Python version of the Caret machine learning package in R, popular because it allows models to be evaluated, compared, and tuned on a given dataset with just a few lines of code.

The PyCaret library provides these features, allowing the machine learning practitioner in Python to spot check a suite of standard machine learning algorithms on a classification or regression dataset with a single function call.

In this tutorial, you will discover the PyCaret Python open source library for machine learning.

After completing this tutorial, you will know:

  • PyCaret is a Python version of the popular and widely used caret machine learning package in R.
  • How to use PyCaret to easily evaluate and compare standard machine learning models on a dataset.
  • How to use PyCaret to easily tune the hyperparameters of a well-performing machine learning model.

Let’s get started.

A Gentle Introduction to PyCaret for Machine Learning

A Gentle Introduction to PyCaret for Machine Learning
Photo by Thomas, some rights reserved.

Tutorial Overview

This tutorial is divided into four parts; they are:

  1. What Is PyCaret?
  2. Sonar Dataset
  3. Comparing Machine Learning Models
  4. Tuning Machine Learning Models

What Is PyCaret?

PyCaret is an open source Python machine learning library inspired by the caret R package.

The goal of the caret package is to automate the major steps for evaluating and comparing machine learning algorithms for classification and regression. The main benefit of the library is that a lot can be achieved with very few lines of code and little manual configuration. The PyCaret library brings these capabilities to Python.

PyCaret is an open-source, low-code machine learning library in Python that aims to reduce the cycle time from hypothesis to insights. It is well suited for seasoned data scientists who want to increase the productivity of their ML experiments by using PyCaret in their workflows or for citizen data scientists and those new to data science with little or no background in coding.

PyCaret Homepage

The PyCaret library automates many steps of a machine learning project, such as:

  • Defining the data transforms to perform (setup())
  • Evaluating and comparing standard models (compare_models())
  • Tuning model hyperparameters (tune_model())

As well as many more features not limited to creating ensembles, saving models, and deploying models.

The PyCaret library has a wealth of documentation for using the API; you can get started here:

We will not explore all of the features of the library in this tutorial; instead, we will focus on simple machine learning model comparison and hyperparameter tuning.

You can install PyCaret using your Python package manager, such as pip. For example:

Once installed, we can confirm that the library is available in your development environment and is working correctly by printing the installed version.

Running the example will load the PyCaret library and print the installed version number.

Your version number should be the same or higher.

If you need help installing PyCaret for your system, you can see the installation instructions here:

Now that we are familiar with what PyCaret is, let’s explore how we might use it on a machine learning project.

Sonar Dataset

We will use the Sonar standard binary classification dataset. You can learn more about it here:

We can download the dataset directly from the URL and load it as a Pandas DataFrame.

The PyCaret seems to require that a dataset has column names, and our dataset does not have column names, so we can set the column number as the column name directly.

Finally, we can summarize the first few rows of data.

Tying this together, the complete example of loading and summarizing the Sonar dataset is listed below.

Running the example first loads the dataset and reports the shape, showing it has 208 rows and 61 columns.

The first five rows are then printed showing that the input variables are all numeric and the target variable is column “60” and has string labels.

Next, we can use PyCaret to evaluate and compare a suite of standard machine learning algorithms to quickly discover what works well on this dataset.

PyCaret for Comparing Machine Learning Models

In this section, we will evaluate and compare the performance of standard machine learning models on the Sonar classification dataset.

First, we must set the dataset with the PyCaret library via the setup() function. This requires that we provide the Pandas DataFrame and specify the name of the column that contains the target variable.

The setup() function also allows you to configure simple data preparation, such as scaling, power transforms, missing data handling, and PCA transforms.

We will specify the data, target variable, and turn off HTML output, verbose output, and requests for user feedback.

Next, we can compare standard machine learning models by calling the compare_models() function.

By default, it will evaluate models using 10-fold cross-validation, sort results by classification accuracy, and return the single best model.

These are good defaults, and we don’t need to change a thing.

Call the compare_models() function will also report a table of results summarizing all of the models that were evaluated and their performance.

Finally, we can report the best-performing model and its configuration.

Tying this together, the complete example of evaluating a suite of standard models on the Sonar classification dataset is listed below.

Running the example will load the dataset, configure the PyCaret library, evaluate a suite of standard models, and report the best model found for the 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 “Extra Trees Classifier” has the best accuracy on the dataset with a score of about 86.95 percent.

We can then see the configuration of the model that was used, which looks like it used default hyperparameter values.

We could use this configuration directly and fit a model on the entire dataset and use it to make predictions on new data.

We can also use the table of results to get an idea of the types of models that perform well on the dataset, in this case, ensembles of decision trees.

Now that we are familiar with how to compare machine learning models using PyCaret, let’s look at how we might use the library to tune model hyperparameters.

Tuning Machine Learning Models

In this section, we will tune the hyperparameters of a machine learning model on the Sonar classification dataset.

We must load and set up the dataset as we did before when comparing models.

We can tune model hyperparameters using the tune_model() function in the PyCaret library.

The function takes an instance of the model to tune as input and knows what hyperparameters to tune automatically. A random search of model hyperparameters is performed and the total number of evaluations can be controlled via the “n_iter” argument.

By default, the function will optimize the ‘Accuracy‘ and will evaluate the performance of each configuration using 10-fold cross-validation, although this sensible default configuration can be changed.

We can perform a random search of the extra trees classifier as follows:

The function will return the best-performing model, which can be used directly or printed to determine the hyperparameters that were selected.

It will also print a table of the results for the best configuration across the number of folds in the k-fold cross-validation (e.g. 10 folds).

Tying this together, the complete example of tuning the hyperparameters of the extra trees classifier on the Sonar dataset is listed below.

Running the example first loads the dataset and configures the PyCaret library.

A grid search is then performed reporting the performance of the best-performing configuration across the 10 folds of cross-validation and the mean accuracy.

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 random search found a configuration with an accuracy of about 75.29 percent, which is not better than the default configuration from the previous section that achieved a score of about 86.95 percent.

We might be able to improve upon the grid search by specifying to the tune_model() function what hyperparameters to search and what ranges to search.

Further Reading

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

Summary

In this tutorial, you discovered the PyCaret Python open source library for machine learning.

Specifically, you learned:

  • PyCaret is a Python version of the popular and widely used caret machine learning package in R.
  • How to use PyCaret to easily evaluate and compare standard machine learning models on a dataset.
  • How to use PyCaret to easily tune the hyperparameters of a well-performing machine learning model.

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

Discover Fast Machine Learning in Python!

Master Machine Learning With Python

Develop Your Own Models in Minutes

...with just a few lines of scikit-learn code

Learn how in my new Ebook:
Machine Learning Mastery With Python

Covers self-study tutorials and end-to-end projects like:
Loading data, visualization, modeling, tuning, and much more...

Finally Bring Machine Learning To
Your Own Projects

Skip the Academics. Just Results.

See What's Inside

15 Responses to A Gentle Introduction to PyCaret for Machine Learning

  1. Avatar
    Faisal Malik November 20, 2020 at 5:29 am #

    PyCaret is really a great boon for the beginners and lazy programmers. Thanks for sharing.

  2. Avatar
    Dan November 20, 2020 at 1:49 pm #

    Hi Jason,
    Love your work! I’m new to pycaret, so how can we use the above with training and test data? Thank you!

    • Avatar
      rajat November 20, 2020 at 11:31 pm #

      #split data into 95% and 5%

      data = dataset.sample(frac=0.95, random_state=786).reset_index(drop=True)
      data_unseen = dataset.drop(data.index).reset_index(drop=True)

    • Avatar
      Jason Brownlee November 21, 2020 at 6:37 am #

      Thanks!

      Good question, perhaps you can review the documentation for the setup() function and see how to specify a custom train/test set:
      https://pycaret.org/classification/

  3. Avatar
    Bartosz November 20, 2020 at 7:26 pm #

    In my case the LGBMClassifier won with the accuracy of 0.8552. I wonder how we can check the value of hyperparamaters before tuning?

    • Avatar
      Jason Brownlee November 21, 2020 at 6:39 am #

      You can review the documentation for the default model hyperparameters, or print the model object to see what hyperparameters were used.

  4. Avatar
    Sandeep S Ramesh November 21, 2020 at 3:04 pm #

    Hi Jason, thanks for sharing this nice quickstart guide for pycaret. I would like to know if pycaret will be able to handle large datasets that cannot be held as one single pandas dataframe. (I.e big data )?

    • Avatar
      Jason Brownlee November 22, 2020 at 6:52 am #

      You’re welcome.

      I don’t believe so, but perhaps check the documentation.

  5. Avatar
    Thibault November 25, 2020 at 8:58 pm #

    Thank you Jason for sharing. I have had consistently worse results with their tuning feature, which is a bit opaque. I struggle to think that it is not a bug.

    Also, I still like to do a lot of my own data preparation with sklearn as I still feel that pycaret lacks transparency and features. But do you think we’re heading towards such landscapes where these frameworks automate all we old timer data scientists used to do from the ground up?

    • Avatar
      Jason Brownlee November 26, 2020 at 6:32 am #

      I like to do it myself as well.

      Yes, I think automl tools and framework start replacing manual work for in-memory datasets.

  6. Avatar
    Iman December 5, 2020 at 4:42 am #

    Hello Jason,
    Thanks for providing this useful information. Really nice!!

    As I see, the Pycaret provide the final results in term of ACC, AUC and ..for classification or MAE for regression tasks.
    Is there any option to have the predicted labels (in classification) and predicted values (in regression)?
    Thank you,
    Iman

  7. Avatar
    Bob Hoyt July 26, 2021 at 8:04 am #

    Many machine learning programs generate performance measures for both of the binary classes e.g. heart disease and no heart disease. In the case of PyCaret, you set the target = target column but I don’t see a way to predict only one class. In the field of medicine, you most commonly want to predict the minority class. When you look at a ROC curve in PyCaret class = 0 and class =1 always have the same AUC. What am I missing?

Leave a Reply