Docs
  • PyCaret 3.0
  • GET STARTED
    • 💻Installation
    • 🚀Quickstart
    • ⭐Tutorials
    • 📶Modules
    • ⚙️Data Preprocessing
      • Data Preparation
      • Scale and Transform
      • Feature Engineering
      • Feature Selection
      • Other setup parameters
    • 💡Functions
      • Initialize
      • Train
      • Optimize
      • Analyze
      • Deploy
      • Others
  • LEARN PYCARET
    • 📖Blog
      • Announcing PyCaret 1.0
      • Announcing PyCaret 2.0
      • 5 things you dont know about PyCaret
      • Build and deploy your first machine learning web app
      • Build your own AutoML in Power BI using PyCaret
      • Deploy ML Pipeline on Google Kubernetes
      • Deploy PyCaret and Streamlit on AWS Fargate
      • Anomaly Detector in Power BI using PyCaret
      • Deploy ML App on Google Kubernetes
      • Deploy Machine Learning Pipeline on GKE
      • Deploy Machine Learning Pipeline on AWS Fargate
      • Deploy ML Pipeline on the cloud with Docker
      • Clustering Analysis in Power BI using PyCaret
      • Deploy PyCaret Models on edge with ONNX Runtime
      • GitHub is the best AutoML you will ever need
      • Deploy PyCaret and Streamlit on AWS Fargate
      • Easy MLOps with PyCaret and MLflow
      • Clustering Analysis in Power BI using PyCaret
      • Machine Learning in Alteryx with PyCaret
      • Machine Learning in KNIME with PyCaret
      • Machine Learning in SQL using PyCaret Part I
      • Machine Learning in Power BI using PyCaret
      • Machine Learning in Tableau with PyCaret
      • Multiple Time Series Forecasting with PyCaret
      • Predict Customer Churn using PyCaret
      • Predict Lead Score (the Right Way) Using PyCaret
      • NLP Text Classification in Python using PyCaret
      • Predict Lead Score (the Right Way) Using PyCaret
      • Predicting Crashes in Gold Prices Using PyCaret
      • Predicting Gold Prices Using Machine Learning
      • PyCaret 2.1 Feature Summary
      • Ship ML Models to SQL Server using PyCaret
      • Supercharge Your ML with PyCaret and Gradio
      • Time Series 101 - For beginners
      • Time Series Anomaly Detection with PyCaret
      • Time Series Forecasting with PyCaret Regression
      • Topic Modeling in Power BI using PyCaret
      • Write and train custom ML models using PyCaret
      • Build and deploy ML app with PyCaret and Streamlit
      • PyCaret 2.3.6 is Here! Learn What’s New?
    • 📺Videos
    • 🛩️Cheat sheet
    • ❓FAQs
    • 👩‍💻Examples
  • IMPORTANT LINKS
    • 🛠️Release Notes
    • ⚙️API Reference
    • 🙋 Discussions
    • 📤Issues
    • 👮 License
  • MEDIA
    • 💻Slack
    • 📺YouTube
    • 🔗LinkedIn
    • 😾GitHub
    • 🔅Stack Overflow
Powered by GitBook
On this page
  • Supercharge Your Machine Learning Experiments with PyCaret and Gradio
  • 👉 Introduction
  • 👉 PyCaret
  • 👉 Gradio
  • 👉 Installing PyCaret
  • 👉 Installing Gradio
  • 👉 Let’s get started
  • 👉 Initialize Setup
  • 👉 Compare Models
  • 👉 Gradio
  • Part I — Creating an internal function
  • Part II — Creating a UI with Gradio
  • Coming Soon!
  • You may also be interested in:
  • Important Links
  • Want to learn about a specific module?

Was this helpful?

  1. LEARN PYCARET
  2. Blog

Supercharge Your ML with PyCaret and Gradio

PreviousShip ML Models to SQL Server using PyCaretNextTime Series 101 - For beginners

Last updated 2 years ago

Was this helpful?

Supercharge Your Machine Learning Experiments with PyCaret and Gradio

A step-by-step tutorial to develop and interact with machine learning pipelines rapidly

Photo by Hunter Harritt on Unsplash

👉 Introduction

👉 PyCaret

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 be used to replace hundreds of lines of code 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.

👉 Gradio

Gradio is an open-source Python library for creating customizable UI components around your machine learning models. Gradio makes it easy for you to “play around” with your model in your browser by dragging and dropping in your own images, pasting your own text, recording your own voice, etc., and seeing what the model outputs.

Gradio is useful for:

  • Creating quick demos around your trained ML pipelines

  • Getting live feedback on model performance

  • Debugging your model interactively during development

👉 Installing PyCaret

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.

**# install slim version (default)
**pip install pycaret

**# install the full version**
pip install pycaret[full]

👉 Installing Gradio

You can install gradio from pip.

pip install gradio

👉 Let’s get started

**# load the iris dataset from pycaret repo**
from pycaret.datasets import get_data
data = get_data('iris')

👉 Initialize Setup

**# initialize setup**
from pycaret.classification import *
s = setup(data, target = 'species', session_id = 123)

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 all the four features (sepal_length, sepal_width, petal_length, and petal_width) are identified correctly as Numeric datatype. You can press enter to continue.

👉 Compare Models

This is the first step we recommend in the workflow of any supervised experiment in PyCaret. This function trains all the available models in the model library using default hyperparameters and evaluates performance metrics using cross-validation.

The output of this function is a table showing the mean cross-validated scores for all the models. The number of folds can be defined using the foldparameter (default = 10 folds). The table is sorted (highest to lowest) by the metric of choice which can be defined using the sortparameter (default = ‘Accuracy’).

best = compare_models(n_select = 15)
compare_model_results = pull()

n_select parameter in the setup function controls the return of trained models. In this case, I am setting it to 15, meaning return the top 15 models as a list. pull function in the second line stores the output of compare_models as pd.DataFrame .

len(best)
>>> 15

print(best[:5])

👉 Gradio

Now that we are done with the modeling process, let’s create a simple UI using Gradio to interact with our models. I will do it in two parts, first I will create a function that will use PyCaret’s predict_model functionality to generate and return predictions and the second part will be feeding that function into Gradio and designing a simple input form for interactivity.

Part I — Creating an internal function

The first two lines of the code take the input features and convert them into pandas DataFrame. Line 7 is creating a unique list of model names displayed in the compare_models output (this will be used as a dropdown in the UI). Line 8 selects the best model based on the index value of the list (which will be passed in through UI) and Line 9 uses the predict_model functionality of PyCaret to score the dataset.

Part II — Creating a UI with Gradio

Line 3 in the code below creates a dropdown for model names, Line 4–7 creates a slider for each of the input features and I have set the default value to the mean of each feature. Line 9 initiates a UI (in the notebook as well as on your local host so you can view it in the browser).

You can see this quick video here to see how easy it is to interact with your pipelines and query your models without writing hundreds of lines of code or developing a full-fledged front-end.

I hope that you will appreciate the ease of use and simplicity in PyCaret and Gradio. In less than 25 lines of code and few minutes of experimentation, I have trained and evaluated multiple models using PyCaret and developed a lightweight UI to interact with models in the Notebook.

Coming Soon!

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.

You may also be interested in:

Important Links

Want to learn about a specific module?

Click on the links below to see the documentation and working examples.

This tutorial is a step-by-step, beginner-friendly explanation of how you can integrate and , the two powerful open-source libraries in Python, and supercharge your machine learning experimentation within minutes.

This tutorial is a “hello world” example, I have used from UCI, which is a multiclassification problem where the goal is to predict the class of iris plants. The code given in this example can be reproduced on any other dataset, without any major modifications.

To learn more about PyCaret, check out their .

To learn more about Gradio, check out their .

The workflow for PyCaret and Gradio

PyCaret’s default installation is a slim version of pycaret which only installs hard dependencies that are .

When you install the full version of pycaret, all the optional dependencies as are also installed.

Sample rows from iris dataset
Output from setup — truncated for display

Common to all modules in PyCaret, the setup function is the first and the only mandatory step to start any machine learning experiment in PyCaret. Besides performing some basic processing tasks by default, PyCaret also offers a wide array of pre-processing features such as , , , and several key data preparatory steps such as , , , etc. To learn more about all the preprocessing functionalities in PyCaret, you can see this .

https://pycaret.org/preprocessing/
Output from compare_models
Output from print(best[:5])
Output from running Gradio interface

Next week I will be writing a tutorial on unsupervised anomaly detection on time-series data using . Please follow me on , , and to get more updates.

To hear more about PyCaret follow us on and .

Join us on our slack channel. Invite link .

📖
PyCaret
Gradio
Iris Dataset
GitHub
GitHub
listed here
listed here
scaling and transformation
feature engineering
feature selection
one-hot-encoding
missing values imputation
over-sampling/under-sampling
link
PyCaret Anomaly Detection Module
Medium
LinkedIn
Twitter
LinkedIn
Youtube
here
Build your own AutoML in Power BI using PyCaret 2.0
Deploy Machine Learning Pipeline on Azure using Docker
Deploy Machine Learning Pipeline on Google Kubernetes Engine
Deploy Machine Learning Pipeline on AWS Fargate
Build and deploy your first machine learning web app
Deploy PyCaret and Streamlit app using AWS Fargate serverless
Build and deploy machine learning web app using PyCaret and Streamlit
Deploy Machine Learning App built using Streamlit and PyCaret on GKE
Documentation
Blog
GitHub
StackOverflow
Install PyCaret
Notebook Tutorials
Contribute in PyCaret
Classification
Regression
Clustering
Anomaly Detection
Natural Language Processing
Association Rule Mining