Skip to content

check_estimator is stricter than what is stated in the Estimator API doc #16241

Open

Description

@ogrisel

This issue was raised in a discussion regarding the LightGBM scikit-learn compatible estimators: lightgbm-org/LightGBM#2628 (comment)

The problem is that check_estimator complains about private attributes set in the __init__ of a scikit-learn estimator while our documentation just state the following (while not explicitly prohibiting setting private attributes in __init__):

The arguments accepted by __init__ should all be keyword arguments with a default value. In other words, a user should be able to instantiate an estimator without passing any arguments to it. The arguments should all correspond to hyperparameters describing the model or the optimisation problem the estimator tries to solve. These initial arguments (or parameters) are always remembered by the estimator. Also note that they should not be documented under the “Attributes” section, but rather under the “Parameters” section for that estimator.

In addition, every keyword argument accepted by __init__ should correspond to an attribute on the instance. Scikit-learn relies on this to find the relevant attributes to set on an estimator when doing model selection.

from: https://scikit-learn.org/0.22/developers/develop.html#instantiation

The strict check is defined in sklearn.utils.estimator_checks.check_no_attributes_set_in_init.

Activity

  1. ogrisel commented on Jan 27, 2020

    @ogrisel
    MemberAuthor

    I think that setting private attributes in __init__ is fine as long as the public attributes defined as constructor params (that is the model hyperparametrs) can be get/set at any moment either via getattr / setattr or via get_params / set_params and that subsequent calls to the fit / predict / transform automatically take those changes into account.

  2. changed the title [-]check_estimator is stricter that what is stated on Estimator API doc[/-] [+]check_estimator is stricter that what is stated in the Estimator API doc[/+] on Jan 27, 2020
  3. ogrisel commented on Jan 27, 2020

    @ogrisel
    MemberAuthor

    Arguably it's hard to write an automated tool that would check what I described above in #16241 (comment) but maybe it's fine if check_estimator does not check every single detail of what we write in the API contract document.

  4. jnothman commented on Jan 27, 2020

    @jnothman
    Member
  5. thomasjpfan commented on Jan 29, 2020

    @thomasjpfan
    Member

    While we figure out a solution to this, should we remove check_no_attributes_set_in_init from check_estimator and only run it for our estimators?

  6. jnothman commented on Jan 30, 2020

    @jnothman
    Member
  7. StrikerRUS commented on Feb 22, 2020

    @StrikerRUS
    Contributor

    Hello guys!

    Is there any progress on this? Will it be possible to resolve this issue by next release?

  8. segatrade commented on Mar 3, 2020

    @segatrade

    Thank you for 0.22+ support preparing :)

  9. jnothman commented on Mar 4, 2020

    @jnothman
    Member
  10. h-vetinari commented on Mar 26, 2020

    @h-vetinari

    +1 to prioritizing this. Fixing unintentional breakage of downstream packages should rank pretty high on the list of todos, IMHO.

  11. rth commented on Mar 26, 2020

    @rth
    Member

    @h-vetinari As mentioned above, a PR for the stict option for check_estimator would be very welcome. I would be happy to review it if someone makes it. There was some related work in #14929 that is now stalled.

  12. h-vetinari commented on Mar 26, 2020

    @h-vetinari

    @rth
    Thanks for the quick response. While I understand how open source development works, and how swamped everyone is, I think that breaking downstream users should be considered a regression and receive higher priority than "we'll accept PRs".

    I do not know the code-base, so I don't know where to start or what would be necessary. However, I'd be willing to try with some pointers of what needs to be done.

  13. 27 remaining items

  14. jeremiedbb commented on Mar 18, 2022

    @jeremiedbb
    Member

    very unlikely to be part of 1.1, moving to 1.2

  15. modified the milestones: 1.1, 1.2 on Mar 18, 2022
  16. modified the milestones: 1.2, 1.3 on Nov 16, 2022
  17. modified the milestones: 1.3, 1.4 on Jun 8, 2023
  18. modified the milestones: 1.4, 1.5 on Dec 21, 2023
  19. betatim commented on May 7, 2024

    @betatim
    Member

    A lot of the discussion in this issue is around setting private attributes in __init__. However the current version of check_no_attributes_set_in_init already filters out attributes that start with a _.

    So maybe we need an update regarding the problem that needs solving.

  20. added
    Developer API
    on May 7, 2024
  21. modified the milestones: 1.5, 2.0 on May 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    APIDeveloper APIDocumentationmodule:utils

    Type

    No type

    Projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions