mlflow.catboost

mlflow.catboost 模块提供了用于记录和加载 CatBoost 模型的 API。此模块使用以下 **Flavors** 导出 CatBoost 模型:

CatBoost(原生)格式

这是可以加载回 CatBoost 的主要 **Flavor**。

mlflow.pyfunc

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

mlflow.catboost.get_default_conda_env()[source]
返回

调用 save_model()log_model() 所生成的 MLflow Models 的默认 Conda 环境。

mlflow.catboost.get_default_pip_requirements()[source]
返回

此 **Flavor** 生成的 MLflow Models 的默认 pip 依赖项列表。调用 save_model()log_model() 会生成至少包含这些依赖项的 pip 环境。

mlflow.catboost.load_model(model_uri, dst_path=None)[source]

从本地文件或运行中加载 CatBoost 模型。

参数
  • model_uri

    MLflow 模型在 URI 格式中的位置。例如:

    • /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

    有关支持的 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.

返回

CatBoost 模型(CatBoostCatBoostClassifierCatBoostRankerCatBoostRegressor 的实例)

mlflow.catboost.log_model(cb_model, artifact_path: str | None = None, conda_env=None, code_paths=None, registered_model_name=None, 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]

将 CatBoost 模型记录为当前运行的 MLflow artifact。

参数
  • **cb_model** – 要保存的 CatBoost 模型(CatBoostCatBoostClassifierCatBoostRankerCatBoostRegressor 的实例)。

  • artifact_path – Deprecated. Use name instead.

  • conda_env

    Conda 环境的字典表示形式或本地文件系统上 conda 环境 yaml 文件的路径。如果提供了此项,它将描述模型应运行的环境。它至少应指定 get_default_conda_env() 中包含的依赖项。如果为 None,则添加到模型中的 pip 环境是通过 mlflow.models.infer_pip_requirements() 从当前软件环境中推断出来的。如果依赖项推断失败,它将回退到使用 get_default_pip_requirements。来自 conda_env 的 pip 依赖项将写入一个 pip requirements.txt 文件,完整的 conda 环境将写入 conda.yaml 文件。以下是 conda 环境的示例字典表示形式

    {
        "name": "mlflow-env",
        "channels": ["conda-forge"],
        "dependencies": [
            "python=3.8.15",
            {
                "pip": [
                    "catboost==x.y.z"
                ],
            },
        ],
    }
    

  • **code_paths** – Python 文件依赖项(或包含文件依赖项的目录)的本地文件系统路径列表。加载模型时,这些文件会预置到系统路径中。

  • registered_model_name – 如果提供,则在 registered_model_name 下创建一个模型版本,如果给定名称的注册模型不存在,也会创建该注册模型。

  • 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 依赖项字符串的可迭代对象(例如 ["catboost", "-r requirements.txt", "-c constraints.txt"])或本地文件系统上 pip 依赖项文件的字符串路径(例如 "requirements.txt")。如果提供,这描述了模型应运行的环境。如果为 None,则默认的依赖项列表由 mlflow.models.infer_pip_requirements() 从当前软件环境中推断出来。如果依赖项推断失败,它将回退到使用 get_default_pip_requirements。依赖项和约束条件都会被自动解析并写入 requirements.txtconstraints.txt 文件,并作为模型的一部分存储。依赖项也会被写入模型 conda 环境 (conda.yaml) 文件的 pip 部分。

  • extra_pip_requirements

    pip 依赖项字符串的可迭代对象(例如 ["pandas", "-r requirements.txt", "-c constraints.txt"])或本地文件系统上 pip 依赖项文件的字符串路径(例如 "requirements.txt")。如果提供,这描述了附加到根据用户当前软件环境自动生成的默认 pip 依赖项集上的额外 pip 依赖项。依赖项和约束条件都会被自动解析并写入 requirements.txtconstraints.txt 文件,并作为模型的一部分存储。依赖项也会被写入模型 conda 环境 (conda.yaml) 文件的 pip 部分。

    警告

    以下参数不能同时指定

    • conda_env

    • pip_requirements

    • extra_pip_requirements

    此示例演示了如何使用 pip_requirementsextra_pip_requirements 指定 pip requirements。

  • metadata – 传递给模型并存储在 MLmodel 文件中的自定义元数据字典。

  • name – 模型名称。

  • params – 要与模型一起记录的参数字典。

  • tags – 要与模型一起记录的标签字典。

  • model_type – 模型的类型。

  • step – 记录模型输出和指标的步骤

  • model_id – 模型的 ID。

  • **kwargs** – 传递给 CatBoost.save_model 方法的 kwargs。

返回

一个 ModelInfo 实例,其中包含已记录模型的元数据。

mlflow.catboost.save_model(cb_model, path, conda_env=None, code_paths=None, mlflow_model=None, 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, **kwargs)[source]

将 CatBoost 模型保存到本地文件系统的路径。

参数
  • **cb_model** – 要保存的 CatBoost 模型(CatBoostCatBoostClassifierCatBoostRankerCatBoostRegressor 的实例)。

  • path – 要保存模型的本地路径。

  • conda_env

    Conda 环境的字典表示形式或本地文件系统上 conda 环境 yaml 文件的路径。如果提供了此项,它将描述模型应运行的环境。它至少应指定 get_default_conda_env() 中包含的依赖项。如果为 None,则添加到模型中的 pip 环境是通过 mlflow.models.infer_pip_requirements() 从当前软件环境中推断出来的。如果依赖项推断失败,它将回退到使用 get_default_pip_requirements。来自 conda_env 的 pip 依赖项将写入一个 pip requirements.txt 文件,完整的 conda 环境将写入 conda.yaml 文件。以下是 conda 环境的示例字典表示形式

    {
        "name": "mlflow-env",
        "channels": ["conda-forge"],
        "dependencies": [
            "python=3.8.15",
            {
                "pip": [
                    "catboost==x.y.z"
                ],
            },
        ],
    }
    

  • **code_paths** – Python 文件依赖项(或包含文件依赖项的目录)的本地文件系统路径列表。加载模型时,这些文件会预置到系统路径中。

  • mlflow_model – 要添加此 flavor 的 mlflow.models.Model

  • 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 依赖项字符串的可迭代对象(例如 ["catboost", "-r requirements.txt", "-c constraints.txt"])或本地文件系统上 pip 依赖项文件的字符串路径(例如 "requirements.txt")。如果提供,这描述了模型应运行的环境。如果为 None,则默认的依赖项列表由 mlflow.models.infer_pip_requirements() 从当前软件环境中推断出来。如果依赖项推断失败,它将回退到使用 get_default_pip_requirements。依赖项和约束条件都会被自动解析并写入 requirements.txtconstraints.txt 文件,并作为模型的一部分存储。依赖项也会被写入模型 conda 环境 (conda.yaml) 文件的 pip 部分。

  • extra_pip_requirements

    pip 依赖项字符串的可迭代对象(例如 ["pandas", "-r requirements.txt", "-c constraints.txt"])或本地文件系统上 pip 依赖项文件的字符串路径(例如 "requirements.txt")。如果提供,这描述了附加到根据用户当前软件环境自动生成的默认 pip 依赖项集上的额外 pip 依赖项。依赖项和约束条件都会被自动解析并写入 requirements.txtconstraints.txt 文件,并作为模型的一部分存储。依赖项也会被写入模型 conda 环境 (conda.yaml) 文件的 pip 部分。

    警告

    以下参数不能同时指定

    • conda_env

    • pip_requirements

    • extra_pip_requirements

    此示例演示了如何使用 pip_requirementsextra_pip_requirements 指定 pip requirements。

  • metadata – 传递给模型并存储在 MLmodel 文件中的自定义元数据字典。

  • **kwargs** – 传递给 CatBoost.save_model 方法的 kwargs。