AutoML with PyCaret 3 in Python: A Practical Guide to Low-Code Machine Learning

Learn how to use PyCaret 3 for automated machine learning in Python. Hands-on classification and regression examples with model comparison, hyperparameter tuning, SHAP interpretation, and deployment — with complete working code.

Why PyCaret Matters in 2026

If you've spent any time building machine learning models, you know the drill. Load the data, clean it up, train a dozen models, compare metrics, tune hyperparameters, deploy — rinse and repeat. Honestly, about 80% of that code looks the same from one project to the next.

That's exactly the problem PyCaret 3 solves.

PyCaret wraps scikit-learn, XGBoost, LightGBM, CatBoost, Optuna, and other popular libraries behind a consistent, low-code API. With PyCaret 3.3 (the latest stable release as of early 2026), you can compare over 18 classification algorithms or 25 regression algorithms with a single function call. The library handles missing-value imputation, categorical encoding, feature scaling, cross-validation, hyperparameter tuning, and even cloud deployment — all through a unified interface that typically needs fewer than 10 lines of code for a complete experiment.

In this guide, I'll walk you through real, working examples for both classification and regression, cover model visualization and interpretation, and show you exactly when PyCaret is the right tool — and when you're better off sticking with plain scikit-learn.

Installation and Environment Setup

PyCaret requires Python 3.9 or later (Python 3.11 is fully supported). Always install it inside a virtual environment — trust me, the dependency list is long enough to cause conflicts if you don't.

Basic Installation

# Create and activate a virtual environment
python -m venv pycaret-env
source pycaret-env/bin/activate  # macOS/Linux
# pycaret-env\Scripts\activate   # Windows

# Install PyCaret (slim version)
pip install pycaret

Full Installation with Optional Extras

# Install with all optional dependencies
pip install pycaret[full]

# Or pick specific extras
pip install pycaret[analysis]   # EDA and profiling tools
pip install pycaret[models]     # additional model libraries
pip install pycaret[tuner]      # Optuna, Hyperopt, scikit-optimize
pip install pycaret[mlops]      # MLflow integration
pip install pycaret[parallel]   # Fugue for distributed computing

Verify the installation by checking the version:

import pycaret
print(pycaret.__version__)  # Should print 3.3.2 or later

Classification Example: Predicting Diabetes Onset

Let's walk through a complete binary classification workflow using the Pima Indians Diabetes dataset, which comes bundled with PyCaret's built-in dataset collection. It's a classic dataset, and perfect for demonstrating the workflow without needing to source external files.

Step 1: Load the Data

from pycaret.datasets import get_data
from pycaret.classification import *

# Load the diabetes dataset (768 rows, 9 columns)
data = get_data("diabetes")
print(data.shape)
print(data.head())

The dataset contains eight numeric features — pregnancies, glucose, blood pressure, skin thickness, insulin, BMI, diabetes pedigree function, and age — plus a binary target column Class variable (0 = no diabetes, 1 = diabetes).

Step 2: Initialize the Experiment with setup()

clf = setup(
    data=data,
    target="Class variable",
    session_id=42,       # reproducibility seed
    fold=10,             # 10-fold cross-validation
    verbose=False
)

Behind the scenes, setup() does quite a bit of heavy lifting automatically:

  • Splits data into a 70/30 train-test holdout
  • Detects feature types (numeric vs. categorical)
  • Imputes missing values (numeric: mean, categorical: mode)
  • Builds a preprocessing pipeline that gets reused at prediction time

You barely notice it happening, but this single call saves you a good 30-40 lines of boilerplate.

Step 3: Compare All Models

# Benchmark every available classifier using 10-fold CV
best_model = compare_models(sort="AUC")

This is where PyCaret really shines. That single line trains and evaluates Logistic Regression, Decision Tree, Random Forest, Gradient Boosting, AdaBoost, LightGBM, XGBoost, CatBoost, KNN, SVM, and more. PyCaret prints a leaderboard ranked by whatever metric you choose — AUC in our case — and returns the top-performing model object.

Want the top three models instead of just one? Easy:

top3 = compare_models(sort="AUC", n_select=3)

Step 4: Tune the Best Model

tuned_model = tune_model(
    best_model,
    optimize="AUC",
    search_library="optuna",   # use Optuna for Bayesian search
    choose_better=True,        # only accept if tuning improves the score
    n_iter=50                  # number of search iterations
)

The choose_better=True parameter is important here — hyperparameter tuning doesn't always improve performance (sometimes it makes things worse, which can be frustrating). This flag ensures PyCaret only replaces the original model if tuning actually produced a better score. You can swap the search backend by changing search_library to "scikit-learn" (the default RandomizedSearchCV), "optuna", or "tune-sklearn".

Step 5: Visualize Model Performance

# AUC-ROC curve
plot_model(tuned_model, plot="auc")

# Confusion matrix with percentages
plot_model(tuned_model, plot="confusion_matrix",
           plot_kwargs={"percent": True})

# Feature importance
plot_model(tuned_model, plot="feature")

# Precision-Recall curve
plot_model(tuned_model, plot="pr")

PyCaret wraps Yellowbrick and Matplotlib to generate publication-ready charts. If you want to save any plot as a PNG, just pass save=True.

Step 6: Interpret the Model with SHAP

# SHAP summary plot (for tree-based models)
interpret_model(tuned_model)

# SHAP correlation plot for a specific feature
interpret_model(tuned_model, plot="correlation", feature="Glucose")

The interpret_model() function uses SHAP values under the hood. Each dot on the summary plot represents a sample, positioned by its SHAP value (its impact on the prediction). Red means a high feature value, blue means low. In the diabetes dataset, you'll clearly see that high glucose pushes predictions strongly toward positive diagnosis — which makes medical sense.

Step 7: Finalize and Save

# Retrain on the full dataset (including the holdout set)
final_model = finalize_model(tuned_model)

# Save the complete pipeline (preprocessing + model) as a pickle file
save_model(final_model, "diabetes_classifier_pipeline")

Important: After calling finalize_model(), don't evaluate the model on the holdout set — it was used for training. Always evaluate before finalizing. I've seen this mistake in production codebases more than once, and the resulting inflated metrics can be seriously misleading.

Regression Example: Predicting Insurance Charges

The workflow for regression is almost identical. You just import from the regression module instead, and PyCaret takes care of the rest.

Complete Regression Workflow

from pycaret.datasets import get_data
from pycaret.regression import *

# Load the insurance dataset
data = get_data("insurance")

# Initialize experiment
reg = setup(
    data=data,
    target="charges",
    session_id=42,
    fold=5,
    verbose=False
)

# Compare all regressors (ranked by R² by default)
best = compare_models(sort="RMSE", n_select=3)
print(best)

# Tune the top model
tuned = tune_model(best[0], optimize="RMSE", choose_better=True)

# Visualize residuals and prediction error
plot_model(tuned, plot="residuals")
plot_model(tuned, plot="error")
plot_model(tuned, plot="feature")

# Finalize and save
final = finalize_model(tuned)
save_model(final, "insurance_regressor_pipeline")

PyCaret's regression module supports over 25 algorithms out of the box, including linear models (Ridge, Lasso, ElasticNet), tree ensembles (Random Forest, Extra Trees), boosting methods (XGBoost, LightGBM, CatBoost), and others like KNN Regressor and Bayesian Ridge. That's a lot of ground to cover in just a few lines of code.

The OOP API: A Cleaner Way to Work

PyCaret 3 introduced an object-oriented API that avoids polluting the global namespace. If you're running multiple experiments or integrating PyCaret into a larger application, this is the way to go.

from pycaret.classification import ClassificationExperiment
from pycaret.datasets import get_data

data = get_data("diabetes")

# Create an experiment object
exp = ClassificationExperiment()
exp.setup(data=data, target="Class variable", session_id=42)

# Compare models
top_models = exp.compare_models(n_select=5)

# Tune each top model
tuned_models = [
    exp.tune_model(m, choose_better=True)
    for m in top_models
]

# Get the single best model across all tuned candidates
best = exp.automl(optimize="AUC")
print(best)

# Finalize
final = exp.finalize_model(best)
exp.save_model(final, "best_diabetes_model")

I personally prefer the OOP approach — it's cleaner, more predictable, and plays nicer with IDEs and type hints.

Advanced Features Worth Knowing

Ensemble Methods: Bagging, Boosting, and Stacking

# Bagging
bagged = ensemble_model(tuned_model, method="Bagging", n_estimators=10)

# Boosting
boosted = ensemble_model(tuned_model, method="Boosting", n_estimators=10)

# Stacking multiple models
stacked = stack_models(top3)

Blending Models

# Blend the top 3 models into a voting classifier
blended = blend_models(top3)

Custom Models

Any scikit-learn compatible estimator works inside PyCaret — so you're not limited to the built-in model list:

from sklearn.neural_network import MLPClassifier

# Create a custom model and include it in the PyCaret workflow
custom_mlp = create_model(MLPClassifier(hidden_layer_sizes=(100, 50),
                                        max_iter=500,
                                        random_state=42))

GPU Acceleration

# Enable GPU training (requires cuML >= 0.15)
clf = setup(data=data, target="Class variable", use_gpu=True)

GPU acceleration is supported for Logistic Regression, Ridge Classifier, Random Forest, KNN, SVM, Linear Regression, Ridge, and Lasso — but only when you have the RAPIDS cuML library installed. It won't magically speed up every algorithm.

Deploying a PyCaret Model to Production

Here's something I really appreciate about PyCaret: it stores the entire preprocessing pipeline alongside the trained model in a single pickle file. This eliminates training-serving skew — the same transformations applied during training get automatically applied during inference. No more "it works in my notebook but fails in production" headaches.

Loading and Predicting with a Saved Model

from pycaret.classification import load_model, predict_model
import pandas as pd

# Load the saved pipeline
pipeline = load_model("diabetes_classifier_pipeline")

# New patient data
new_patient = pd.DataFrame({
    "Pregnancies": [2],
    "Glucose": [148],
    "BloodPressure": [72],
    "SkinThickness": [35],
    "Insulin": [0],
    "BMI": [33.6],
    "DiabetesPedigreeFunction": [0.627],
    "Age": [50]
})

# Predict — preprocessing is applied automatically
result = predict_model(pipeline, data=new_patient)
print(result[["prediction_label", "prediction_score"]])

Deploying to Cloud Storage

# Deploy to AWS S3
deploy_model(
    final_model,
    model_name="diabetes-classifier-v1",
    platform="aws",
    authentication={"bucket": "my-ml-models-bucket"}
)

# Deploy to Google Cloud Storage
deploy_model(
    final_model,
    model_name="diabetes-classifier-v1",
    platform="gcp",
    authentication={"project": "my-gcp-project",
                    "bucket": "my-gcs-bucket"}
)

# Deploy to Azure Blob Storage
deploy_model(
    final_model,
    model_name="diabetes-classifier-v1",
    platform="azure",
    authentication={"container": "my-container"}
)

PyCaret vs. Scikit-Learn: When to Use Which

PyCaret wraps scikit-learn — it doesn't replace it. Knowing when each tool shines helps you pick the right one for the job.

Scenario Best Choice Why
Rapid prototyping and model selection PyCaret Compare 20+ models in one line, not 200 lines
Kaggle competitions and quick experiments PyCaret Fast iteration, built-in ensembling and stacking
Custom preprocessing logic scikit-learn Full control over every pipeline step
Production systems with strict latency needs scikit-learn No overhead from PyCaret's abstraction layer
Large-scale datasets (100M+ rows) scikit-learn + Dask/Spark PyCaret adds overhead; direct control scales better
Teaching ML concepts to beginners PyCaret Less boilerplate lets students focus on concepts
Deep learning or neural architectures Neither (use PyTorch/TensorFlow) PyCaret and scikit-learn are for classical ML

Common Pitfalls and How to Avoid Them

I've run into (or seen others run into) most of these at some point. Save yourself the debugging time:

  • Evaluating after finalize_model(): Once you call finalize_model(), the holdout set becomes part of the training data. Any evaluation after that point is meaningless. Always complete all evaluation, visualization, and interpretation before finalizing.
  • Skipping choose_better=True: Hyperparameter tuning can actually make a model worse. Always pass choose_better=True to tune_model() so PyCaret only accepts the tuned version if it genuinely improves the metric.
  • Forgetting to set session_id: Without session_id, your results aren't reproducible across runs. Always set it to a fixed integer — your future self will thank you.
  • Using PyCaret for very large datasets without profiling: PyCaret loads the full dataset into memory via pandas. For datasets exceeding available RAM, preprocess and sample the data first, or use the Fugue integration (pip install pycaret[parallel]) for distributed execution.
  • Dependency conflicts: PyCaret pins specific versions of scikit-learn, XGBoost, and other libraries. Always use a dedicated virtual environment — this isn't optional, it's essential.

Frequently Asked Questions

What is PyCaret and how does it differ from scikit-learn?

PyCaret is an open-source, low-code Python library that wraps scikit-learn, XGBoost, LightGBM, CatBoost, and other ML frameworks behind a unified API. While scikit-learn gives you fine-grained control over each pipeline step, PyCaret automates the repetitive parts — data preprocessing, model comparison, hyperparameter tuning, and deployment — letting you accomplish in 10 lines what would normally take 200. It builds on top of scikit-learn rather than replacing it.

Is PyCaret suitable for production use?

It depends on your production requirements. PyCaret saves the full preprocessing pipeline alongside the trained model as a single pickle file, which eliminates training-serving skew. For internal tools, batch prediction jobs, or moderate-traffic APIs, that's perfectly production-ready. For high-throughput, low-latency services, you might want to extract the underlying scikit-learn pipeline and serve it directly to remove PyCaret's abstraction overhead.

Can PyCaret handle large datasets?

PyCaret loads data into memory using pandas, so it works well with datasets that fit in RAM (typically up to a few million rows, depending on column count and data types). For larger datasets, PyCaret integrates with Fugue (pip install pycaret[parallel]) to distribute compare_models() across Spark, Dask, or Ray clusters. You can also sample your data for model selection and then train the final model on the full dataset using the underlying library directly.

Does PyCaret support GPU acceleration?

Yes. Pass use_gpu=True in the setup() function to enable GPU training for supported algorithms. This requires the RAPIDS cuML library (version 0.15 or later). GPU-accelerated algorithms include Logistic Regression, Ridge Classifier, Random Forest, KNN, SVM, Linear Regression, Ridge, and Lasso.

How do I use PyCaret with my own custom model?

Any scikit-learn compatible estimator (meaning it has fit() and predict() methods) can be passed directly to create_model(). PyCaret will evaluate it using the same cross-validation, metrics, and visualization tools it uses for built-in models. It's a great way to benchmark a custom algorithm against PyCaret's full model library.

About the Author Editorial Team

Our team of expert writers and editors.