PyCaret Tutorial: Low-Code Machine Learning for Rapid Prototyping
PyCaret is an open-source Python library that provides a low-code interface for conducting machine learning experiments quickly and efficiently. With PyCaret, you can complete an entire ML pipeline — from preprocessing, training, tuning, to deployment — with just a few lines of code.
This library is ideal for data scientists who want to perform rapid prototyping, compare dozens of models simultaneously, and get the best baseline model without writing lengthy boilerplate code. PyCaret wraps popular libraries like scikit-learn, XGBoost, LightGBM, CatBoost, and many more into a single consistent API.
In this tutorial, we will learn how to use PyCaret from installation to model deployment, complete with runnable code examples.
Installation
PyCaret is available on PyPI and can be installed using pip. It is recommended to use a virtual environment to avoid dependency conflicts.
# Create virtual environment
python -m venv pycaret-env
source pycaret-env/bin/activate # Linux/Mac
pycaret-env\Scripts\activate # Windows
Install full PyCaret
pip install pycaret[full]
If you only need specific modules, you can install selectively:
# Classification and regression only
pip install pycaret
With NLP support
pip install pycaret[nlp]
With time series support
pip install pycaret[ts]
To verify the installation:
import pycaret
print(pycaret.version)
System Requirements
PyCaret requires Python 3.9 or newer. Some optional features need additional dependencies:
# For interactive visualizations
pip install plotly kaleido
For cloud deployment
pip install pycaret[aws] # AWS
pip install pycaret[gcp] # Google Cloud
pip install pycaret[azure] # Azure
Core Concepts
PyCaret is organized into several modules based on the type of machine learning task:
| Module | Use Case |
|--------|----------|
| pycaret.classification | Classification (binary & multiclass) |
| pycaret.regression | Regression |
| pycaret.clustering | Clustering |
| pycaret.anomaly | Anomaly detection |
| pycaret.timeseries | Time series forecasting |
| pycaret.nlp | Topic modeling |
Every module follows the same workflow: setup → compare → create → tune → evaluate → deploy.
Basic Usage: Classification
Let's start with a classification example using PyCaret's built-in dataset.
Step 1: Setup Experiment
The setup() function is the first step in any PyCaret experiment. It handles data preprocessing automatically.
from pycaret.classification import
Load built-in dataset
from pycaret.datasets import get
data
data = getdata('diabetes')
Setup experiment
clf = setup(
data=data,
target='Class variable',
sessionid=42,
verbose=False
)
PyCaret automatically handles:
- Data type detection (numeric, categorical)
- Train-test split (default 70/30)
- Missing value imputation
- Categorical variable encoding
- Normalization (optional)
Step 2: Compare Models
The comparemodels() function trains and evaluates all available models with a single call.
# Compare all models
bestmodel = comparemodels()
Compare with specific metric
bestmodel = comparemodels(sort='AUC', nselect=3)
This function displays a comparison table with metrics like Accuracy, AUC, Recall, Precision, F1, Kappa, and MCC for each model, letting you immediately see which model performs best.
Step 3: Create a Specific Model
If you already know which model you want to use:
# Create Random Forest
rf = createmodel('rf')
Create Gradient Boosting
gbc = createmodel('gbc')
Create LightGBM
lgbm = createmodel('lightgbm')
Create XGBoost
xgb = createmodel('xgboost')
Each model is automatically evaluated using cross-validation (default 10-fold).
Step 4: Hyperparameter Tuning
# Tune with random search
tunedrf = tunemodel(rf)
Tune with specific iterations
tunedrf = tunemodel(rf, niter=100)
Tune for specific metric
tunedrf = tunemodel(rf, optimize='F1')
Tune using Optuna
tunedrf = tunemodel(rf, searchlibrary='optuna', niter=50)
Step 5: Evaluate the Model
# Show all evaluation plots
evaluatemodel(tunedrf)
Plot confusion matrix
plotmodel(tunedrf, plot='confusionmatrix')
Plot ROC curve
plotmodel(tunedrf, plot='auc')
Plot feature importance
plotmodel(tunedrf, plot='feature')
Plot decision boundary (for 2D)
plotmodel(tunedrf, plot='boundary')
Step 6: Predict and Finalize
# Predict on test set
predictions = predictmodel(tunedrf)
Predict on new data
newdata = data.drop('Class variable', axis=1).head(5)
newpredictions = predictmodel(tunedrf, data=newdata)
Finalize model (train on entire dataset)
finalmodel = finalizemodel(tunedrf)
Step 7: Save and Load Model
# Save model
savemodel(finalmodel, 'diabetesclassifier')
Load model
loadedmodel = loadmodel('diabetesclassifier')
Basic Usage: Regression
The workflow for regression is nearly identical:
from pycaret.regression import
from pycaret.datasets import getdata
Load dataset
data = getdata('boston')
Setup
reg = setup(
data=data,
target='medv',
sessionid=42,
verbose=False
)
Compare models
best = comparemodels()
Create and tune model
et = createmodel('et')
tunedet = tunemodel(et, optimize='MAE')
Evaluate
plotmodel(tunedet, plot='residuals')
plotmodel(tunedet, plot='error')
Finalize and save
final = finalizemodel(tunedet)
savemodel(final, 'bostonregressor')
Advanced Usage
Custom Preprocessing Pipeline
from pycaret.classification import
clf = setup(
data=data,
target='Class variable',
session
id=42,
# Preprocessing
normalize=True,
normalizemethod='zscore',
# Feature engineering
polynomialfeatures=True,
polynomialdegree=2,
# Handling imbalance
fiximbalance=True,
fiximbalancemethod='smote',
# Feature selection
featureselection=True,
featureselectionthreshold=0.8,
# Transformation
transformation=True,
transformationmethod='yeo-johnson',
# Remove multicollinearity
removemulticollinearity=True,
multicollinearitythreshold=0.9,
# PCA
pca=True,
pcacomponents=0.95,
verbose=False
)
Ensemble Methods
PyCaret provides several ensemble techniques:
# Bagging
bagged = ensemblemodel(tunedrf, method='Bagging', nestimators=15)
Boosting
boosted = ensemblemodel(tunedrf, method='Boosting', nestimators=15)
Blending (combine multiple models)
blend = blendmodels(
estimatorlist=[tunedrf, createmodel('gbc'), createmodel('lightgbm')],
method='soft'
)
Stacking
stack = stackmodels(
estimatorlist=[tunedrf, createmodel('gbc'), createmodel('lightgbm')],
metamodel=createmodel('lr')
)
Time Series Forecasting
from pycaret.timeseries import
from pycaret.datasets import getdata
Load data
data = getdata('airline')
Setup
ts = setup(
data=data,
fh=12, # forecast horizon: 12 months
sessionid=42,
verbose=False
)
Compare models
best = comparemodels()
Create specific models
arima = createmodel('arima')
ets = createmodel('ets')
prophet = createmodel('prophet')
Tune
tuned = tunemodel(arima)
Plot forecast
plotmodel(tuned, plot='forecast')
plotmodel(tuned, plot='diagnostics')
Predict
predictions = predictmodel(tuned)
Clustering
from pycaret.clustering import
from pycaret.datasets import get
data
data = getdata('jewellery')
Setup (no target since it's unsupervised)
clu = setup(
data=data,
sessionid=42,
verbose=False
)
Create K-Means model
kmeans = createmodel('kmeans', numclusters=4)
Assign cluster labels
result = assignmodel(kmeans)
Visualize
plotmodel(kmeans, plot='cluster')
plotmodel(kmeans, plot='elbow')
plotmodel(kmeans, plot='silhouette')
plotmodel(kmeans, plot='distribution')
Anomaly Detection
from pycaret.anomaly import
from pycaret.datasets import getdata
data = getdata('anomaly')
Setup
ano = setup(
data=data,
sessionid=42,
verbose=False
)
Create Isolation Forest
iforest = createmodel('iforest', fraction=0.05)
Assign anomaly labels
result = assignmodel(iforest)
Plot
plotmodel(iforest, plot='tsne')
plotmodel(iforest, plot='umap')
Experiment Logging with MLflow
PyCaret integrates with MLflow for experiment tracking:
from pycaret.classification import
Setup with MLflow logging
clf = setup(
data=data,
target='Class variable',
logexperiment=True,
experiment
name='diabetesexperiment',
log
plots=True,
logdata=True,
sessionid=42,
verbose=False
)
All following operations are logged automatically
best = comparemodels()
tuned = tunemodel(best)
final = finalizemodel(tuned)
View results in the MLflow UI:
mlflow ui
Open http://localhost:5000
Custom Metrics
from pycaret.classification import
from sklearn.metrics import logloss
Define custom metric
def customlogloss(ytrue, ypred):
return logloss(ytrue, ypred)
Add to PyCaret
addmetric('logloss', 'Log Loss', customlogloss, greaterisbetter=False)
Now this metric appears in comparemodels()
best = comparemodels(sort='logloss')
Remove custom metric if needed
removemetric('logloss')
Custom Pipeline and Transformations
from pycaret.classification import
from sklearn.preprocessing import FunctionTransformer
Custom transformer
def log
transform(X):
import numpy as np
return np.log1p(X.selectdtypes(include='number'))
logtransformer = FunctionTransformer(logtransform)
clf = setup(
data=data,
target='Class variable',
custompipeline=[('logtransform', logtransformer)],
sessionid=42,
verbose=False
)
Deployment
Deploy as FastAPI Service
from pycaret.classification import
Load saved model
model = loadmodel('diabetesclassifier')
Get the full pipeline
pipeline = getconfig('pipeline')
Create the API file:
# app.py
from fastapi import FastAPI
from pydantic import BaseModel
import pandas as pd
from pycaret.classification import loadmodel, predictmodel
app = FastAPI()
model = loadmodel('diabetesclassifier')
class PatientData(BaseModel):
pregnancies: float
glucose: float
bloodpressure: float
skinthickness: float
insulin: float
bmi: float
diabetespedigree: float
age: float
@app.post("/predict")
def predict(patient: PatientData):
data = pd.DataFrame([patient.dict()])
data.columns = [
'Number of times pregnant',
'Plasma glucose concentration',
'Diastolic blood pressure',
'Triceps skin fold thickness',
'2-Hour serum insulin',
'Body mass index',
'Diabetes pedigree function',
'Age (years)'
]
prediction = predictmodel(model, data=data)
return {
"prediction": int(prediction['predictionlabel'].iloc[0]),
"score": float(prediction['predictionscore'].iloc[0])
}
Run:
uvicorn app:app --host 0.0.0.0 --port 8000
Deploy to Cloud
from pycaret.classification import
Deploy to AWS S3
deploymodel(finalmodel, 'my-model', platform='aws', authentication={
'bucket': 'my-pycaret-models',
'path': 'models/diabetes'
})
Deploy to Google Cloud Storage
deploymodel(finalmodel, 'my-model', platform='gcp', authentication={
'project': 'my-project',
'bucket': 'my-pycaret-models'
})
Deploy to Azure Blob Storage
deploymodel(finalmodel, 'my-model', platform='azure', authentication={
'container': 'my-pycaret-models',
'connstr': 'your-connection-string'
})
Best Practices
1. Always Set Session ID
# For reproducibility
clf = setup(data=data, target='target', sessionid=42)
Without sessionid, experiment results may differ on each run due to different random states.
2. Use Appropriate Cross-Validation
# For small datasets, use more folds
best = comparemodels(fold=15)
For large datasets, fewer folds are sufficient
best = comparemodels(fold=5)
For time series data, use TimeSeriesSplit
clf = setup(data=data, target='target', foldstrategy='timeseries', fold=5)
3. Handle Imbalanced Data
clf = setup(
data=data,
target='target',
fiximbalance=True,
sessionid=42
)
Or use appropriate metrics for imbalanced data
best = comparemodels(sort='F1')
4. Automatic Feature Engineering
clf = setup(
data=data,
target='target',
polynomialfeatures=True, # Create polynomial features
trigonometryfeatures=True, # Create trigonometric features
featureinteraction=True, # Feature interactions
binnumericfeatures=['age'], # Bin numeric features
groupfeatures=[['col1', 'col2']], # Group features
sessionid=42
)
5. Use Turbo Mode for Large Datasets
# turbo=True skips slow models (SVM, KNN, etc.)
best = comparemodels(turbo=True)
Or select specific models only
best = comparemodels(include=['rf', 'lightgbm', 'xgboost', 'catboost'])
Exclude specific models
best = comparemodels(exclude=['svm', 'knn'])
6. Combine Multiple Experiments
from pycaret.classification import
Experiment 1: Default preprocessing
clf1 = setup(data=data, target='target', sessionid=42)
best1 = comparemodels(nselect=1)
Experiment 2: With normalization
clf2 = setup(data=data, target='target', normalize=True, sessionid=42)
best2 = comparemodels(nselect=1)
Experiment 3: With PCA
clf3 = setup(data=data, target='target', pca=True, sessionid=42)
best3 = comparemodels(nselect=1)
7. Interpretability with SHAP
# Plot SHAP summary
interpretmodel(tunedrf)
Plot SHAP for specific observation
interpretmodel(tunedrf, plot='reason', observation=5)
Plot correlation
interpretmodel(tunedrf, plot='correlation')
End-to-End Project Example
Here is a complete credit scoring classification project from start to finish:
import pandas as pd
from pycaret.classification import *
1. Load data
data = pd.readcsv('creditdata.csv')
2. Setup experiment
clf = setup(
data=data,
target='default',
sessionid=42,
normalize=True,
fiximbalance=True,
removemulticollinearity=True,
logexperiment=True,
experimentname='creditscoring',
verbose=False
)
3. Compare models
top3 = comparemodels(nselect=3, sort='AUC')
4. Tune top 3 models
tunedmodels = [tunemodel(m, optimize='AUC') for m in top3]
5. Blend top 3 models
blended = blendmodels(tunedmodels, method='soft')
6. Evaluate
plotmodel(blended, plot='auc', save=True)
plotmodel(blended, plot='confusionmatrix', save=True)
plotmodel(blended, plot='feature', save=True)
7. Finalize
final = finalizemodel(blended)
8. Save
savemodel(final, 'creditscoringmodel')
9. Test on new data
newdata = pd.readcsv('newapplications.csv')
results = predictmodel(final, data=newdata)
print(results[['predictionlabel', 'predictionscore']])
Conclusion
PyCaret is an incredibly useful tool for accelerating machine learning workflows. Its key strengths include:
- Rapid Prototyping: Compare dozens of models with a single function call
- Low-Code: Dramatically reduce boilerplate code
- Automatic Preprocessing: Handle missing values, encoding, and scaling automatically
- MLflow Integration: Built-in experiment tracking
- Easy Deployment: Deploy to AWS, GCP, or Azure with a single function
- Modular: Available modules for classification, regression, clustering, anomaly detection, time series, and NLP
PyCaret is best suited for the exploration and prototyping phase. For production systems requiring granular optimization, you may still need custom pipelines. However, as a starting point for finding the best model and preprocessing approach, PyCaret is hard to beat.
Start with comparemodels() to get a baseline, then iterate from there. Combine it with MLflow to systematically track your experiments. With this approach, you can build production-quality ML models in far less time than traditional approaches.