Skip to content

Commit ea9e3bc

Browse files
committed
Pushing the docs to dev/ for branch: main, commit 150ff34e85f91a3a19a29dc4e19d50131fbb0251
1 parent c0f3784 commit ea9e3bc

File tree

1,310 files changed

+7178
-6013
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,310 files changed

+7178
-6013
lines changed

dev/.buildinfo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# Sphinx build info version 1
22
# This file hashes the configuration used when building these files. When it is not found, a full rebuild will be done.
3-
config: 2052ea0f8b88e58fe3db2fce6f371cc6
3+
config: 893649cf12a9e48d0f087abbe605ee51
44
tags: 645f666f9bcd5a90fca523b33c5a78b7
Binary file not shown.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"\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.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"
8+
]
9+
},
10+
{
11+
"cell_type": "markdown",
12+
"metadata": {},
13+
"source": [
14+
"## 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"
15+
]
16+
},
17+
{
18+
"cell_type": "code",
19+
"execution_count": null,
20+
"metadata": {
21+
"collapsed": false
22+
},
23+
"outputs": [],
24+
"source": [
25+
"# Author: Kushan <[email protected]>\n#\n# License: 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"
26+
]
27+
}
28+
],
29+
"metadata": {
30+
"kernelspec": {
31+
"display_name": "Python 3",
32+
"language": "python",
33+
"name": "python3"
34+
},
35+
"language_info": {
36+
"codemirror_mode": {
37+
"name": "ipython",
38+
"version": 3
39+
},
40+
"file_extension": ".py",
41+
"mimetype": "text/x-python",
42+
"name": "python",
43+
"nbconvert_exporter": "python",
44+
"pygments_lexer": "ipython3",
45+
"version": "3.9.16"
46+
}
47+
},
48+
"nbformat": 4,
49+
"nbformat_minor": 0
50+
}
Binary file not shown.
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
"""
2+
========================================
3+
`__sklearn_is_fitted__` as Developer API
4+
========================================
5+
6+
The `__sklearn_is_fitted__` method is a convention used in scikit-learn for
7+
checking whether an estimator object has been fitted or not. This method is
8+
typically implemented in custom estimator classes that are built on top of
9+
scikit-learn's base classes like `BaseEstimator` or its subclasses.
10+
11+
Developers should use :func:`~sklearn.sklearn.utils.validation.check_is_fitted`
12+
at the beginning of all methods except `fit`. If they need to customize or
13+
speed-up the check, they can implement the `__sklearn_is_fitted__` method as
14+
shown below.
15+
16+
In this example the custom estimator showcases the usage of the
17+
`__sklearn_is_fitted__` method and the `check_is_fitted` utility function
18+
as developer APIs. The `__sklearn_is_fitted__` method checks fitted status
19+
by verifying the presence of the `_is_fitted` attribute.
20+
"""
21+
22+
# %%
23+
# An example custom estimator implementing a simple classifier
24+
# ------------------------------------------------------------
25+
# This code snippet defines a custom estimator class called `CustomEstimator`
26+
# that extends both the `BaseEstimator` and `ClassifierMixin` classes from
27+
# scikit-learn and showcases the usage of the `__sklearn_is_fitted__` method
28+
# and the `check_is_fitted` utility function.
29+
30+
# Author: Kushan <[email protected]>
31+
#
32+
# License: BSD 3 clause
33+
34+
from sklearn.base import BaseEstimator, ClassifierMixin
35+
from sklearn.utils.validation import check_is_fitted
36+
37+
38+
class CustomEstimator(BaseEstimator, ClassifierMixin):
39+
def __init__(self, parameter=1):
40+
self.parameter = parameter
41+
42+
def fit(self, X, y):
43+
"""
44+
Fit the estimator to the training data.
45+
"""
46+
self.classes_ = sorted(set(y))
47+
# Custom attribute to track if the estimator is fitted
48+
self._is_fitted = True
49+
return self
50+
51+
def predict(self, X):
52+
"""
53+
Perform Predictions
54+
55+
If the estimator is not fitted, then raise NotFittedError
56+
"""
57+
check_is_fitted(self)
58+
# Perform prediction logic
59+
predictions = [self.classes_[0]] * len(X)
60+
return predictions
61+
62+
def score(self, X, y):
63+
"""
64+
Calculate Score
65+
66+
If the estimator is not fitted, then raise NotFittedError
67+
"""
68+
check_is_fitted(self)
69+
# Perform scoring logic
70+
return 0.5
71+
72+
def __sklearn_is_fitted__(self):
73+
"""
74+
Check fitted status and return a Boolean value.
75+
"""
76+
return hasattr(self, "_is_fitted") and self._is_fitted

dev/_downloads/scikit-learn-docs.zip

46.2 KB
Binary file not shown.

dev/_images/binder_badge_logo31.svg

Lines changed: 1 addition & 0 deletions
Lines changed: 3 additions & 0 deletions
-212 Bytes
-209 Bytes

0 commit comments

Comments
 (0)