Statsmodels¶
Users can now use statsmodels with BentoML with the following API: load
, save
, and load_runner
as follow:
import bentoml
import pandas as pd
import statsmodels
from statsmodels.tsa.holtwinters import HoltWintersResults
from statsmodels.tsa.holtwinters import ExponentialSmoothing
def holt_model() -> "HoltWintersResults":
df: pd.DataFrame = pd.read_csv(
"https://raw.githubusercontent.com/jbrownlee/Datasets/master/shampoo.csv"
)
# Taking a test-train split of 80 %
train = df[0 : int(len(df) * 0.8)]
test = df[int(len(df) * 0.8) :]
# Pre-processing the Month field
train.Timestamp = pd.to_datetime(train.Month, format="%m-%d")
train.index = train.Timestamp
test.Timestamp = pd.to_datetime(test.Month, format="%m-%d")
test.index = test.Timestamp
# fitting the model based on optimal parameters
return ExponentialSmoothing(
np.asarray(train["Sales"]),
seasonal_periods=7,
trend="add",
seasonal="add",
).fit()
# `save` a given classifier and retrieve coresponding tag:
tag = bentoml.statsmodels.save("holtwinters", HoltWintersResults())
# retrieve metadata with `bentoml.models.get`:
metadata = bentoml.models.get(tag)
# `load` the model back in memory:
model = bentoml.statsmodels.load("holtwinters:latest")
# Run a given model under `Runner` abstraction with `load_runner`
input_data = pd.from_csv("/path/to/csv")
runner = bentoml.statsmodels.load_runner(tag)
runner.run(pd.DataFrame("/path/to/csv"))
Note
You can find more examples for statsmodels in our gallery repo.
- bentoml.statsmodels.save(name, model, *, labels=None, custom_objects=None, metadata=None)¶
Save a model instance to BentoML modelstore.
- Parameters
name (
str
) – Name for given model instance. This should pass Python identifier check.model (t.Any) – Instance of model to be saved.
labels (
Dict[str, str]
, optional, default toNone
) – user-defined labels for managing models, e.g. team=nlp, stage=devcustom_objects (
Dict[str, Any]]
, optional, default toNone
) – user-defined additional python objects to be saved alongside the model, e.g. a tokenizer instance, preprocessor function, model configuration jsonmetadata (
Dict[str, Any]
, optional, default toNone
) – Custom metadata for given model.model_store (
ModelStore
, default toBentoMLContainer.model_store
) – BentoML modelstore, provided by DI Container.
- Returns
A
tag
with a format name:version where name is the user-defined model’s name, and a generated version by BentoML.- Return type
Examples:
import bentoml import pandas as pd import statsmodels from statsmodels.tsa.holtwinters import HoltWintersResults from statsmodels.tsa.holtwinters import ExponentialSmoothing def holt_model() -> "HoltWintersResults": df: pd.DataFrame = pd.read_csv( "https://raw.githubusercontent.com/jbrownlee/Datasets/master/shampoo.csv" ) # Taking a test-train split of 80 % train = df[0 : int(len(df) * 0.8)] test = df[int(len(df) * 0.8) :] # Pre-processing the Month field train.Timestamp = pd.to_datetime(train.Month, format="%m-%d") train.index = train.Timestamp test.Timestamp = pd.to_datetime(test.Month, format="%m-%d") test.index = test.Timestamp # fitting the model based on optimal parameters return ExponentialSmoothing( np.asarray(train["Sales"]), seasonal_periods=7, trend="add", seasonal="add", ).fit() # `save` a given classifier and retrieve coresponding tag: tag = bentoml.statsmodels.save("holtwinters", HoltWintersResults())
- bentoml.statsmodels.load(tag, model_store=<simple_di.providers.SingletonFactory object>)¶
Load a model from BentoML local modelstore with given name.
- Parameters
tag (
Union[str, Tag]
) – Tag of a saved model in BentoML local modelstore.model_store (
ModelStore
, default toBentoMLContainer.model_store
) – BentoML modelstore, provided by DI Container.
- Returns
an instance of
statsmodels
that is unpickled from BentoML modelstore.- Return type
Any
Examples:
import bentoml model = bentoml.statsmodels.load("holtswinter")
- bentoml.statsmodels.load_runner(tag, *, predict_fn_name='predict', name=None)¶
Runner represents a unit of serving logic that can be scaled horizontally to maximize throughput. bentoml.statsmodels.load_runner implements a Runner class that wrap around a statsmodels instance, which optimize it for the BentoML runtime.
- Parameters
tag (
Union[str, Tag]
) – Tag of a saved model in BentoML local modelstore.predict_fn_name (
str
, default topredict
) – Options for inference functions
- Returns
Runner instances for
bentoml.statsmodels
model- Return type
Runner
Examples:
import bentoml import pandas as pd runner = bentoml.statsmodels.load_runner("holtswinter") runner.run_batch(pd.DataFrame("/path/to/data"))