diff --git a/doc/whats_new/v0.23.rst b/doc/whats_new/v0.23.rst index e7b9fdd24989d..30cf098eee768 100644 --- a/doc/whats_new/v0.23.rst +++ b/doc/whats_new/v0.23.rst @@ -85,6 +85,15 @@ Changelog :class:`linear_model.RidgeClassifierCV`. :pr:`15653` by :user:`Jérôme Dockès `. +:mod:`sklearn.model_selection` +.............................. + +- |Enhancement| :class:`model_selection.GridSearchCV` and + :class:`model_selection.RandomizedSearchCV` yields stack trace information + in fit failed warning messages in addition to previously emitted + type and details. + :pr:`15622` by :user:`Gregory Morse `. + :mod:`sklearn.preprocessing` ............................ diff --git a/sklearn/exceptions.py b/sklearn/exceptions.py index c3da8e8d7986f..4acf7863dd682 100644 --- a/sklearn/exceptions.py +++ b/sklearn/exceptions.py @@ -139,7 +139,10 @@ class FitFailedWarning(RuntimeWarning): ... print(repr(w[-1].message)) FitFailedWarning('Estimator fit failed. The score on this train-test partition for these parameters will be set to 0.000000. - Details: \\nValueError: Penalty term must be positive; got (C=-2)\\n'...) + Details: + \\nTraceback (most recent call last):...\\nValueError: + Penalty term must be positive; got (C=-2)\\n') + .. versionchanged:: 0.18 Moved from sklearn.cross_validation. diff --git a/sklearn/model_selection/_validation.py b/sklearn/model_selection/_validation.py index 88eb5d49c4d0f..67a30c6416031 100644 --- a/sklearn/model_selection/_validation.py +++ b/sklearn/model_selection/_validation.py @@ -13,7 +13,7 @@ import warnings import numbers import time -from traceback import format_exception_only +from traceback import format_exc from contextlib import suppress import numpy as np @@ -532,7 +532,7 @@ def _fit_and_score(estimator, X, y, scorer, train, test, verbose, warnings.warn("Estimator fit failed. The score on this train-test" " partition for these parameters will be set to %f. " "Details: \n%s" % - (error_score, format_exception_only(type(e), e)[0]), + (error_score, format_exc()), FitFailedWarning) else: raise ValueError("error_score must be the string 'raise' or a" diff --git a/sklearn/model_selection/tests/test_validation.py b/sklearn/model_selection/tests/test_validation.py index c72ac0c1b7a14..d1c67930fac77 100644 --- a/sklearn/model_selection/tests/test_validation.py +++ b/sklearn/model_selection/tests/test_validation.py @@ -1632,8 +1632,14 @@ def test_fit_and_score_failing(): "partition for these parameters will be set to %f. " "Details: \n%s" % (fit_and_score_kwargs['error_score'], error_message)) - # check if the same warning is triggered - assert_warns_message(FitFailedWarning, warning_message, _fit_and_score, + + def test_warn_trace(msg): + assert 'Traceback (most recent call last):\n' in msg + split = msg.splitlines() # note: handles more than '\n' + mtb = split[0] + '\n' + split[-1] + return warning_message in mtb + # check traceback is included + assert_warns_message(FitFailedWarning, test_warn_trace, _fit_and_score, *fit_and_score_args, **fit_and_score_kwargs) fit_and_score_kwargs = {'error_score': 'raise'}