Perform is_fitted validation for estimator. Checks if the estimator is fitted by verifying the presence of fitted attributes (ending with a trailing underscore) and otherwise raises a :class:`~sklearn.exceptions.NotFittedError` with the given message. If an estimator does not set a
(estimator, attributes=None, *, msg=None, all_or_any=all)
| 1632 | |
| 1633 | |
| 1634 | def check_is_fitted(estimator, attributes=None, *, msg=None, all_or_any=all): |
| 1635 | """Perform is_fitted validation for estimator. |
| 1636 | |
| 1637 | Checks if the estimator is fitted by verifying the presence of |
| 1638 | fitted attributes (ending with a trailing underscore) and otherwise |
| 1639 | raises a :class:`~sklearn.exceptions.NotFittedError` with the given message. |
| 1640 | |
| 1641 | If an estimator does not set any attributes with a trailing underscore, it |
| 1642 | can define a ``__sklearn_is_fitted__`` method returning a boolean to |
| 1643 | specify if the estimator is fitted or not. See |
| 1644 | :ref:`sphx_glr_auto_examples_developing_estimators_sklearn_is_fitted.py` |
| 1645 | for an example on how to use the API. |
| 1646 | |
| 1647 | If no `attributes` are passed, this function will pass if an estimator is stateless. |
| 1648 | An estimator can indicate it's stateless by setting the `requires_fit` tag. See |
| 1649 | :ref:`estimator_tags` for more information. Note that the `requires_fit` tag |
| 1650 | is ignored if `attributes` are passed. |
| 1651 | |
| 1652 | Parameters |
| 1653 | ---------- |
| 1654 | estimator : estimator instance |
| 1655 | Estimator instance for which the check is performed. |
| 1656 | |
| 1657 | attributes : str, list or tuple of str, default=None |
| 1658 | Attribute name(s) given as string or a list/tuple of strings |
| 1659 | Eg.: ``["coef_", "estimator_", ...], "coef_"`` |
| 1660 | |
| 1661 | If `None`, `estimator` is considered fitted if there exist an |
| 1662 | attribute that ends with a underscore and does not start with double |
| 1663 | underscore. |
| 1664 | |
| 1665 | msg : str, default=None |
| 1666 | The default error message is, "This %(name)s instance is not fitted |
| 1667 | yet. Call 'fit' with appropriate arguments before using this |
| 1668 | estimator." |
| 1669 | |
| 1670 | For custom messages if "%(name)s" is present in the message string, |
| 1671 | it is substituted for the estimator name. |
| 1672 | |
| 1673 | Eg. : "Estimator, %(name)s, must be fitted before sparsifying". |
| 1674 | |
| 1675 | all_or_any : callable, {all, any}, default=all |
| 1676 | Specify whether all or any of the given attributes must exist. |
| 1677 | |
| 1678 | Raises |
| 1679 | ------ |
| 1680 | TypeError |
| 1681 | If the estimator is a class or not an estimator instance |
| 1682 | |
| 1683 | NotFittedError |
| 1684 | If the attributes are not found. |
| 1685 | |
| 1686 | Examples |
| 1687 | -------- |
| 1688 | >>> from sklearn.linear_model import LogisticRegression |
| 1689 | >>> from sklearn.utils.validation import check_is_fitted |
| 1690 | >>> from sklearn.exceptions import NotFittedError |
| 1691 | >>> lr = LogisticRegression() |
searching dependent graphs…