@@ -342,12 +342,25 @@ def test_sample_weight():
342
342
assert_array_equal (eclf3 .predict (X ), clf1 .predict (X ))
343
343
assert_array_almost_equal (eclf3 .predict_proba (X ), clf1 .predict_proba (X ))
344
344
345
+ # check that an error is raised and indicative if sample_weight is not
346
+ # supported.
345
347
clf4 = KNeighborsClassifier ()
346
348
eclf3 = VotingClassifier (estimators = [
347
349
('lr' , clf1 ), ('svc' , clf3 ), ('knn' , clf4 )],
348
350
voting = 'soft' )
349
- msg = ('Underlying estimator \' knn\' does not support sample weights.' )
350
- assert_raise_message (ValueError , msg , eclf3 .fit , X , y , sample_weight )
351
+ msg = ('Underlying estimator KNeighborsClassifier does not support '
352
+ 'sample weights.' )
353
+ with pytest .raises (ValueError , match = msg ):
354
+ eclf3 .fit (X , y , sample_weight )
355
+
356
+ # check that _parallel_fit_estimator will raise the right error
357
+ # it should raise the original error if this is not linked to sample_weight
358
+ class ClassifierErrorFit (BaseEstimator , ClassifierMixin ):
359
+ def fit (self , X , y , sample_weight ):
360
+ raise TypeError ('Error unrelated to sample_weight.' )
361
+ clf = ClassifierErrorFit ()
362
+ with pytest .raises (TypeError , match = 'Error unrelated to sample_weight' ):
363
+ clf .fit (X , y , sample_weight = sample_weight )
351
364
352
365
353
366
def test_sample_weight_kwargs ():
@@ -404,8 +417,10 @@ def test_set_params():
404
417
@pytest .mark .filterwarnings ('ignore: Default solver will be changed' ) # 0.22
405
418
@pytest .mark .filterwarnings ('ignore: Default multi_class will' ) # 0.22
406
419
@pytest .mark .filterwarnings ('ignore:The default value of n_estimators' )
407
- def test_set_estimator_none ():
408
- """VotingClassifier set_params should be able to set estimators as None"""
420
+ @pytest .mark .parametrize ("drop" , [None , 'drop' ])
421
+ def test_set_estimator_none (drop ):
422
+ """VotingClassifier set_params should be able to set estimators as None or
423
+ drop"""
409
424
# Test predict
410
425
clf1 = LogisticRegression (random_state = 123 )
411
426
clf2 = RandomForestClassifier (random_state = 123 )
@@ -417,22 +432,22 @@ def test_set_estimator_none():
417
432
eclf2 = VotingClassifier (estimators = [('lr' , clf1 ), ('rf' , clf2 ),
418
433
('nb' , clf3 )],
419
434
voting = 'hard' , weights = [1 , 1 , 0.5 ])
420
- eclf2 .set_params (rf = None ).fit (X , y )
435
+ eclf2 .set_params (rf = drop ).fit (X , y )
421
436
assert_array_equal (eclf1 .predict (X ), eclf2 .predict (X ))
422
437
423
- assert dict (eclf2 .estimators )["rf" ] is None
438
+ assert dict (eclf2 .estimators )["rf" ] is drop
424
439
assert len (eclf2 .estimators_ ) == 2
425
440
assert all (isinstance (est , (LogisticRegression , GaussianNB ))
426
441
for est in eclf2 .estimators_ )
427
- assert eclf2 .get_params ()["rf" ] is None
442
+ assert eclf2 .get_params ()["rf" ] is drop
428
443
429
444
eclf1 .set_params (voting = 'soft' ).fit (X , y )
430
445
eclf2 .set_params (voting = 'soft' ).fit (X , y )
431
446
assert_array_equal (eclf1 .predict (X ), eclf2 .predict (X ))
432
447
assert_array_almost_equal (eclf1 .predict_proba (X ), eclf2 .predict_proba (X ))
433
- msg = 'All estimators are None. At least one is required!'
448
+ msg = 'All estimators are None or "drop" . At least one is required!'
434
449
assert_raise_message (
435
- ValueError , msg , eclf2 .set_params (lr = None , rf = None , nb = None ).fit , X , y )
450
+ ValueError , msg , eclf2 .set_params (lr = drop , rf = drop , nb = drop ).fit , X , y )
436
451
437
452
# Test soft voting transform
438
453
X1 = np .array ([[1 ], [2 ]])
@@ -444,7 +459,7 @@ def test_set_estimator_none():
444
459
eclf2 = VotingClassifier (estimators = [('rf' , clf2 ), ('nb' , clf3 )],
445
460
voting = 'soft' , weights = [1 , 0.5 ],
446
461
flatten_transform = False )
447
- eclf2 .set_params (rf = None ).fit (X1 , y1 )
462
+ eclf2 .set_params (rf = drop ).fit (X1 , y1 )
448
463
assert_array_almost_equal (eclf1 .transform (X1 ),
449
464
np .array ([[[0.7 , 0.3 ], [0.3 , 0.7 ]],
450
465
[[1. , 0. ], [0. , 1. ]]]))
@@ -522,12 +537,13 @@ def test_transform():
522
537
[('lr' , LinearRegression ()),
523
538
('rf' , RandomForestRegressor (n_estimators = 5 ))]))]
524
539
)
525
- def test_none_estimator_with_weights (X , y , voter ):
540
+ @pytest .mark .parametrize ("drop" , [None , 'drop' ])
541
+ def test_none_estimator_with_weights (X , y , voter , drop ):
526
542
# check that an estimator can be set to None and passing some weight
527
543
# regression test for
528
544
# https://github.com/scikit-learn/scikit-learn/issues/13777
529
545
voter .fit (X , y , sample_weight = np .ones (y .shape ))
530
- voter .set_params (lr = None )
546
+ voter .set_params (lr = drop )
531
547
voter .fit (X , y , sample_weight = np .ones (y .shape ))
532
548
y_pred = voter .predict (X )
533
549
assert y_pred .shape == y .shape
0 commit comments