check_estimator#

sklearn.utils.estimator_checks.check_estimator(estimator=None, generate_only=False, *, legacy: bool = True, expected_failed_checks: dict[str, str] | None = None, on_skip: Literal['warn'] | None = 'warn', on_fail: Literal['raise', 'warn'] | None = 'raise', callback: Callable | None = None)[源代码]#

检查估计器是否遵守scikit-learn惯例。

This function will run an extensive test-suite for input validation, shapes, etc, making sure that the estimator complies with scikit-learn conventions as detailed in 滚动您自己的估计器. Additional tests for classifiers, regressors, clustering or transformers will be run if the Estimator class inherits from the corresponding mixin from sklearn.base.

scikit-learn还提供了一个pytest特定的装饰器, parametrize_with_checks ,使测试多个估计量变得更容易。

支票分为以下组:

  • API检查:一组检查,以确保API与scikit-learn兼容。请参阅https://scikit-learn.org/dev/developers/develop.html scikit-learn估计器的要求。

  • 遗产:一组支票,将逐渐分组为其他类别。

参数:
estimator估计器对象

估计要检查的实例。

generate_only布尔,默认=假

False ,在以下情况下评估检查 check_estimator 被称为。当 True , check_estimator 返回产生(估计器,检查)二元组的生成器。通过调用来运行检查 check(estimator) .

Added in version 0.22.

自 1.6 版本弃用: generate_only 将在1.8中删除。使用 estimator_checks_generator 而不是.

legacy布尔,默认=True

是否包括遗留检查。随着时间的推移,我们将支票从此类别中删除并将其移至其特定类别中。

Added in version 1.6.

expected_failed_checksdict,默认=无

形式为::

{
    "check_name": "this check is expected to fail because ...",
}

哪里 "check_name" 是支票的名称,并且 "my reason" 就是检查失败的原因

Added in version 1.6.

on_skip“warn”,无,默认=“warn”

此参数控制跳过检查时会发生什么。

  • “警告”:A SkipTestWarning 已记录并继续运行测试。

  • 无:不记录警告,继续运行测试。

Added in version 1.6.

on_fail{"raise", "warn"}, None, default="raise"

此参数控制检查失败时会发生什么。

  • “raise”:引发第一次失败检查引发的异常并中止正在运行的测试。这不包括预计会失败的测试。

  • “警告”:A EstimatorCheckFailedWarning 已记录并继续运行测试。

  • 无:没有引发异常,也没有记录警告。

注意如果 on_fail != "raise" ,即使检查失败,也不会引发异常。您需要检查返回结果 check_estimator 检查是否有检查失败。

Added in version 1.6.

callback可调用,或无,默认=无

将通过估计器和检查名称、异常(如果有)、检查状态(xfail、失败、跳过、通过)以及如果检查预计失败的预期失败原因来调用此回调。可调用的签名需要是::

def callback(
    estimator,
    check_name: str,
    exception: Exception,
    status: Literal["xfail", "failed", "skipped", "passed"],
    expected_to_fail: bool,
    expected_to_fail_reason: str,
)

callback 不能与一起提供 on_fail="raise" .

Added in version 1.6.

返回:
test_results列表

包含失败测试结果的字典列表,形式为::

{
    "estimator": estimator,
    "check_name": check_name,
    "exception": exception,
    "status": status (one of "xfail", "failed", "skipped", "passed"),
    "expected_to_fail": expected_to_fail,
    "expected_to_fail_reason": expected_to_fail_reason,
}
estimator_checks_generator发生器

产生(估计器、检查)二元组的生成器。时返回 generate_only=True .

自 1.6 版本弃用: generate_only 将在1.8中删除。使用 estimator_checks_generator 而不是.

提出:
例外

如果 on_fail="raise" ,将引发第一次失败检查引发的异常,并中止正在运行的测试。

注意如果 on_fail != "raise" ,即使检查失败,也不会引发异常。您需要检查返回结果 check_estimator 检查是否有检查失败。

参见

parametrize_with_checks

用于参数化估计器检查的Pytest特定装饰器。

estimator_checks_generator

产生(估计器、检查)二元组的生成器。

示例

>>> from sklearn.utils.estimator_checks import check_estimator
>>> from sklearn.linear_model import LogisticRegression
>>> check_estimator(LogisticRegression())
[...]