17
17
from sklearn .utils ._testing import assert_almost_equal
18
18
from sklearn .utils ._testing import assert_raises
19
19
from sklearn .utils ._testing import assert_raise_message
20
- from sklearn .utils ._testing import assert_warns
21
- from sklearn .utils ._testing import assert_warns_message
22
20
from sklearn .utils ._testing import assert_raises_regex
23
21
from sklearn .utils ._testing import assert_array_almost_equal
24
22
from sklearn .utils ._testing import assert_array_equal
@@ -857,13 +855,12 @@ def split(self, X, y=None, groups=None):
857
855
858
856
X , y = load_iris (return_X_y = True )
859
857
860
- warning_message = ('Number of classes in training fold (2 ) does '
861
- 'not match total number of classes (3 ). '
858
+ warning_message = (r 'Number of classes in training fold \(2\ ) does '
859
+ r 'not match total number of classes \(3\ ). '
862
860
'Results may not be appropriate for your use case.' )
863
- assert_warns_message (RuntimeWarning , warning_message ,
864
- cross_val_predict ,
865
- LogisticRegression (solver = "liblinear" ),
866
- X , y , method = 'predict_proba' , cv = KFold (2 ))
861
+ with pytest .warns (RuntimeWarning , match = warning_message ):
862
+ cross_val_predict (LogisticRegression (solver = "liblinear" ),
863
+ X , y , method = 'predict_proba' , cv = KFold (2 ))
867
864
868
865
869
866
def test_cross_val_predict_decision_function_shape ():
@@ -1210,9 +1207,13 @@ def test_learning_curve_remove_duplicate_sample_sizes():
1210
1207
n_redundant = 0 , n_classes = 2 ,
1211
1208
n_clusters_per_class = 1 , random_state = 0 )
1212
1209
estimator = MockImprovingEstimator (2 )
1213
- train_sizes , _ , _ = assert_warns (
1214
- RuntimeWarning , learning_curve , estimator , X , y , cv = 3 ,
1215
- train_sizes = np .linspace (0.33 , 1.0 , 3 ))
1210
+ warning_message = (
1211
+ "Removed duplicate entries from 'train_sizes'. Number of ticks "
1212
+ "will be less than the size of 'train_sizes': 2 instead of 3."
1213
+ )
1214
+ with pytest .warns (RuntimeWarning , match = warning_message ):
1215
+ train_sizes , _ , _ = learning_curve (
1216
+ estimator , X , y , cv = 3 , train_sizes = np .linspace (0.33 , 1.0 , 3 ))
1216
1217
assert_array_equal (train_sizes , [1 , 2 ])
1217
1218
1218
1219
@@ -1753,8 +1754,13 @@ def test_fit_and_score_failing():
1753
1754
# passing error score to trigger the warning message
1754
1755
fit_and_score_kwargs = {'error_score' : 0 }
1755
1756
# check if the warning message type is as expected
1756
- assert_warns (FitFailedWarning , _fit_and_score , * fit_and_score_args ,
1757
- ** fit_and_score_kwargs )
1757
+ warning_message = (
1758
+ "Estimator fit failed. The score on this train-test partition for "
1759
+ "these parameters will be set to %f."
1760
+ % (fit_and_score_kwargs ['error_score' ])
1761
+ )
1762
+ with pytest .warns (FitFailedWarning , match = warning_message ):
1763
+ _fit_and_score (* fit_and_score_args , ** fit_and_score_kwargs )
1758
1764
# since we're using FailingClassfier, our error will be the following
1759
1765
error_message = "ValueError: Failing classifier failed as required"
1760
1766
# the warning message we're expecting to see
@@ -1769,8 +1775,13 @@ def test_warn_trace(msg):
1769
1775
mtb = split [0 ] + '\n ' + split [- 1 ]
1770
1776
return warning_message in mtb
1771
1777
# check traceback is included
1772
- assert_warns_message (FitFailedWarning , test_warn_trace , _fit_and_score ,
1773
- * fit_and_score_args , ** fit_and_score_kwargs )
1778
+ warning_message = (
1779
+ "Estimator fit failed. The score on this train-test partition for "
1780
+ "these parameters will be set to %f."
1781
+ % (fit_and_score_kwargs ['error_score' ])
1782
+ )
1783
+ with pytest .warns (FitFailedWarning , match = warning_message ):
1784
+ _fit_and_score (* fit_and_score_args , ** fit_and_score_kwargs )
1774
1785
1775
1786
fit_and_score_kwargs = {'error_score' : 'raise' }
1776
1787
# check if exception was raised, with default error_score='raise'
0 commit comments