You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
check_estimator is stricter than what is stated in the Estimator API doc #16241
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.
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.
Reacted by Nikita Titov, Adrin Jalali, James Lamb, Zhen-Lun (Kevin) Hong and Julien Jerphanion
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
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.
So I suppose what we really want to assert is that:
vars(Est(param=val).fit(X)) == vars(Est().set_params(param=val).fit(X))
and
vars(Est(param=val).fit(X))
== vars(Est(param=alternative_val).set_params(param=val).fit(X))
We don't currently have a way to determine a reasonable value (different
from the default) to use in a generalised test.
A pull request implementing a "strict" tags is welcome, which would enforce
adherence to standards we apply within the core library, but which are not
strictly necessary elsewhere.
@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.
@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.
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.
Activity
ogrisel commentedon Jan 27, 2020
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 viagetattr/setattror viaget_params/set_paramsand that subsequent calls to thefit/predict/transformautomatically take those changes into account.[-]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[/+]ogrisel commentedon Jan 27, 2020
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_estimatordoes not check every single detail of what we write in the API contract document.jnothman commentedon Jan 27, 2020
thomasjpfan commentedon Jan 29, 2020
While we figure out a solution to this, should we remove
check_no_attributes_set_in_initfromcheck_estimatorand only run it for our estimators?jnothman commentedon Jan 30, 2020
StrikerRUS commentedon Feb 22, 2020
Hello guys!
Is there any progress on this? Will it be possible to resolve this issue by next release?
segatrade commentedon Mar 3, 2020
Thank you for 0.22+ support preparing :)
jnothman commentedon Mar 4, 2020
h-vetinari commentedon Mar 26, 2020
+1 to prioritizing this. Fixing unintentional breakage of downstream packages should rank pretty high on the list of todos, IMHO.
rth commentedon Mar 26, 2020
@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.
h-vetinari commentedon Mar 26, 2020
@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.
27 remaining items
jeremiedbb commentedon Mar 18, 2022
very unlikely to be part of 1.1, moving to 1.2
betatim commentedon May 7, 2024
A lot of the discussion in this issue is around setting private attributes in
__init__. However the current version ofcheck_no_attributes_set_in_initalready filters out attributes that start with a_.So maybe we need an update regarding the problem that needs solving.