Preserve estimator errors when `error_score="raise"` #331
The test matrix is blocked during dependency installation. The Python 3.9 job installs Optuna's current development branch, which now requires Python 3.10 or later. Tests never start, and fail-fast cancels the other Python versions.
The same error occurs in the scheduled Python 3.9 job on unchanged main:
the current Python version (3.9.25) does not satisfy Python>=3.10
Additional local verification of this branch:
13a3ff1c78314b6a1117dcee9dbbe54ab66788f9): all 38 sklearn tests pass.The style/type checks, Reviewdog, Sphinx, and Read the Docs builds passed. Rerunning a cancelled job through the API requires repository admin access, so the hosted test matrix still needs a maintainer rerun after the dependency mismatch is resolved.
Could you merge the main branch to fix the CI fail?
Merged the latest main (c2cc20b4) in 30f4726. The test matrix now passes on Python 3.10, 3.11, 3.12, and 3.13. The style/type checks and Sphinx build also passed.
The full sklearn suite passed locally as well: 38 tests. The PR diff still contains only the error-handling fix and its regression tests.
Since error messages tend to change, Optuna follows a policy of not testing them.
with pytest.raises(ValueError):
Removed the message match in b3708cf. All 38 sklearn tests pass locally.
Thank you! LGTM!
not522
ska2704
Motivation
Closes #330.
When an estimator raises
ValueError,OptunaSearchCV(error_score="raise")currently builds a score array containing"raise"and fails innp.nanmean. This hides the estimator's validation message and preventscatch=(ValueError,)from continuing the search.Description of the changes
Re-raise the original exception before constructing fallback scores, matching the existing pruning path and the documented
error_scorebehavior. Numeric fallback scores are unchanged.The regression test uses an invalid
SGDClassifierparameter and covers pruning on and off, with and withoutcatch. Both non-pruning cases fail on unchanged upstream and pass with this fix.Validation:
pytest tests/sklearn/test_sklearn.py -q: 38 passed on Python 3.13 / scikit-learn 1.9.1 and Python 3.11 / scikit-learn 1.3.2.No documentation change is needed because the existing
error_scoredocstring already describes this behavior.