Skip to content

Instantly share code, notes, and snippets.

@ogrisel
Created September 28, 2020 13:08
Show Gist options
  • Save ogrisel/a1e94aaaab7bb73409d0e2892b1f9ed0 to your computer and use it in GitHub Desktop.
Save ogrisel/a1e94aaaab7bb73409d0e2892b1f9ed0 to your computer and use it in GitHub Desktop.
In [19]: from sklearn.preprocessing import StandardScaler
In [20]: from sklearn.linear_model import LogisticRegression
In [21]: from sklearn.pipeline import Pipeline
In [22]: p = Pipeline([("scaler", StandardScaler()), ("classifier", LogisticRegression())])
In [23]: import numpy as np
In [24]: p.fit([[0., 1.], [1., 0.]], [0, 1])
Out[24]:
Pipeline(steps=[('scaler', StandardScaler()),
('classifier', LogisticRegression())])
In [25]: p.fit([[0., 1.], [1., 0.]], [0, 1], sample_weight=[1., 1.])
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-25-62be8b6c7725> in <module>
----> 1 p.fit([[0., 1.], [1., 0.]], [0, 1], sample_weight=[1., 1.])
~/code/scikit-learn/sklearn/pipeline.py in fit(self, X, y, **fit_params)
333 This estimator
334 """
--> 335 fit_params_steps = self._check_fit_params(**fit_params)
336 Xt = self._fit(X, y, **fit_params_steps)
337 with _print_elapsed_time('Pipeline',
~/code/scikit-learn/sklearn/pipeline.py in _check_fit_params(self, **fit_params)
247 for pname, pval in fit_params.items():
248 if '__' not in pname:
--> 249 raise ValueError(
250 "Pipeline.fit does not accept the {} parameter. "
251 "You can pass parameters to specific steps of your "
ValueError: Pipeline.fit does not accept the sample_weight parameter. You can pass parameters to specific steps of your pipeline using the stepname__parameter format, e.g. `Pipeline.fit(X, y, logisticregression__sample_weight=sample_weight)`.
In [26]: p.fit([[0., 1.], [1., 0.]], [0, 1], classifier__sample_weight=[1., 1.])
Out[26]:
Pipeline(steps=[('scaler', StandardScaler()),
('classifier', LogisticRegression())])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment