问题
could you help me with the problem below? Many thanks in advance.
Without fit_params=fit_params
, the code below works fine, but I want to try early stopping with lgbm.
I did try to search for clues but found limited resources but some github issues from lightgbm and BayesSearchCV.
lg = lgb.LGBMClassifier(random_state=42, n_jobs=-1, objective='multiclass', n_estimators=5000)
fullPipeline = Pipeline(steps=[
('scaler', StandardScaler()),
('model', lg)
])
param_space = {'model__max_depth': [2, 63],
'model__num_leaves': [7, 4095],
}
fit_params = {
'early_stopping_rounds':30,
'eval_metric':'accuracy',
'eval_set':[(xValid, yValid)],
}
BSLGB = BayesSearchCV(fullPipeline, param_space, random_state=42, scoring='accuracy', cv=5, n_iter=50, verbose=3, n_jobs=-1,
fit_params=fit_params)
%time BSLGB.fit(xTrain.astype(float), yTrain)
Exception:
ValueError Traceback (most recent call last)
<timed eval> in <module>
C:\Anaconda3x64\envs\ml\lib\site-packages\skopt\searchcv.py in fit(self, X, y, groups, callback)
652 optim_result = self._step(
653 X, y, search_space, optimizer,
--> 654 groups=groups, n_points=n_points_adjusted
655 )
656 n_iter -= n_points
C:\Anaconda3x64\envs\ml\lib\site-packages\skopt\searchcv.py in _step(self, X, y, search_space, optimizer, groups, n_points)
548 refit = self.refit
549 self.refit = False
--> 550 self._fit(X, y, groups, params_dict)
551 self.refit = refit
552
C:\Anaconda3x64\envs\ml\lib\site-packages\skopt\searchcv.py in _fit(self, X, y, groups, parameter_iterable)
401 error_score=self.error_score
402 )
--> 403 for parameters in parameter_iterable
404 for train, test in cv_iter)
405
C:\Anaconda3x64\envs\ml\lib\site-packages\sklearn\externals\joblib\parallel.py in __call__(self, iterable)
928
929 with self._backend.retrieval_context():
--> 930 self.retrieve()
931 # Make sure that we get a last message telling us we are done
932 elapsed_time = time.time() - self._start_time
C:\Anaconda3x64\envs\ml\lib\site-packages\sklearn\externals\joblib\parallel.py in retrieve(self)
831 try:
832 if getattr(self._backend, 'supports_timeout', False):
--> 833 self._output.extend(job.get(timeout=self.timeout))
834 else:
835 self._output.extend(job.get())
C:\Anaconda3x64\envs\ml\lib\site-packages\sklearn\externals\joblib\_parallel_backends.py in wrap_future_result(future, timeout)
519 AsyncResults.get from multiprocessing."""
520 try:
--> 521 return future.result(timeout=timeout)
522 except LokyTimeoutError:
523 raise TimeoutError()
C:\Anaconda3x64\envs\ml\lib\concurrent\futures\_base.py in result(self, timeout)
430 raise CancelledError()
431 elif self._state == FINISHED:
--> 432 return self.__get_result()
433 else:
434 raise TimeoutError()
C:\Anaconda3x64\envs\ml\lib\concurrent\futures\_base.py in __get_result(self)
382 def __get_result(self):
383 if self._exception:
--> 384 raise self._exception
385 else:
386 return self._result
ValueError: not enough values to unpack (expected 2, got 1)
回答1:
The root cause of this issue is I passed a pipeline, not a model into BayesSearchCV. Meanwhile, my fit_params
do not have a prefix. To fix:
fit_params = {
'model__early_stopping_rounds':30,
'model__eval_metric':'multi_logloss',
'model__eval_set':[(xValid, yValid)],
}
来源:https://stackoverflow.com/questions/59508955/bayessearchcv-lightgbm-early-stopping-valueerror-not-enough-values-to-un