如何将参数仅传递给 scikit 学习中管道对象的一部分?
How to pass a parameter to only one part of a pipeline object in scikit learn?
我需要向我的 RandomForestClassifier
传递一个参数 sample_weight
,如下所示:
X = np.array([[2.0, 2.0, 1.0, 0.0, 1.0, 3.0, 3.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0,
1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 5.0, 3.0,
2.0, '0'],
[15.0, 2.0, 5.0, 5.0, 0.466666666667, 4.0, 3.0, 2.0, 0.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0,
7.0, 14.0, 2.0, '0'],
[3.0, 4.0, 3.0, 1.0, 1.33333333333, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0,
9.0, 8.0, 2.0, '0'],
[3.0, 2.0, 3.0, 0.0, 0.666666666667, 2.0, 2.0, 1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0,
5.0, 3.0, 1.0, '0']], dtype=object)
y = np.array([ 0., 0., 1., 0.])
m = sklearn.ensemble.RandomForestClassifier(
random_state=0,
oob_score=True,
n_estimators=100,
min_samples_leaf=5,
max_depth=10)
m.fit(X, y, sample_weight=np.array([3,4,2,3]))
上面的代码工作得很好。然后,我尝试像这样在管道对象中执行此操作,使用管道对象而不是仅使用随机森林:
m = sklearn.pipeline.Pipeline([
('feature_selection', sklearn.feature_selection.SelectKBest(
score_func=sklearn.feature_selection.f_regression,
k=25)),
('model', sklearn.ensemble.RandomForestClassifier(
random_state=0,
oob_score=True,
n_estimators=500,
min_samples_leaf=5,
max_depth=10))])
m.fit(X, y, sample_weight=np.array([3,4,2,3]))
现在在 fit
方法中使用“ValueError: need more than 1 value to unpack
”中断。
ValueError Traceback (most recent call last)
<ipython-input-212-c4299f5b3008> in <module>()
25 max_depth=10))])
26
---> 27 m.fit(X, y, sample_weights=np.array([3,4,2,3]))
/usr/local/lib/python2.7/dist-packages/sklearn/pipeline.pyc in fit(self, X, y, **fit_params)
128 data, then fit the transformed data using the final estimator.
129 """
--> 130 Xt, fit_params = self._pre_transform(X, y, **fit_params)
131 self.steps[-1][-1].fit(Xt, y, **fit_params)
132 return self
/usr/local/lib/python2.7/dist-packages/sklearn/pipeline.pyc in _pre_transform(self, X, y, **fit_params)
113 fit_params_steps = dict((step, {}) for step, _ in self.steps)
114 for pname, pval in six.iteritems(fit_params):
--> 115 step, param = pname.split('__', 1)
116 fit_params_steps[step][param] = pval
117 Xt = X
ValueError: need more than 1 value to unpack
我正在使用 sklearn
版本 0.14
.
我认为问题在于管道中的 F selection
步骤没有接受 sample_weights 的参数。如何使用 I 运行“fit
”将此参数仅传递给管道中的一个步骤?谢谢。
The purpose of the pipeline is to assemble several steps that can be
cross-validated together while setting different parameters. For this,
it enables setting parameters of the various steps using their names
and the parameter name separated by a ‘__’, as in the example below.
因此,您只需在要传递给 'model'
步骤的任何拟合参数 kwargs 前面插入 model__
:
m.fit(X, y, model__sample_weight=np.array([3,4,2,3]))
您也可以使用方法 set_params
并在步骤名称前添加。
m = sklearn.pipeline.Pipeline([
('feature_selection', sklearn.feature_selection.SelectKBest(
score_func=sklearn.feature_selection.f_regression,
k=25)),
('model', sklearn.ensemble.RandomForestClassifier(
random_state=0,
oob_score=True,
n_estimators=500,
min_samples_leaf=5,
max_depth=10))])
m.set_params(model__sample_weight=np.array([3,4,2,3]))
希望我可以在上面的@rovyko post 上发表评论,而不是单独回答,但我还没有足够的 Whosebug 声誉来发表评论,所以在这里。
您不能使用:
Pipeline.set_params(model__sample_weight=np.array([3,4,2,3])
为RandomForestClassifier.fit()
方法设置参数。 Pipeline.set_params()
如代码中所示 (here) is only for initialization parameters for individual steps in the Pipeline. RandomForestClassifier
has no initialization parameter called sample_weight
(see its __init__()
method here)。 sample_weight
实际上是 RandomForestClassifier
的 fit()
方法的输入参数,因此只能通过正确标记答案中提供的方法设置 @ali_m,即,
m.fit(X, y, model__sample_weight=np.array([3,4,2,3]))
.
我需要向我的 RandomForestClassifier
传递一个参数 sample_weight
,如下所示:
X = np.array([[2.0, 2.0, 1.0, 0.0, 1.0, 3.0, 3.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0,
1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 5.0, 3.0,
2.0, '0'],
[15.0, 2.0, 5.0, 5.0, 0.466666666667, 4.0, 3.0, 2.0, 0.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0,
7.0, 14.0, 2.0, '0'],
[3.0, 4.0, 3.0, 1.0, 1.33333333333, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0,
9.0, 8.0, 2.0, '0'],
[3.0, 2.0, 3.0, 0.0, 0.666666666667, 2.0, 2.0, 1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0,
5.0, 3.0, 1.0, '0']], dtype=object)
y = np.array([ 0., 0., 1., 0.])
m = sklearn.ensemble.RandomForestClassifier(
random_state=0,
oob_score=True,
n_estimators=100,
min_samples_leaf=5,
max_depth=10)
m.fit(X, y, sample_weight=np.array([3,4,2,3]))
上面的代码工作得很好。然后,我尝试像这样在管道对象中执行此操作,使用管道对象而不是仅使用随机森林:
m = sklearn.pipeline.Pipeline([
('feature_selection', sklearn.feature_selection.SelectKBest(
score_func=sklearn.feature_selection.f_regression,
k=25)),
('model', sklearn.ensemble.RandomForestClassifier(
random_state=0,
oob_score=True,
n_estimators=500,
min_samples_leaf=5,
max_depth=10))])
m.fit(X, y, sample_weight=np.array([3,4,2,3]))
现在在 fit
方法中使用“ValueError: need more than 1 value to unpack
”中断。
ValueError Traceback (most recent call last)
<ipython-input-212-c4299f5b3008> in <module>()
25 max_depth=10))])
26
---> 27 m.fit(X, y, sample_weights=np.array([3,4,2,3]))
/usr/local/lib/python2.7/dist-packages/sklearn/pipeline.pyc in fit(self, X, y, **fit_params)
128 data, then fit the transformed data using the final estimator.
129 """
--> 130 Xt, fit_params = self._pre_transform(X, y, **fit_params)
131 self.steps[-1][-1].fit(Xt, y, **fit_params)
132 return self
/usr/local/lib/python2.7/dist-packages/sklearn/pipeline.pyc in _pre_transform(self, X, y, **fit_params)
113 fit_params_steps = dict((step, {}) for step, _ in self.steps)
114 for pname, pval in six.iteritems(fit_params):
--> 115 step, param = pname.split('__', 1)
116 fit_params_steps[step][param] = pval
117 Xt = X
ValueError: need more than 1 value to unpack
我正在使用 sklearn
版本 0.14
.
我认为问题在于管道中的 F selection
步骤没有接受 sample_weights 的参数。如何使用 I 运行“fit
”将此参数仅传递给管道中的一个步骤?谢谢。
The purpose of the pipeline is to assemble several steps that can be cross-validated together while setting different parameters. For this, it enables setting parameters of the various steps using their names and the parameter name separated by a ‘__’, as in the example below.
因此,您只需在要传递给 'model'
步骤的任何拟合参数 kwargs 前面插入 model__
:
m.fit(X, y, model__sample_weight=np.array([3,4,2,3]))
您也可以使用方法 set_params
并在步骤名称前添加。
m = sklearn.pipeline.Pipeline([
('feature_selection', sklearn.feature_selection.SelectKBest(
score_func=sklearn.feature_selection.f_regression,
k=25)),
('model', sklearn.ensemble.RandomForestClassifier(
random_state=0,
oob_score=True,
n_estimators=500,
min_samples_leaf=5,
max_depth=10))])
m.set_params(model__sample_weight=np.array([3,4,2,3]))
希望我可以在上面的@rovyko post 上发表评论,而不是单独回答,但我还没有足够的 Whosebug 声誉来发表评论,所以在这里。
您不能使用:
Pipeline.set_params(model__sample_weight=np.array([3,4,2,3])
为RandomForestClassifier.fit()
方法设置参数。 Pipeline.set_params()
如代码中所示 (here) is only for initialization parameters for individual steps in the Pipeline. RandomForestClassifier
has no initialization parameter called sample_weight
(see its __init__()
method here)。 sample_weight
实际上是 RandomForestClassifier
的 fit()
方法的输入参数,因此只能通过正确标记答案中提供的方法设置 @ali_m,即,
m.fit(X, y, model__sample_weight=np.array([3,4,2,3]))
.