PyCaret: Better Machine Learning with Python | by Dario Radečić | Towards Data Science

Pick up any data science book or online course. I’d bet the only machine learning library covered is Scikit-Learn. It’s a great place to start, don’t get me wrong, but in 2020 we need something a bit more automated and time-saving.
That’s where PyCaret comes in. It’s a relatively new library, so there are not too many tutorials available — although the official documentation does an amazing job. Here’s a one-sentence summary:
PyCaret is an open source, low-code machine learning library in Python that allows you to go from preparing your data to deploying your model within seconds in your choice of notebook environment.[1]
If you’re more of a video person, or just want to reinforce your knowledge, feel free to watch our video on the topic. Source code is included:
This article is aimed towards someone familiar with the concepts of machine learning, and also knows how to use various algorithms in libraries like Scikit-Learn. The ideal reader is aware of the need for automation and doesn’t want to spend days or weeks seeking optimal algorithm and its hyperparameters.
This will be a rather short and hands-on article, as the aim is only to showcase what the library is capable of doing. Future articles will focus more on regression/classification/clustering tasks.
Okay, let’s get started!
The installation process varies based on your operating system. I’m on macOS, so some steps were required before the library could be installed.
Once done, you’re ready to start. We’ll begin with two imports:
from pycaret.classification import * from pycaret.datasets import get_data
The first one is a recommended way to get started with classification tasks — although some of you might not be comfortable with the import * syntax. Avoid it if you with. The second import allows us to use built-in datasets.
We’ll use the in-built diabetes dataset, and get_data() function to obtain it. Next, we need to do a bit of setup, and tell PyCaret what’s the target variable:
diabetes = get_data(‘diabetes’)exp_clf = setup(diabetes, target=’Class variable’)
Executing this code will result in the following:
This resulting DataFrame is quite long and reports back a lot of information about your data. We can now proceed with the machine learning portion.
This step is very simple. Type the following:
compare_models()
And here’s the result:
Yep, the compare_models() function does all that. It also highlights cells with the highest scores in yellow, so it’s easier to see. Logistic regression seems to do the best accuracy-wise, but the XGBoost algorithm performs the best overall — so that’s the one we’ll use.
How? Once again, can’t be any simpler:
xgb = create_model(‘xgboost’)
On this link, you’ll find all model abbreviations, so you can use something other than XGBoost. Up next, let’s see visually how our model performs.
We can use the plot_model() function to visualize model performance:
plot_model(xgb)
It shows the ROC curves plot by default, but that’s easy to change. Here’s the confusion matrix:
plot_model(xgb, ‘confusion_matrix’)
And the classification report:
plot_model(xgb, ‘class_report’)
Neat, right? Here’s the link to all of the visualizations you can make. Let’s proceed with model interpretability.
SHAP, or SHapley Additive exPlanations, is a way to explain the outputs of a machine learning model. We can use it to see which features are most important by plotting the SHAP values of every feature for every sample.
Once again, PyCaret makes it utterly simple to do:
interpret_model(xgb)
The plot above sorts features by the sum of SHAP value magnitudes over all samples and uses SHAP values to show the distribution of the impacts each feature has on the model output[2]. Quite a long sentence, so read through it a couple of times. The color represents the feature value — red being high and blue being low.
In a nutshell, it states that the higher levels of Plasma glucose concentration a 2 hours in an oral glucose tolerance test (whatever that means), leads to higher (due to red color) chances for diabetes.
I plan to cover SHAP in much more depth in some other article, so I won’t go too deep here. Let’s proceed with the model evaluation on the test set.
PyCaret split the data into training and test portions (70:30) upon loading, so we don’t have to do it manually.
We can now evaluate the model on previously unseen data:
predictions = predict_model(xgb)
The above code yields the following results:
Okay, now when that’s covered, let’s see how to save and load the model.
Before saving the model, we need to finalize it:
finalize_model(xgb)
Guess how simple it is to save the model:
save_model(xgb, ‘diabetes_xgboost’)
The model is saved in the pickle format.
Try to guess the name of the function for loading saved models:
model = load_model(‘diabetes_xgboost’)
And you’re ready to use it again.
We’ve covered a lot of things today, so let’s wrap things up in the next section. More articles like this are coming soon, so stay tuned.
Today we’ve briefly covered what PyCaret has to offer. My idea is to turn this into a series, both article-based and video-based, and cover the library in much more depth.
When working with libraries like this one, the ones like Scikit-Learn quickly become a part of the past. Not because they’re bad, but these ones are just superior in every single way.
I hope you’ve liked it and managed to get something useful out of it. Thanks for reading.
Loved the article? Become a Medium member to continue learning without limits. I’ll receive a portion of your membership fee if you use the following link, with no extra cost to you.