API Revamp estimator tags - #29677
Conversation
glemaitre
left a comment
There was a problem hiding this comment.
A bunch of changes to be consistent for the documentation style mainly.
|
It's a green CI @glemaitre |
|
I had a quick look at the docs and maybe I missed it, but as a consumer of sklearn that subclasses BaseEstimator I'm not sure how to adapt my code. Fore example, I currently have classes that have stuff like: How do I adapt these classes in a way that is backward compatible with previous sklearn versions? I can't just leave But I can't remove it because then it won't be backward compatible. One solution would be to add an opt-in to have the validator ignore this attribute being present, or maybe change it to ensure that if Or is there a simpler way for me to adjust my code? |
|
@larsoner you can leave Something like this: import numpy as np
import sklearn
from packaging import version
from sklearn.base import BaseEstimator
from sklearn.utils.estimator_checks import parametrize_with_checks
from sklearn.utils.metaestimators import available_if
from sklearn.utils.validation import check_is_fitted, validate_data
def check_version(estimator):
return version.parse(sklearn.__version__) < version.parse("1.6.dev")
class MyEstimator(BaseEstimator):
@available_if(check_version)
def _more_tags(self):
return {"_skip_test": False}
def fit(self, X, y=None):
validate_data(self, X, y)
return self
def predict(self, X):
check_is_fitted(self)
validate_data(self, X, reset=False)
return np.zeros(X.shape[0], dtype=int)
@parametrize_with_checks([MyEstimator()])
def test_my_estimator(estimator, check):
check(estimator) |
Closes #22606
Closes #20804
This PR revamps estimator tags, puts them in dataclasses, and is based on #22606
High level changes from this PR:
(from #22606):
_get_tagsand_more_tagsand introduce__sklearn_tags__this PR:
statelessis removed and now we only userequires_fit. The two were redundant.only_binaryis now replaced withmulti_classmultioutput_onlyis removed and now we havemulti_outputandsingle_outputget_tags,default_tags, andTagsare put into public API