check_estimator#

sklearn.utils.estimator_checks.check_estimator(estimator=None, generate_only=False, *, legacy: bool = True)[source]#

Check if estimator adheres to scikit-learn conventions.

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 Rolling your own estimator. Additional tests for classifiers, regressors, clustering or transformers will be run if the Estimator class inherits from the corresponding mixin from sklearn.base.

Setting generate_only=True returns a generator that yields (estimator, check) tuples where the check can be called independently from each other, i.e. check(estimator). This allows all checks to be run independently and report the checks that are failing.

scikit-learn provides a pytest specific decorator, parametrize_with_checks, making it easier to test multiple estimators.

Checks are categorised into the following groups:

Parameters:
estimatorestimator object

Estimator instance to check.

Added in version 1.1: Passing a class was deprecated in version 0.23, and support for classes was removed in 0.24.

generate_onlybool, default=False

When False, checks are evaluated when check_estimator is called. When True, check_estimator returns a generator that yields (estimator, check) tuples. The check is run by calling check(estimator).

Added in version 0.22.

legacybool, default=True

Whether to include legacy checks. Over time we remove checks from this category and move them into their specific category.

Added in version 1.6.

Returns:
checks_generatorgenerator

Generator that yields (estimator, check) tuples. Returned when generate_only=True.

See also

parametrize_with_checks

Pytest specific decorator for parametrizing estimator checks.

Examples

>>> from sklearn.utils.estimator_checks import check_estimator
>>> from sklearn.linear_model import LogisticRegression
>>> check_estimator(LogisticRegression(), generate_only=True)
<generator object ...>