Every Data Scientist Should Use PyCaret | Towards Data Science

IntroductionImporting Libraries and Reading DataSetting Model ParametersComparing Machine Learning AlgorithmsCreating, Interpreting, and Predicting Final ModelSummaryReferences
Introduction
Importing Libraries and Reading Data
Setting Model Parameters
Comparing Machine Learning Algorithms
Creating, Interpreting, and Predicting Final Model
Summary
References
Whereas data scientists in the past have had to use quite a bit of code to come up with testing, comparing, and evaluating machine learning algorithms, there has recently been an emergence of libraries in Python that reduce that work significantly. One of those libraries is PyCaret [2], by Moez Ali, an open-source library with small amounts of code required that ultimately allows you to quickly prepare data to deploy your final model in minutes. There are several benefits, which are native to the functions of PyCaret. Some of those benefits include ease of use, efficiency, and learning about new machine learning algorithms. In addition to those more board benefits, there are also around four main steps that all PyCaret models follow that serve as easy ways to execute a process that otherwise, could take more time without this library. With that being said, I will be discussing the four steps along with their respective benefits and advantages below.
As with most data science projects, you will import your respective libraries and read in your data. We will be using the main library of PyCaret, of course, as well as another popular library.
# import librariesfrom pycaret.regression import *import pandas as pd
In this code, you are simply importing the PyCaret library and regression module, as well as the pandas for reading in your data. These few lines of code are the start to having most of the data science process at your hands, in terms of Python code.
# read in your datadata = pd.read_csv(‘file location of your data on your computer.csv’)
Other modules that you can import include classification, clustering, anomaly detection, natural language processing, and association rule mining. As you can see, this one library encompasses most of the types of data science topics and specific machine learning algorithms.
Within this one regression module, you can compare the following algorithms:
Linear RegressionLasso RegressionRidge RegressionElastic NetOrthogonal Matching PursuitBayesian RidgeGradient Boosting RegressorExtreme Gradient BoostingRandom Forest RegressorDecision Tree RegressorCatBoost RegressorLight Gradient Boosting MachineExtra Trees RegressorAdaBoost RegressorK Neighbors RegressorLasso Least Angle RegressionHuber RegressorPassive Aggressive RegressorLeast Angle Regression
Benefits
The benefit of importing this module is the impressive amount of options without having to worry about separately importing each algorithm, one by one, ultimately saving time and removing unnecessary code.
Now that we know what type of algorithm we are using, we can initiate the setup function of PyCaret. This function includes tasks like data type inference, cleaning and preprocessing data, data sampling, training-test split, and assigning a session ID for reproducibility.
Data Type Inference
The setup feature can allow you to include your data and target, the most fundamental aspect of data science and machine learning algorithms. Examples of data types include categorical, numeric, and your label. You can simply check and confirm the data types of your columns/features by hitting enter once you run your setup.
Data Cleaning and Preprocessing Data
Missing value imputation is automatically applied in this step, as well as categorical encoding. For numeric features, the mean value is filled for missing values, whereas for categorical features, the missing values are filled with the mode of that feature.
— more preprocessing includes ordinal encoding, cardinal encoding, fixing imbalances, normalization, transformation functions
— for feature engineering, there is target transformation, feature interaction group features, binning numeric features, and combining rare level functions
Data Sampling
This feature is quite unique from libraries that work to improve data science processes. The sample size, if over 25,000, will build a linear model to show how the size affects the performance of the model.
Train and Test Split
A common function, PyCaret includes in its setup step. The default ratio for splitting is a 70:30 split.
Session ID
Another word or phrase for this feature is a random seed, or sometimes, random state. It is included so that if a different environment is being used, or you are working on this project in the future, you can reproduce your findings.
# setup your regression parameters regression = setup(data = data,  target = 'what you are trying to predict’, session_id = 200,  )
As you can see, the above code is simple, and you are already nearly halfway finished with your data science modeling already with just really 2 parts of code.
Benefits
The main benefit is how you can see the effect of data sampling size has on common metrics like accuracy, AUC, recall, precision, and F1
Automatic inferring of data types without needing to convert (sometimes)
Missing value imputation for both numeric and categorical features
This feature is where PyCaret really shines, allowing you to compare several machine learning algorithms including some that you may have not heard of yet. Some were mentioned above in the first section, and you can expect to compare algorithms like these — side-by-side with accuracy and error metrics, highlighting which algorithms are the best. Here are some of those metrics below:
Classification Comparisons
Accuracy, AUC, Recall, Precision, F1, as well as Kappa and MCC
Regression Comparisons
MAE, MSE, RMSE, R2, RMSLE, MAPE
Some important features of the compare_models() function is n_select, sort, include, and exlucde.
n_select: allows you to return just the top models based on a metric like accuracy
sort: allows you to sort by your metric, like MAE, for example
include: only use certain algorithms in your comparison — if you know some algorithms take longer and are not as useful, you can use the following parameter below
exclude: remove unnecessary or longer training models from your analysis
# compare modelscompare_models(n_select = 3, sort = 'MAE')
Above is an example code block of how to apply those parameters to the comparing models feature.
Benefits
Reduce time on isolating or removing certain algorithms that you know (from previous experiences or from sampling your data) — as comparing, say, 20 algorithms can take hours, depending on which ones are included
Easy sorting for comparison (PyCaret should highlight which model is best as well automatically)
In the last part of the PyCaret process, you can create the model that was the best one after having seen the comparison of several models (for accuracy, time to train — that is ultimately up to you which is best).
Some critical parameters of the creating model function are the following:
fold
cross_validation
learning_rate
max_depth
etc.
The model or machine learning names are simplified, for example, Decision Trees is ‘dt’, so in that case, let’s pretend we are choosing that one for our creation, prediction, and interpretation of our final model.
When interpreting your model, you can look at a myriad of functions, including:
Summary Plot
Correlation Plot
Reason Plot at Observation Level
These plots are created from the popular SHAP library (which, in my opinion, is the best feature important explanation library)
# create a modeldectrees = create_model('dt')# interpreting modelinterpret_model(dectrees)# predict on test setpredictions = predict_model(dectrees)
Benefits
You can easily choose which model you want to create
You can easily access SHAP functions and respective plots
In only a few lines of code, you can pick the model you want to use, interpret the features and results, and lastly, predict on some test data. You can even deploy a model with PyCaret with AWS. There are also useful integrations with AutoML and MLFlow.
As you can see, with each main step of PyCaret, there are also respective benefits. Although this article is lengthy for the purpose of explanation. The code I used was very minimal to compare several models and predict on test data.
To summarize, we looked at a few steps, including:
Importing Libraries and Reading Data
Setting Model Parameters
Comparing Machine Learning Algorithms
Creating, Interpreting, and Predicting Final Model
Those same steps are articulated though the code below:
# import librariesfrom pycaret.regression import *import pandas as pd# read in your dataspotify = pd.read_csv(‘file location of your data on your computer.csv’)# setup your regression parametersregression = setup(data = data,  target = 'what you are trying to predict’, session_id = 200,  )# compare modelscompare_models()# create a modeldectrees = create_model('dt')# predict on test setpredictions = predict_model(dectrees)# interpreting modelinterpret_model(dectrees)
Some other useful features (as I have not covered all aspects of PyCaret), include but are not limited to, removing multicollinearity, principal competent analysis, ignoring low variance, and removing outliers
I hope you enjoyed this article on a library that everyone should know, PyCaret. Now, I will ask some questions to you. Have you used this library before — why or why not? Have you heard of it? Do you enjoy the data science process in a more manual, yet perhaps, more specific sense, without libraries that are more of a one-stop-shop? What other benefits have you experienced from using this powerful data science and machine learning library? Thank you for reading, I appreciate it!
Please feel free to check out my profile and other articles, Matt Przybyla, as well as reach out to me on LinkedIn.
I am not affiliated with any of the mentioned companies.
[1] Photo by Pablo Arroyo on Unsplash, (2021)
[2] Moez Ali, PyCaret Homepage, (2021)
[3] Photo by NASA on Unsplash, (2015)
[4] Photo by Joshua Aragon on Unsplash, (2019)
[5] M.Przybyla, Comparing models screenshot, (2021)
[6] M.Przybyla, Example SHAP interpretation plot screenshot, (2021)