Example PyCaret deployment
In this example we will use PyCaret to predict whether someone has diabetes, using the
PyCaret sample dataset diabetes
. PyCaret has some unique behavior when it comes
to creating deployment-ready models, which we'll focus on in this example.
First, in your notebook, import and log in to Modelbit:
import modelbit
mb = modelbit.login()
Now we'll ask PyCaret to make a model using the diabetes
sample dataset:
from pycaret.regression import *
from pycaret.datasets import get_data
diabetes = get_data("diabetes")
s = setup(diabetes, target = 'Class variable')
model = create_model('et')
With our model created, we need to save and re-load it so it can be pickled for deployment. PyCaret models are not pickle-able right after they've been trained, and the save/load process forces the model into a pickle-able state.
save_model(model, 'pycaret_example_model')
model = load_model("pycaret_example_model", verbose=False)
With our model re-loaded, we'll make a deployment function with a unit test. Notice that getting
the output of the PyCaret model requires calling predict_model
, and then fetching the value
from the Label
column in the returned dataframe:
import pandas as pd
def predict_with_pycaret(input_dict):
"""
Example pycaret predictor
>>> predict_with_pycaret({ "Number of times pregnant": 6, "Plasma glucose concentration a 2 hours in an oral glucose tolerance test": 148, "Diastolic blood pressure (mm Hg)": 72, "Triceps skin fold thickness (mm)": 35, "2-Hour serum insulin (mu U/ml)": 0, "Body mass index (weight in kg/(height in m)^2)": 33.6, "Diabetes pedigree function": 0.627, "Age (years)": 50 })
{ "has_diabetes": 1}
"""
df = pd.DataFrame.from_records([input_dict])
return { "has_diabetes": predict_model(model, data=df)[['Label']].iloc[0][0] }
We can now deploy our PyCaret deployment. Include numpy's version in the python_packages
as well, as it's an implicit dependency of PyCaret.
mb.deploy(predict_with_pycaret, python_packages=["pycaret==2.3.10", "numpy==1.20.0"])
Modelbit will package the PyCaret model and predict_with_pycaret
function into
a deployment that can then be called from REST, Snowflake, and Redshift.