mlflow.statsmodels
mlflow.statsmodels 模块提供了用于记录和加载 statsmodels 模型的 API。该模块以以下格式导出 statsmodels 模型:
- statsmodels (原生) 格式
这是可以加载回 statsmodels 的主要格式,它内部依赖 pickle 来序列化模型。
mlflow.pyfuncProduced for use by generic pyfunc-based deployment tools and batch inference.
- class mlflow.statsmodels.AutologHelpers[source]
Bases:
object
- mlflow.statsmodels.autolog(log_models=True, log_datasets=True, disable=False, exclusive=False, disable_for_unsupported_versions=False, silent=False, registered_model_name=None, extra_tags=None)[source]
注意
Autologging 已知兼容以下软件包版本:
0.14.1<=statsmodels<=0.14.6。使用此范围之外的软件包版本进行 autologging 可能会失败。启用(或禁用)并配置从 statsmodels 到 MLflow 的自动记录。记录以下内容:
statsmodels.base.model.Model 的任何子类的 fit 方法返回的允许列表指标,允许列表指标包括:aic, bic, centered_tss, condition_number, df_model, df_resid, ess, f_pvalue, fvalue, llf, mse_model, mse_resid, mse_total, rsquared, rsquared_adj, scale, ssr, uncentered_tss
训练好的模型。
显示模型摘要的 HTML 工件。
- 参数
log_models – 如果为
True,则训练好的模型将作为 MLflow 模型工件进行记录。如果为False,则不记录训练好的模型。输入样本和模型签名(MLflow 模型的属性)在log_models为False时也会被省略。log_datasets – 如果为
True,则数据集信息将被记录到 MLflow Tracking。如果为False,则不记录数据集信息。disable – 如果为
True,则禁用 statsmodels autologging 集成。如果为False,则启用 statsmodels autologging 集成。exclusive – 如果为
True,则自动记录的内容不会记录到用户创建的流畅运行中。如果为False,则自动记录的内容将记录到活动的流畅运行中,该运行可能是用户创建的。disable_for_unsupported_versions – 如果为
True,则禁用与此版本 MLflow 客户端未经测试或不兼容的 statsmodels 版本的 autologging。silent – 如果为
True,则在 statsmodels autologging 期间抑制 MLflow 的所有事件日志和警告。如果为False,则在 statsmodels autologging 期间显示所有事件和警告。registered_model_name – If given, each time a model is trained, it is registered as a new model version of the registered model with this name. The registered model is created if it does not already exist.
extra_tags – 要为自动日志记录创建的每个托管运行设置的额外标签的字典。
- mlflow.statsmodels.get_default_conda_env()[source]
- 返回
通过调用
save_model()和log_model()生成的 MLflow Models 的默认 Conda 环境。
- mlflow.statsmodels.get_default_pip_requirements()[source]
- 返回
此格式生成的 MLflow Models 的默认 pip requirements 列表。调用
save_model()和log_model()会生成一个 pip 环境,该环境至少包含这些 requirements。
- mlflow.statsmodels.load_model(model_uri, dst_path=None)[source]
从本地文件或 run 加载 statsmodels 模型。
- 参数
model_uri –
MLflow 模型在 URI 格式中的位置。例如:
/Users/me/path/to/local/modelrelative/path/to/local/models3://my_bucket/path/to/modelruns:/<mlflow_run_id>/run-relative/path/to/model
有关支持的 URI 方案的更多信息,请参阅 引用 Artifacts。
dst_path – The local filesystem path to which to download the model artifact. This directory must already exist. If unspecified, a local output path will be created.
- 返回
statsmodels 模型(statsmodels.base.model.Results 的实例)。
- mlflow.statsmodels.log_model(statsmodels_model, artifact_path: str | None = None, conda_env=None, code_paths=None, registered_model_name=None, remove_data: bool = False, signature: mlflow.models.signature.ModelSignature = None, input_example: Union[pandas.core.frame.DataFrame, numpy.ndarray, dict, list, csr_matrix, csc_matrix, str, bytes, tuple] = None, await_registration_for=300, pip_requirements=None, extra_pip_requirements=None, metadata=None, name: str | None = None, params: dict[str, typing.Any] | None = None, tags: dict[str, typing.Any] | None = None, model_type: str | None = None, step: int = 0, model_id: str | None = None, **kwargs)[source]
将 statsmodels 模型记录为当前 run 的 MLflow 工件。
- 参数
statsmodels_model – 要保存的 statsmodels 模型(statsmodels.base.model.Results 的实例)。
artifact_path – Deprecated. Use name instead.
conda_env –
Conda 环境的字典表示或 conda 环境 yaml 文件的路径。如果提供,它将描述模型应运行的环境。至少,它应该指定 get_default_conda_env() 中包含的依赖项。如果为
None,则将一个由mlflow.models.infer_pip_requirements()推断的 pip requirements 的 conda 环境添加到模型中。如果 requirement 推断失败,它将回退到使用 get_default_pip_requirements。来自conda_env的 pip requirements 被写入到 piprequirements.txt文件,完整的 conda 环境被写入到conda.yaml。以下是 conda 环境的字典表示的 *示例*。{ "name": "mlflow-env", "channels": ["conda-forge"], "dependencies": [ "python=3.8.15", { "pip": [ "statsmodels==x.y.z" ], }, ], }
code_paths –
A list of local filesystem paths to Python file dependencies (or directories containing file dependencies). These files are prepended to the system path when the model is loaded. Files declared as dependencies for a given model should have relative imports declared from a common root path if multiple files are defined with import dependencies between them to avoid import errors when loading the model.
For a detailed explanation of
code_pathsfunctionality, recommended usage patterns and limitations, see the code_paths usage guide.registered_model_name – 如果提供,则在
registered_model_name下创建一个模型版本,如果给定名称的注册模型不存在,也会创建该注册模型。remove_data – bool。如果为 False(默认值),则实例在不更改的情况下被 pickling。如果为 True,则所有长度为 nobs 的数组都设置为 None,然后进行 pickling。请参阅 remove_data 方法。在某些情况下,并非所有数组都会设置为 None。
signature –
一个
ModelSignature类的实例,描述模型的输入和输出。如果未指定但提供了input_example,则将根据提供的输入示例和模型自动推断签名。要在使用输入示例时禁用自动签名推断,请将signature设置为False。要手动推断模型签名,请在具有有效模型输入的 数据集上调用infer_signature(),例如训练数据集(省略目标列),以及有效的模型输出,例如在训练数据集上进行的模型预测,例如:from mlflow.models import infer_signature train = df.drop_column("target_label") predictions = ... # compute model predictions signature = infer_signature(train, predictions)
input_example – 一个或多个有效的模型输入实例。输入示例用作要馈送给模型的数据的提示。它将被转换为 Pandas DataFrame,然后使用 Pandas 的面向拆分(split-oriented)格式序列化为 json,或者转换为 numpy 数组,其中示例将通过转换为列表来序列化为 json。字节将进行 base64 编码。当
signature参数为None时,输入示例用于推断模型签名。await_registration_for – 等待模型版本完成创建并处于
READY状态的秒数。默认情况下,函数等待五分钟。指定 0 或 None 可跳过等待。pip_requirements – pip requirement 字符串的可迭代对象(例如
["statsmodels", "-r requirements.txt", "-c constraints.txt"])或本地文件系统上的 pip requirements 文件的字符串路径(例如"requirements.txt")。如果提供,它将描述模型应运行的环境。如果为None,则由用户当前软件环境推断的默认 requirements 列表由mlflow.models.infer_pip_requirements()推断。如果 requirement 推断失败,它将回退到使用 get_default_pip_requirements。requirements 和 constraints 都会被自动解析并分别写入到requirements.txt和constraints.txt文件,并作为模型的一部分存储。requirements 也会被写入到模型conda.yaml文件的pip部分。extra_pip_requirements –
pip requirement 字符串的可迭代对象(例如
["pandas", "-r requirements.txt", "-c constraints.txt"])或本地文件系统上的 pip requirements 文件的字符串路径(例如"requirements.txt")。如果提供,它将描述附加到根据用户当前软件环境自动生成的默认 pip requirements 集的附加 pip requirements。requirements 和 constraints 都会被自动解析并分别写入到requirements.txt和constraints.txt文件,并作为模型的一部分存储。requirements 也会被写入到模型conda.yaml文件的pip部分。警告
以下参数不能同时指定
conda_envpip_requirementsextra_pip_requirements
此示例演示了如何使用
pip_requirements和extra_pip_requirements指定 pip requirements。metadata – 传递给模型并存储在 MLmodel 文件中的自定义元数据字典。
name – 模型名称。
params – 要与模型一起记录的参数字典。
tags – 要与模型一起记录的标签字典。
model_type – 模型的类型。
step – 记录模型输出和指标的步骤
model_id – 模型的 ID。
kwargs – 传递给
mlflow.models.Model.log的额外 kwargs。
- 返回
一个
ModelInfo实例,其中包含已记录模型的元数据。
- mlflow.statsmodels.save_model(statsmodels_model, path, conda_env=None, code_paths=None, mlflow_model=None, remove_data: bool = False, signature: mlflow.models.signature.ModelSignature = None, input_example: Union[pandas.core.frame.DataFrame, numpy.ndarray, dict, list, csr_matrix, csc_matrix, str, bytes, tuple] = None, pip_requirements=None, extra_pip_requirements=None, metadata=None)[source]
将 statsmodels 模型保存到本地文件系统上的路径。
- 参数
statsmodels_model – 要保存的 statsmodels 模型(statsmodels.base.model.Results 的实例)。
path – 要保存模型的本地路径。
conda_env –
Conda 环境的字典表示或 conda 环境 yaml 文件的路径。如果提供,它将描述模型应运行的环境。至少,它应该指定 get_default_conda_env() 中包含的依赖项。如果为
None,则将一个由mlflow.models.infer_pip_requirements()推断的 pip requirements 的 conda 环境添加到模型中。如果 requirement 推断失败,它将回退到使用 get_default_pip_requirements。来自conda_env的 pip requirements 被写入到 piprequirements.txt文件,完整的 conda 环境被写入到conda.yaml。以下是 conda 环境的字典表示的 *示例*。{ "name": "mlflow-env", "channels": ["conda-forge"], "dependencies": [ "python=3.8.15", { "pip": [ "statsmodels==x.y.z" ], }, ], }
code_paths –
A list of local filesystem paths to Python file dependencies (or directories containing file dependencies). These files are prepended to the system path when the model is loaded. Files declared as dependencies for a given model should have relative imports declared from a common root path if multiple files are defined with import dependencies between them to avoid import errors when loading the model.
For a detailed explanation of
code_pathsfunctionality, recommended usage patterns and limitations, see the code_paths usage guide.mlflow_model – 要添加此 flavor 的
mlflow.models.Model。remove_data – bool。如果为 False(默认值),则实例在不更改的情况下被 pickling。如果为 True,则所有长度为 nobs 的数组都设置为 None,然后进行 pickling。请参阅 remove_data 方法。在某些情况下,并非所有数组都会设置为 None。
signature –
一个
ModelSignature类的实例,描述模型的输入和输出。如果未指定但提供了input_example,则将根据提供的输入示例和模型自动推断签名。要在使用输入示例时禁用自动签名推断,请将signature设置为False。要手动推断模型签名,请在具有有效模型输入的 数据集上调用infer_signature(),例如训练数据集(省略目标列),以及有效的模型输出,例如在训练数据集上进行的模型预测,例如:from mlflow.models import infer_signature train = df.drop_column("target_label") predictions = ... # compute model predictions signature = infer_signature(train, predictions)
input_example – 一个或多个有效的模型输入实例。输入示例用作要馈送给模型的数据的提示。它将被转换为 Pandas DataFrame,然后使用 Pandas 的面向拆分(split-oriented)格式序列化为 json,或者转换为 numpy 数组,其中示例将通过转换为列表来序列化为 json。字节将进行 base64 编码。当
signature参数为None时,输入示例用于推断模型签名。pip_requirements – pip requirement 字符串的可迭代对象(例如
["statsmodels", "-r requirements.txt", "-c constraints.txt"])或本地文件系统上的 pip requirements 文件的字符串路径(例如"requirements.txt")。如果提供,它将描述模型应运行的环境。如果为None,则由用户当前软件环境推断的默认 requirements 列表由mlflow.models.infer_pip_requirements()推断。如果 requirement 推断失败,它将回退到使用 get_default_pip_requirements。requirements 和 constraints 都会被自动解析并分别写入到requirements.txt和constraints.txt文件,并作为模型的一部分存储。requirements 也会被写入到模型conda.yaml文件的pip部分。extra_pip_requirements –
pip requirement 字符串的可迭代对象(例如
["pandas", "-r requirements.txt", "-c constraints.txt"])或本地文件系统上的 pip requirements 文件的字符串路径(例如"requirements.txt")。如果提供,它将描述附加到根据用户当前软件环境自动生成的默认 pip requirements 集的附加 pip requirements。requirements 和 constraints 都会被自动解析并分别写入到requirements.txt和constraints.txt文件,并作为模型的一部分存储。requirements 也会被写入到模型conda.yaml文件的pip部分。警告
以下参数不能同时指定
conda_envpip_requirementsextra_pip_requirements
此示例演示了如何使用
pip_requirements和extra_pip_requirements指定 pip requirements。metadata – 传递给模型并存储在 MLmodel 文件中的自定义元数据字典。