mlflow.statsmodels

The mlflow.statsmodels module provides an API for logging and loading statsmodels models. This module exports statsmodels models with the following flavors:

statsmodels (native) format

This is the main flavor that can be loaded back into statsmodels, which relies on pickle internally to serialize a model.

mlflow.pyfunc

Produced for use by generic pyfunc-based deployment tools and batch inference.

class mlflow.statsmodels.AutologHelpers[source]

Bases: object

should_autolog = True
mlflow.statsmodels.autolog(log_models=True, disable=False, exclusive=False, disable_for_unsupported_versions=False, silent=False)[source]

Note

Experimental: This method may change or be removed in a future release without warning.

Note

Autologging is known to be compatible with the following package versions: 0.11.1 <= statsmodels <= 0.12.2. Autologging may not succeed when used with package versions outside of this range.

Enables (or disables) and configures automatic logging from statsmodels to MLflow. Logs the following:

  • results metrics returned by method fit of any subclass of statsmodels.base.model.Model

  • trained model.

Parameters
  • log_models – If True, trained models are logged as MLflow model artifacts. If False, trained models are not logged. Input examples and model signatures, which are attributes of MLflow models, are also omitted when log_models is False.

  • disable – If True, disables the statsmodels autologging integration. If False, enables the statsmodels autologging integration.

  • exclusive – If True, autologged content is not logged to user-created fluent runs. If False, autologged content is logged to the active fluent run, which may be user-created.

  • disable_for_unsupported_versions – If True, disable autologging for versions of statsmodels that have not been tested against this version of the MLflow client or are incompatible.

  • silent – If True, suppress all event logs and warnings from MLflow during statsmodels autologging. If False, show all events and warnings during statsmodels autologging.

mlflow.statsmodels.get_default_conda_env()[source]
Returns

The default Conda environment for MLflow Models produced by calls to save_model() and log_model().

mlflow.statsmodels.load_model(model_uri)[source]

Load a statsmodels model from a local file or a run.

Parameters

model_uri

The location, in URI format, of the MLflow model. For example:

  • /Users/me/path/to/local/model

  • relative/path/to/local/model

  • s3://my_bucket/path/to/model

  • runs:/<mlflow_run_id>/run-relative/path/to/model

For more information about supported URI schemes, see Referencing Artifacts.

Returns

A statsmodels model (an instance of statsmodels.base.model.Results).

mlflow.statsmodels.log_model(statsmodels_model, artifact_path, conda_env=None, registered_model_name=None, remove_data: bool = False, signature: Optional[mlflow.models.signature.ModelSignature] = None, input_example: Optional[Union[pandas.core.frame.DataFrame, numpy.ndarray, dict, list]] = None, await_registration_for=300, **kwargs)[source]

Log a statsmodels model as an MLflow artifact for the current run.

Parameters
  • statsmodels_model – statsmodels model (an instance of statsmodels.base.model.Results) to be saved.

  • artifact_path – Run-relative artifact path.

  • conda_env

    Either a dictionary representation of a Conda environment or the path to a Conda environment yaml file. If provided, this describes the environment this model should be run in. At minimum, it should specify the dependencies contained in get_default_conda_env(). If None, the default get_default_conda_env() environment is added to the model. The following is an example dictionary representation of a Conda environment:

    {
        'name': 'mlflow-env',
        'channels': ['defaults'],
        'dependencies': [
            'python=3.7.0',
            'statsmodels=0.11.1'
        ]
    }
    

  • registered_model_name – (Experimental) If given, create a model version under registered_model_name, also creating a registered model if one with the given name does not exist.

  • remove_data – bool. If False (default), then the instance is pickled without changes. If True, then all arrays with length nobs are set to None before pickling. See the remove_data method. In some cases not all arrays will be set to None.

  • signature

    (Experimental) ModelSignature describes model input and output Schema. The model signature can be inferred from datasets with valid model input (e.g. the training dataset with target column omitted) and valid model output (e.g. model predictions generated on the training dataset), for example:

    from mlflow.models.signature import infer_signature
    train = df.drop_column("target_label")
    predictions = ... # compute model predictions
    signature = infer_signature(train, predictions)
    

  • input_example – (Experimental) Input example provides one or several instances of valid model input. The example can be used as a hint of what data to feed the model. The given example will be converted to a Pandas DataFrame and then serialized to json using the Pandas split-oriented format. Bytes are base64-encoded.

  • await_registration_for – Number of seconds to wait for the model version to finish being created and is in READY status. By default, the function waits for five minutes. Specify 0 or None to skip waiting.

mlflow.statsmodels.save_model(statsmodels_model, path, conda_env=None, mlflow_model=None, remove_data: bool = False, signature: Optional[mlflow.models.signature.ModelSignature] = None, input_example: Optional[Union[pandas.core.frame.DataFrame, numpy.ndarray, dict, list]] = None)[source]

Save a statsmodels model to a path on the local file system.

Parameters
  • statsmodels_model – statsmodels model (an instance of statsmodels.base.model.Results) to be saved.

  • path – Local path where the model is to be saved.

  • conda_env

    Either a dictionary representation of a Conda environment or the path to a Conda environment yaml file. If provided, this describes the environment this model should be run in. At minimum, it should specify the dependencies contained in get_default_conda_env(). If None, the default get_default_conda_env() environment is added to the model. The following is an example dictionary representation of a Conda environment:

    {
        'name': 'mlflow-env',
        'channels': ['defaults'],
        'dependencies': [
            'python=3.7.0',
            'statsmodels=0.11.1'
        ]
    }
    

  • mlflow_modelmlflow.models.Model this flavor is being added to.

  • remove_data – bool. If False (default), then the instance is pickled without changes. If True, then all arrays with length nobs are set to None before pickling. See the remove_data method. In some cases not all arrays will be set to None.

  • signature

    (Experimental) ModelSignature describes model input and output Schema. The model signature can be inferred from datasets with valid model input (e.g. the training dataset with target column omitted) and valid model output (e.g. model predictions generated on the training dataset), for example:

    from mlflow.models.signature import infer_signature
    train = df.drop_column("target_label")
    predictions = ... # compute model predictions
    signature = infer_signature(train, predictions)
    

  • input_example – (Experimental) Input example provides one or several instances of valid model input. The example can be used as a hint of what data to feed the model. The given example will be converted to a Pandas DataFrame and then serialized to json using the Pandas split-oriented format. Bytes are base64-encoded.