{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# `__sklearn_is_fitted__` as Developer API\n\nThe `__sklearn_is_fitted__` method is a convention used in scikit-learn for\nchecking whether an estimator object has been fitted or not. This method is\ntypically implemented in custom estimator classes that are built on top of\nscikit-learn's base classes like `BaseEstimator` or its subclasses.\n\nDevelopers should use :func:`~sklearn.utils.validation.check_is_fitted`\nat the beginning of all methods except `fit`. If they need to customize or\nspeed-up the check, they can implement the `__sklearn_is_fitted__` method as\nshown below.\n\nIn this example the custom estimator showcases the usage of the\n`__sklearn_is_fitted__` method and the `check_is_fitted` utility function\nas developer APIs. The `__sklearn_is_fitted__` method checks fitted status\nby verifying the presence of the `_is_fitted` attribute.\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## An example custom estimator implementing a simple classifier\nThis code snippet defines a custom estimator class called `CustomEstimator`\nthat extends both the `BaseEstimator` and `ClassifierMixin` classes from\nscikit-learn and showcases the usage of the `__sklearn_is_fitted__` method\nand the `check_is_fitted` utility function.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Authors: The scikit-learn developers\n# SPDX-License-Identifier: BSD-3-Clause\n\nfrom sklearn.base import BaseEstimator, ClassifierMixin\nfrom sklearn.utils.validation import check_is_fitted\n\n\nclass CustomEstimator(BaseEstimator, ClassifierMixin):\n    def __init__(self, parameter=1):\n        self.parameter = parameter\n\n    def fit(self, X, y):\n        \"\"\"\n        Fit the estimator to the training data.\n        \"\"\"\n        self.classes_ = sorted(set(y))\n        # Custom attribute to track if the estimator is fitted\n        self._is_fitted = True\n        return self\n\n    def predict(self, X):\n        \"\"\"\n        Perform Predictions\n\n        If the estimator is not fitted, then raise NotFittedError\n        \"\"\"\n        check_is_fitted(self)\n        # Perform prediction logic\n        predictions = [self.classes_[0]] * len(X)\n        return predictions\n\n    def score(self, X, y):\n        \"\"\"\n        Calculate Score\n\n        If the estimator is not fitted, then raise NotFittedError\n        \"\"\"\n        check_is_fitted(self)\n        # Perform scoring logic\n        return 0.5\n\n    def __sklearn_is_fitted__(self):\n        \"\"\"\n        Check fitted status and return a Boolean value.\n        \"\"\"\n        return hasattr(self, \"_is_fitted\") and self._is_fitted"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.11.14"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}