¿Cómo pasar un parámetro a solo una parte de un objeto de canalización en scikit learn?
Necesito pasar un parámetrosample_weight
, para miRandomForestClassifier
al igual que:
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]))
El código anterior funciona perfectamente bien. Luego, trato de hacer esto en un objeto de tubería de esta manera, usando un objeto de tubería en lugar de solo un bosque aleatorio:
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]))
Ahora esto se rompe en elfit
método con "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
estoy usandosklearn
versión0.14
.
Creo que el problema es que elF selection
El paso en la tubería no incluye un argumento para sample_weights. ¿Cómo paso este parámetro a un solo paso en la tubería con ejecutar "fit
"? Gracias.