Skip to content

Commit 458c558

Browse files
rcap107ines.ibnukhseinGaelVaroquauxInes1999LilianBoulard
authored
ENH improve visual HTML estimator representation (scikit-learn#26616)
Co-authored-by: ines.ibnukhsein <[email protected]> Co-authored-by: Gael Varoquaux <[email protected]> Co-authored-by: Ines <[email protected]> Co-authored-by: Lilian <[email protected]> Co-authored-by: Olivier Grisel <[email protected]> Co-authored-by: Loïc Estève <[email protected]> Co-authored-by: Guillaume Lemaitre <[email protected]> Co-authored-by: Tim Head <[email protected]>
1 parent e9c74a3 commit 458c558

File tree

7 files changed

+851
-227
lines changed

7 files changed

+851
-227
lines changed

doc/developers/develop.rst

+34
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,40 @@ method taking no input and returning a boolean. If this method exists,
723723
See :ref:`sphx_glr_auto_examples_developing_estimators_sklearn_is_fitted.py`
724724
for an example on how to use the API.
725725

726+
Developer API for HTML representation
727+
=====================================
728+
729+
.. warning::
730+
731+
The HTML representation API is experimental and the API is subject to change.
732+
733+
Estimators inheriting from :class:`~sklearn.base.BaseEstimator` display
734+
a HTML representation of themselves in interactive programming
735+
environments such as Jupyter notebooks. For instance, we can display this HTML
736+
diagram::
737+
738+
from sklearn.base import BaseEstimator
739+
740+
BaseEstimator()
741+
742+
The raw HTML representation is obtained by invoking the function
743+
:func:`~sklearn.utils.estimator_html_repr` on an estimator instance.
744+
745+
To customize the URL linking to an estimator's documentation (i.e. when clicking on the
746+
"?" icon), override the `_doc_link_module` and `_doc_link_template` attributes. In
747+
addition, you can provide a `_doc_link_url_param_generator` method. Set
748+
`_doc_link_module` to the name of the (top level) module that contains your estimator.
749+
If the value does not match the top level module name, the HTML representation will not
750+
contain a link to the documentation. For scikit-learn estimators this is set to
751+
`"sklearn"`.
752+
753+
The `_doc_link_template` is used to construct the final URL. By default, it can contain
754+
two variables: `estimator_module` (the full name of the module containing the estimator)
755+
and `estimator_name` (the class name of the estimator). If you need more variables you
756+
should implement the `_doc_link_url_param_generator` method which should return a
757+
dictionary of the variables and their values. This dictionary will be used to render the
758+
`_doc_link_template`.
759+
726760
.. _coding-guidelines:
727761

728762
Coding guidelines

doc/whats_new/v1.4.rst

+7
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ Changes impacting all modules
4545
to work with our estimators and functions.
4646
:pr:`26464` by `Thomas Fan`_.
4747

48+
- |Enhancement| The HTML representation of estimators now includes a link to the
49+
documentation and is color-coded to denote whether the estimator is fitted or
50+
not (unfitted estimators are orange, fitted estimators are blue).
51+
:pr:`26616` by :user:`Riccardo Cappuzzo <rcap107>`,
52+
:user:`Ines Ibnukhsein <Ines1999>`, :user:`Gael Varoquaux <GaelVaroquaux>`, and
53+
:user:`Lilian Boulard <LilianBoulard>`.
54+
4855
- |Fix| Fixed a bug in most estimators and functions where setting a parameter to
4956
a large integer would cause a `TypeError`.
5057
:pr:`26648` by :user:`Naoise Holohan <naoise-h>`.

setup.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,9 @@ def setup_package():
605605
cmdclass=cmdclass,
606606
python_requires=python_requires,
607607
install_requires=min_deps.tag_to_packages["install"],
608-
package_data={"": ["*.csv", "*.gz", "*.txt", "*.pxd", "*.rst", "*.jpg"]},
608+
package_data={
609+
"": ["*.csv", "*.gz", "*.txt", "*.pxd", "*.rst", "*.jpg", "*.css"]
610+
},
609611
zip_safe=False, # the package can run out of an .egg file
610612
extras_require={
611613
key: min_deps.tag_to_packages[key]

sklearn/base.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from ._config import config_context, get_config
1818
from .exceptions import InconsistentVersionWarning
1919
from .utils import _IS_32BIT
20-
from .utils._estimator_html_repr import estimator_html_repr
20+
from .utils._estimator_html_repr import _HTMLDocumentationLinkMixin, estimator_html_repr
2121
from .utils._metadata_requests import _MetadataRequester, _routing_enabled
2222
from .utils._param_validation import validate_parameter_constraints
2323
from .utils._set_output import _SetOutputMixin
@@ -134,7 +134,7 @@ def _clone_parametrized(estimator, *, safe=True):
134134
return new_object
135135

136136

137-
class BaseEstimator(_MetadataRequester):
137+
class BaseEstimator(_HTMLDocumentationLinkMixin, _MetadataRequester):
138138
"""Base class for all estimators in scikit-learn.
139139
140140
Notes

0 commit comments

Comments
 (0)