Skip to content

Commit 0b5cd27

Browse files
authored
Make FrozenEstimator explicitly accept and ignore sample_weight (scikit-learn#30874)
1 parent 836f8af commit 0b5cd27

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
- :class:`~frozen.FrozenEstimator` now explicitly accepts a `sample_weight`
2+
argument in `fit` (and ignores it explicitly) to make it inspectable by
3+
meta-estimators and testing frameworks. By :user:`Olivier Grisel <ogrisel>`

sklearn/frozen/_frozen.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def __sklearn_is_fitted__(self):
8686
except NotFittedError:
8787
return False
8888

89-
def fit(self, X, y, *args, **kwargs):
89+
def fit(self, X, y, sample_weight=None, *args, **kwargs):
9090
"""No-op.
9191
9292
As a frozen estimator, calling `fit` has no effect.
@@ -99,6 +99,9 @@ def fit(self, X, y, *args, **kwargs):
9999
y : object
100100
Ignored.
101101
102+
sample_weight : object
103+
Ignored.
104+
102105
*args : tuple
103106
Additional positional arguments. Ignored, but present for API compatibility
104107
with `self.estimator`.

sklearn/frozen/tests/test_frozen.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
from sklearn.pipeline import make_pipeline
2727
from sklearn.preprocessing import RobustScaler, StandardScaler
2828
from sklearn.utils._testing import set_random_state
29-
from sklearn.utils.validation import check_is_fitted
29+
from sklearn.utils.validation import check_is_fitted, has_fit_parameter
3030

3131

3232
@pytest.fixture
@@ -221,3 +221,16 @@ def test_frozen_params():
221221
other_est = LocalOutlierFactor()
222222
frozen.set_params(estimator=other_est)
223223
assert frozen.get_params() == {"estimator": other_est}
224+
225+
226+
def test_frozen_ignores_sample_weight(regression_dataset):
227+
X, y = regression_dataset
228+
estimator = LinearRegression().fit(X, y)
229+
frozen = FrozenEstimator(estimator)
230+
231+
# Should not raise: sample_weight is just ignored as it is not used.
232+
frozen.fit(X, y, sample_weight=np.ones(len(y)))
233+
234+
# FrozenEstimator should have sample_weight in its signature to make it
235+
# explicit that sample_weight is accepted and ignored intentionally.
236+
assert has_fit_parameter(frozen, "sample_weight")

0 commit comments

Comments
 (0)