5
5
from joblib import cpu_count
6
6
7
7
from sklearn .utils ._testing import assert_almost_equal
8
- from sklearn .utils ._testing import assert_raises
9
- from sklearn .utils ._testing import assert_raises_regex
10
- from sklearn .utils ._testing import assert_raise_message
11
8
from sklearn .utils ._testing import assert_array_equal
12
9
from sklearn .utils ._testing import assert_array_almost_equal
13
10
from sklearn import datasets
@@ -80,7 +77,9 @@ def test_multi_target_regression_one_target():
80
77
# Test multi target regression raises
81
78
X , y = datasets .make_regression (n_targets = 1 )
82
79
rgr = MultiOutputRegressor (GradientBoostingRegressor (random_state = 0 ))
83
- assert_raises (ValueError , rgr .fit , X , y )
80
+ msg = 'at least two dimensions'
81
+ with pytest .raises (ValueError , match = msg ):
82
+ rgr .fit (X , y )
84
83
85
84
86
85
def test_multi_target_sparse_regression ():
@@ -106,8 +105,9 @@ def test_multi_target_sample_weights_api():
106
105
w = [0.8 , 0.6 ]
107
106
108
107
rgr = MultiOutputRegressor (OrthogonalMatchingPursuit ())
109
- assert_raises_regex (ValueError , "does not support sample weights" ,
110
- rgr .fit , X , y , w )
108
+ msg = "does not support sample weights"
109
+ with pytest .raises (ValueError , match = msg ):
110
+ rgr .fit (X , y , w )
111
111
112
112
# no exception should be raised if the base estimator supports weights
113
113
rgr = MultiOutputRegressor (GradientBoostingRegressor (random_state = 0 ))
@@ -252,9 +252,9 @@ def test_multi_output_classification_partial_fit():
252
252
def test_multi_output_classification_partial_fit_no_first_classes_exception ():
253
253
sgd_linear_clf = SGDClassifier (loss = 'log' , random_state = 1 , max_iter = 5 )
254
254
multi_target_linear = MultiOutputClassifier (sgd_linear_clf )
255
- assert_raises_regex ( ValueError , "classes must be passed on the first call "
256
- "to partial_fit." ,
257
- multi_target_linear .partial_fit , X , y )
255
+ msg = "classes must be passed on the first call to partial_fit. "
256
+ with pytest . raises ( ValueError , match = msg ):
257
+ multi_target_linear .partial_fit ( X , y )
258
258
259
259
260
260
def test_multi_output_classification ():
@@ -386,17 +386,27 @@ def test_multi_output_exceptions():
386
386
# NotFittedError when fit is not done but score, predict and
387
387
# and predict_proba are called
388
388
moc = MultiOutputClassifier (LinearSVC (random_state = 0 ))
389
- assert_raises (NotFittedError , moc .predict , y )
389
+
390
+ with pytest .raises (NotFittedError ):
391
+ moc .predict (y )
392
+
390
393
with pytest .raises (NotFittedError ):
391
394
moc .predict_proba
392
- assert_raises (NotFittedError , moc .score , X , y )
395
+
396
+ with pytest .raises (NotFittedError ):
397
+ moc .score (X , y )
398
+
393
399
# ValueError when number of outputs is different
394
400
# for fit and score
395
401
y_new = np .column_stack ((y1 , y2 ))
396
402
moc .fit (X , y )
397
- assert_raises (ValueError , moc .score , X , y_new )
403
+ with pytest .raises (ValueError ):
404
+ moc .score (X , y_new )
405
+
398
406
# ValueError when y is continuous
399
- assert_raise_message (ValueError , "Unknown label type" , moc .fit , X , X [:, 1 ])
407
+ msg = "Unknown label type"
408
+ with pytest .raises (ValueError , match = msg ):
409
+ moc .fit (X , X [:, 1 ])
400
410
401
411
402
412
def generate_multilabel_dataset_with_correlations ():
0 commit comments