.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/developing_estimators/sklearn_is_fitted.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. or to run this example in your browser via JupyterLite or Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_developing_estimators_sklearn_is_fitted.py: ======================================== `__sklearn_is_fitted__` as Developer API ======================================== The `__sklearn_is_fitted__` method is a convention used in scikit-learn for checking whether an estimator object has been fitted or not. This method is typically implemented in custom estimator classes that are built on top of scikit-learn's base classes like `BaseEstimator` or its subclasses. Developers should use :func:`~sklearn.utils.validation.check_is_fitted` at the beginning of all methods except `fit`. If they need to customize or speed-up the check, they can implement the `__sklearn_is_fitted__` method as shown below. In this example the custom estimator showcases the usage of the `__sklearn_is_fitted__` method and the `check_is_fitted` utility function as developer APIs. The `__sklearn_is_fitted__` method checks fitted status by verifying the presence of the `_is_fitted` attribute. .. GENERATED FROM PYTHON SOURCE LINES 23-29 An example custom estimator implementing a simple classifier ------------------------------------------------------------ This code snippet defines a custom estimator class called `CustomEstimator` that extends both the `BaseEstimator` and `ClassifierMixin` classes from scikit-learn and showcases the usage of the `__sklearn_is_fitted__` method and the `check_is_fitted` utility function. .. GENERATED FROM PYTHON SOURCE LINES 29-76 .. code-block:: Python # Authors: The scikit-learn developers # SPDX-License-Identifier: BSD-3-Clause from sklearn.base import BaseEstimator, ClassifierMixin from sklearn.utils.validation import check_is_fitted class CustomEstimator(BaseEstimator, ClassifierMixin): def __init__(self, parameter=1): self.parameter = parameter def fit(self, X, y): """ Fit the estimator to the training data. """ self.classes_ = sorted(set(y)) # Custom attribute to track if the estimator is fitted self._is_fitted = True return self def predict(self, X): """ Perform Predictions If the estimator is not fitted, then raise NotFittedError """ check_is_fitted(self) # Perform prediction logic predictions = [self.classes_[0]] * len(X) return predictions def score(self, X, y): """ Calculate Score If the estimator is not fitted, then raise NotFittedError """ check_is_fitted(self) # Perform scoring logic return 0.5 def __sklearn_is_fitted__(self): """ Check fitted status and return a Boolean value. """ return hasattr(self, "_is_fitted") and self._is_fitted .. _sphx_glr_download_auto_examples_developing_estimators_sklearn_is_fitted.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/scikit-learn/scikit-learn/1.6.X?urlpath=lab/tree/notebooks/auto_examples/developing_estimators/sklearn_is_fitted.ipynb :alt: Launch binder :width: 150 px .. container:: lite-badge .. image:: images/jupyterlite_badge_logo.svg :target: ../../lite/lab/index.html?path=auto_examples/developing_estimators/sklearn_is_fitted.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: sklearn_is_fitted.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: sklearn_is_fitted.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: sklearn_is_fitted.zip ` .. include:: sklearn_is_fitted.recommendations .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_