8000 ENH Yield stack trace information in resilient mode model_selection w… · scikit-learn/scikit-learn@a05a5bc · GitHub
[go: up one dir, main page]

Skip to content

Commit a05a5bc

Browse files
GregoryMorsethomasjpfan
authored andcommitted
ENH Yield stack trace information in resilient mode model_selection warnings (#15622)
1 parent 43827c9 commit a05a5bc

File tree

4 files changed

+23
-5
lines changed

4 files changed

+23
-5
lines changed

doc/whats_new/v0.23.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,15 @@ Changelog
8585
:class:`linear_model.RidgeClassifierCV`.
8686
:pr:`15653` by :user:`Jérôme Dockès <jeromedockes>`.
8787

88+
:mod:`sklearn.model_selection`
89+
..............................
90+
91+
- |Enhancement| :class:`model_selection.GridSearchCV` and
92+
:class:`model_selection.RandomizedSearchCV` yields stack trace information
93+
in fit failed warning messages in addition to previously emitted
94+
type and details.
95+
:pr:`15622` by :user:`Gregory Morse <GregoryMorse>`.
96+
8897
:mod:`sklearn.preprocessing`
8998
............................
9099

sklearn/exceptions.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,10 @@ class FitFailedWarning(RuntimeWarning):
139139
... print(repr(w[-1].message))
140140
FitFailedWarning('Estimator fit failed. The score on this train-test
141141
partition for these parameters will be set to 0.000000.
142-
Details: \\nValueError: Penalty term must be positive; got (C=-2)\\n'...)
142+
Details:
143+
\\nTraceback (most recent call last):...\\nValueError:
144+
Penalty term must be positive; got (C=-2)\\n')
145+
143146
144147
.. versionchanged:: 0.18
145148
Moved from sklearn.cross_validation.

sklearn/model_selection/_validation.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import warnings
1414
import numbers
1515
import time
16-
from traceback import format_exception_only
16+
from traceback import format_exc
1717
from contextlib import suppress
1818

1919
import numpy as np
@@ -532,7 +532,7 @@ def _fit_and_score(estimator, X, y, scorer, train, test, verbose,
532532
warnings.warn("Estimator fit failed. The score on this train-test"
533533
" partition for these parameters will be set to %f. "
534534
"Details: \n%s" %
535-
(error_score, format_exception_only(type(e), e)[0]),
535+
(error_score, format_exc()),
536536
FitFailedWarning)
537537
else:
538538
raise ValueError("error_score must be the string 'raise' or a"

sklearn/model_selection/tests/test_validation.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1632,8 +1632,14 @@ def test_fit_and_score_failing():
16321632
"partition for these parameters will be set to %f. "
16331633
"Details: \n%s" % (fit_and_score_kwargs['error_score'],
16341634
error_message))
1635-
# check if the same warning is triggered
1636-
assert_warns_message(FitFailedWarning, warning_message, _fit_and_score,
1635+
1636+
def test_warn_trace(msg):
1637+
assert 'Traceback (most recent call last):\n' in msg
1638+
split = msg.splitlines() # note: handles more than '\n'
1639+
mtb = split[0] + '\n' + split[-1]
1640+
return warning_message in mtb
1641+
# check traceback is included
1642+
assert_warns_message(FitFailedWarning, test_warn_trace, _fit_and_score,
16371643
*fit_and_score_args, **fit_and_score_kwargs)
16381644

16391645
fit_and_score_kwargs = {'error_score': 'raise'}

0 commit comments

Comments
 (0)
0