parametrize_with_checks#
- sklearn.utils.estimator_checks.parametrize_with_checks(estimators, *, legacy: bool = True, expected_failed_checks: Callable | None = None, xfail_strict: bool | None = None)[源代码]#
用于参数化估算器检查的 pytest 特定装饰器。
检查分为以下几类
API 检查:一组检查,用于确保与 scikit-learn 的 API 兼容性。请参阅 https://scikit-learn.cn/dev/developers/develop.html,这是 scikit-learn 估计器的要求。
legacy:一组检查,将逐渐归入其他类别。
每个检查的
id设置为估计器和检查名称及其关键字参数的 pprint 版本。这允许使用pytest -k来指定要运行的测试pytest test_check_estimators.py -k check_estimators_fit_returns_self
- 参数:
- estimators估计器实例列表
要生成检查的估计器。
版本 0.24 中的变化: 在版本 0.23 中,传递类已被弃用,并在 0.24 中删除了对类的支持。请传递实例。
0.24 版本新增。
- legacybool, default=True
是否包含遗留检查。随着时间的推移,我们会从这个类别中删除检查,并将它们移动到特定的类别中。
版本 1.6 中新增。
- expected_failed_checks可调用对象,默认为 None
一个可调用对象,它将估计器作为输入,并返回以下形式的字典
{ "check_name": "my reason", }
其中
"check_name"是检查的名称,"my reason"是检查失败的原因。如果检查失败,这些测试将被标记为 xfail。版本 1.6 中新增。
- xfail_strictbool, 默认为 None
是否以 xfail 严格模式运行检查。如果为 True,则预期失败但实际通过的检查将导致测试失败。如果为 False,意外通过的测试将被标记为 xpass。如果为 None,则使用默认的 pytest 行为。
1.8 版本新增。
- 返回:
- decorator
pytest.mark.parametrize
- decorator
另请参阅
check_estimator检查估算器是否符合 scikit-learn 约定。
示例
>>> from sklearn.utils.estimator_checks import parametrize_with_checks >>> from sklearn.linear_model import LogisticRegression >>> from sklearn.tree import DecisionTreeRegressor
>>> @parametrize_with_checks([LogisticRegression(), ... DecisionTreeRegressor()]) ... def test_sklearn_compatible_estimator(estimator, check): ... check(estimator)