Skip to content

Commit 1fcea5f

Browse files
committed
SYNC raise error parameter passed to fit
1 parent 7032dcb commit 1fcea5f

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

imblearn/pipeline.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
from sklearn import pipeline
1717
from sklearn.base import clone
18+
from sklearn.utils import Bunch, _print_elapsed_time
1819
from sklearn.utils.metaestimators import if_delegate_has_method
1920
from sklearn.utils.validation import check_memory
2021

@@ -60,7 +61,7 @@ class Pipeline(pipeline.Pipeline):
6061
6162
Attributes
6263
----------
63-
named_steps : dict
64+
named_steps : bunch object, a dictionary with attribute access
6465
Read-only attribute to access any step parameter by user given name.
6566
Keys are step names and values are steps parameters.
6667
@@ -178,6 +179,13 @@ def _fit(self, X, y=None, **fit_params):
178179
name: {} for name, step in self.steps if step is not None
179180
}
180181
for pname, pval in fit_params.items():
182+
if '__' not in pname:
183+
raise ValueError(
184+
"Pipeline.fit does not accept the {} parameter. "
185+
"You can pass parameters to specific steps of your "
186+
"pipeline using the stepname__parameter format, e.g. "
187+
"`Pipeline.fit(X, y, logisticregression__sample_weight"
188+
"=sample_weight)`.".format(pname))
181189
step, param = pname.split("__", 1)
182190
fit_params_steps[step][param] = pval
183191
for step_idx, name, transformer in self._iter(with_final=False):

imblearn/tests/test_pipeline.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1261,3 +1261,10 @@ def test_score_samples_on_pipeline_without_score_samples():
12611261
"'score_samples'",
12621262
):
12631263
pipe.score_samples(X)
1264+
1265+
1266+
def test_pipeline_param_error():
1267+
clf = make_pipeline(LogisticRegression())
1268+
with pytest.raises(ValueError, match="Pipeline.fit does not accept "
1269+
"the sample_weight parameter"):
1270+
clf.fit([[0], [0]], [0, 1], sample_weight=[1, 1])

0 commit comments

Comments
 (0)