Build with PyCaret, Deploy with FastAPI | by Moez Ali | Towards Data Science

This is a step-by-step, beginner-friendly tutorial on how to build an end-to-end Machine Learning Pipeline with PyCaret and deploy it in production as a web API using FastAPI.
Build an end-to-end machine learning pipeline using PyCaret
What is a deployment and why do we deploy machine learning models
Develop an API using FastAPI to generate predictions on unseen data
Use Python to send a request to API for generating predictions programmatically.
This tutorial will cover the entire machine learning life cycle at a high level which is broken down into the following sections:
PyCaret is an open-source, low-code machine learning library and end-to-end model management tool built-in Python for automating machine learning workflows. It is incredibly popular for its ease of use, simplicity, and ability to build and deploy end-to-end ML prototypes quickly and efficiently.
PyCaret is an alternate low-code library that can replace hundreds of code lines with few lines only. This makes the experiment cycle exponentially fast and efficient.
PyCaret is simple and easy to use. All the operations performed in PyCaret are sequentially stored in a Pipeline that is fully automated for deployment. Whether it’s imputing missing values, one-hot-encoding, transforming categorical data, feature engineering, or even hyperparameter tuning, PyCaret automates all of it.
To learn more about PyCaret, check out their GitHub.
FastAPI is a modern, fast (high-performance), web framework for building APIs with Python 3.6+ based on standard Python type hints. The key features are:
Fast: Very high performance, on par with NodeJS and Go (thanks to Starlette and Pydantic). One of the fastest Python frameworks available.
Fast to code: Increase the speed to develop features by about 200% to 300%.
Easy: Designed to be easy to use and learn. Less time reading docs.
To learn more about FastAPI, check out their GitHub.
Installing PyCaret is very easy and takes only a few minutes. We strongly recommend using a virtual environment to avoid potential conflicts with other libraries.
PyCaret’s default installation is a slim version of pycaret that only installs hard dependencies listed here.
# install slim version (default)pip install pycaret# install the full versionpip install pycaret[full]
When you install the full version of pycaret, all the optional dependencies as listed here are also installed.
You can install FastAPI from pip.
pip install fastapi
For this tutorial, I will be using a very popular case study by Darden School of Business, published in Harvard Business. The case is regarding the story of two people who are going to be married in the future. The guy named Greg wanted to buy a ring to propose to a girl named Sarah. The problem is to find the ring Sarah will like, but after a suggestion from his close friend, Greg decides to buy a diamond stone instead so that Sarah can decide her choice. Greg then collects data of 6000 diamonds with their price and attributes like cut, color, shape, etc.
In this tutorial, I will be using a dataset from a very popular case study by the Darden School of Business, published in Harvard Business. The goal of this tutorial is to predict the diamond price based on its attributes like carat weight, cut, color, etc. You can download the dataset from PyCaret’s repository.
# load the dataset from pycaretfrom pycaret.datasets import get_datadata = get_data('diamond')
Let’s do some quick visualization to assess the relationship of independent features (weight, cut, color, clarity, etc.) with the target variable i.e. Price
# plot scatter carat_weight and Priceimport plotly.express as pxfig = px.scatter(x=data['Carat Weight'], y=data['Price'],                  facet_col = data['Cut'], opacity = 0.25, template = 'plotly_dark', trendline='ols',                 trendline_color_override = 'red', title = 'SARAH GETS A DIAMOND - A CASE STUDY')fig.show()
Let’s check the distribution of the target variable.
# plot histogramfig = px.histogram(data, x=["Price"], template = 'plotly_dark', title = 'Histogram of Price')fig.show()
Notice that distribution of Price is right-skewed, we can quickly check to see if log transformation can make Price approximately normal to give fighting chance to algorithms that assume normality.
import numpy as np# create a copy of datadata_copy = data.copy()# create a new feature Log_Pricedata_copy['Log_Price'] = np.log(data['Price'])# plot histogramfig = px.histogram(data_copy, x=["Log_Price"], title = 'Histgram of Log Price', template = 'plotly_dark')fig.show()
This confirms our hypothesis. The transformation will help us to get away with skewness and make the target variable approximately normal. Based on this, we will transform the Price variable before training our models.
Common to all modules in PyCaret, the setup is the first and the only mandatory step in any machine learning experiment performed in PyCaret. This function takes care of all the data preparation required prior to training models. Besides performing some basic default processing tasks, PyCaret also offers a wide array of pre-processing features. To learn more about all the preprocessing functionalities in PyCaret, you can see this link.
# init setupfrom pycaret.regression import *s = setup(data, target = 'Price', transform_target = True)
Whenever you initialize the setup function in PyCaret, it profiles the dataset and infers the data types for all input features. In this case, you can see except for Carat Weight all the other features are inferred as categorical, which is correct. You can press enter to continue.
Notice that I have used transform_target = True inside the setup. PyCaret will transform the Price variable behind the scene using box-cox transformation. It affects the distribution of data in a similar way as log transformation (technically different). If you would like to learn more about box-cox transformations, you can refer to this link.
Now that data preparation is done, let’s start the training process by using compare_models functionality. This function trains all the algorithms available in the model library and evaluates multiple performance metrics using cross-validation.
# compare all modelsbest = compare_models()
The best model based on Mean Absolute Error (MAE) is CatBoost Regressor. MAE using 10-fold cross-validation is $543 compared to the average diamond value of $11,600. This is less than 5%. Not bad for the efforts we have put in so far.
# check the residuals of trained modelplot_model(best, plot = 'residuals_interactive')
# check feature importanceplot_model(best, plot = 'feature')
Let’s now finalize the best model i.e. train the best model on the entire dataset including the test set and then save the pipeline as a pickle file.
# finalize the modelfinal_best = finalize_model(best)# save model to disksave_model(final_best, 'diamond-pipeline')
First, let’s understand why to deploy machine learning models?
The deployment of machine learning models is the process of making models available in production where web applications, enterprise software, and APIs can consume the trained model by providing new data points and generating predictions. Normally machine learning models are built so that they can be used to predict an outcome (binary value i.e. 1 or 0 for Classification, continuous values for Regression, labels for Clustering, etc. There are two broad ways of generating predictions (i) predict by batch; and (ii) predict in real-time. This tutorial will show how you can deploy your machine learning models as API to predict in real-time.
Now that we understand why deployment is necessary and we have everything we need to create an API i.e. Trained Model Pipeline as a pickle file. Creating an API is extremely simple using FastAPI.
The first few lines of the code are simple imports. Line 8 is initializing an app by calling FastAPI() . Line 11 is loading the trained model diamond-pipeline from your disk (Your script must be in the same folder as the file). Line 15–20 is defining a function called predict which will take the input and internally uses PyCaret’s predict_model function to generate predictions and return the value as a dictionary (Line 20).
You can then run this script by running the following command in your command prompt. You must be in the same directory as the python script and the model pickle file is, before executing this command.
uvicorn main:app --reload
This will initialize an API service on your localhost. On your browser type http://localhost:8000/docs and it should show something like this:
Click on green POST button and it will be open a form like this:
Click on “Try it out” on the top right corner and fill in some values in the form and click on “Execute”. If you have done everything correctly, you will see this response:
Notice that under the response body we have a prediction value of 5396 (this is based on values I entered in the form). This means that given all the attributes you entered, the predicted price of this diamond is $5,396.
This is great, this shows that our API is working. Now we can use the requests library in Python or any other language to connect to API and generate predictions. I have created the script shown below for that:
Let’s see this function in action:
Notice that prediction is 5396 that's because I have used the same values here as I have used in the form above. (1.1, ‘Ideal’, ‘H’, ‘SII’, ‘VG’, ‘EX’, ‘GIA’)
I hope that you will appreciate the ease of use and simplicity in PyCaret and FastAPI. In less than 25 lines of code and few minutes of experimentation, I have trained and evaluated multiple models using PyCaret and deployed ML Pipeline using an API.
Next week I will be writing a tutorial to advance deployment to the next level, I will introduce the concepts like Containerization and Dockers in my next tutorial. Please follow me on Medium, LinkedIn, and Twitter to get more updates.
There is no limit to what you can achieve using this lightweight workflow automation library in Python. If you find this useful, please do not forget to give us ⭐️ on our GitHub repository.
To hear more about PyCaret follow us on LinkedIn and Youtube.
Join us on our slack channel. Invite link here.
Build your own AutoML in Power BI using PyCaret 2.0Deploy Machine Learning Pipeline on Azure using DockerDeploy Machine Learning Pipeline on Google Kubernetes EngineDeploy Machine Learning Pipeline on AWS FargateBuild and deploy your first machine learning web appDeploy PyCaret and Streamlit app using AWS Fargate serverlessBuild and deploy machine learning web app using PyCaret and StreamlitDeploy Machine Learning App built using Streamlit and PyCaret on GKE
DocumentationBlogGitHubStackOverflowInstall PyCaretNotebook TutorialsContribute in PyCaret
Click on the links below to see the documentation and working examples.
ClassificationRegressionClusteringAnomaly DetectionNatural Language ProcessingAssociation Rule Mining