14
14
15
15
from sklearn .datasets import load_digits , load_boston
16
16
from sklearn .datasets import make_regression , make_multilabel_classification
17
+ from sklearn .exceptions import ConvergenceWarning
17
18
from sklearn .externals .six .moves import cStringIO as StringIO
18
19
from sklearn .metrics import roc_auc_score
19
20
from sklearn .neural_network import MLPClassifier
22
23
from sklearn .preprocessing import StandardScaler , MinMaxScaler
23
24
from scipy .sparse import csr_matrix
24
25
from sklearn .utils .testing import (assert_raises , assert_greater , assert_equal ,
25
- assert_false )
26
+ assert_false , ignore_warnings )
26
27
27
28
28
29
np .seterr (all = 'warn' )
@@ -60,7 +61,8 @@ def test_alpha():
60
61
61
62
for alpha in alpha_values :
62
63
mlp = MLPClassifier (hidden_layer_sizes = 10 , alpha = alpha , random_state = 1 )
63
- mlp .fit (X , y )
64
+ with ignore_warnings (category = ConvergenceWarning ):
65
+ mlp .fit (X , y )
64
66
alpha_vectors .append (np .array ([absolute_sum (mlp .coefs_ [0 ]),
65
67
absolute_sum (mlp .coefs_ [1 ])]))
66
68
@@ -262,17 +264,18 @@ def test_lbfgs_regression():
262
264
263
265
264
266
def test_learning_rate_warmstart ():
265
- # Tests that warm_start reuses past solution."""
267
+ # Test that warm_start reuses past solution."""
266
268
X = [[3 , 2 ], [1 , 6 ], [5 , 6 ], [- 2 , - 4 ]]
267
269
y = [1 , 1 , 1 , 0 ]
268
270
for learning_rate in ["invscaling" , "constant" ]:
269
271
mlp = MLPClassifier (algorithm = 'sgd' , hidden_layer_sizes = 4 ,
270
272
learning_rate = learning_rate , max_iter = 1 ,
271
273
power_t = 0.25 , warm_start = True )
272
- mlp .fit (X , y )
273
- prev_eta = mlp ._optimizer .learning_rate
274
- mlp .fit (X , y )
275
- post_eta = mlp ._optimizer .learning_rate
274
+ with ignore_warnings (category = ConvergenceWarning ):
275
+ mlp .fit (X , y )
276
+ prev_eta = mlp ._optimizer .learning_rate
277
+ mlp .fit (X , y )
278
+ post_eta = mlp ._optimizer .learning_rate
276
279
277
280
if learning_rate == 'constant' :
278
281
assert_equal (prev_eta , post_eta )
@@ -321,15 +324,16 @@ def test_partial_fit_classes_error():
321
324
322
325
def test_partial_fit_classification ():
323
326
# Test partial_fit on classification.
324
- # `partial_fit` should yield the same results as 'fit'for binary and
327
+ # `partial_fit` should yield the same results as 'fit' for binary and
325
328
# multi-class classification.
326
329
for X , y in classification_datasets :
327
330
X = X
328
331
y = y
329
332
mlp = MLPClassifier (algorithm = 'sgd' , max_iter = 100 , random_state = 1 ,
330
333
tol = 0 , alpha = 1e-5 , learning_rate_init = 0.2 )
331
334
332
- mlp .fit (X , y )
335
+ with ignore_warnings (category = ConvergenceWarning ):
336
+ mlp .fit (X , y )
333
337
pred1 = mlp .predict (X )
334
338
mlp = MLPClassifier (algorithm = 'sgd' , random_state = 1 , alpha = 1e-5 ,
335
339
learning_rate_init = 0.2 )
@@ -405,7 +409,8 @@ def test_predict_proba_binary():
405
409
y = y_digits_binary [:50 ]
406
410
407
411
clf = MLPClassifier (hidden_layer_sizes = 5 )
408
- clf .fit (X , y )
412
+ with ignore_warnings (category = ConvergenceWarning ):
413
+ clf .fit (X , y )
409
414
y_proba = clf .predict_proba (X )
410
415
y_log_proba = clf .predict_log_proba (X )
411
416
@@ -427,7 +432,8 @@ def test_predict_proba_multi():
427
432
y = y_digits_multi [:10 ]
428
433
429
434
clf = MLPClassifier (hidden_layer_sizes = 5 )
430
- clf .fit (X , y )
435
+ with ignore_warnings (category = ConvergenceWarning ):
436
+ clf .fit (X , y )
431
437
y_proba = clf .predict_proba (X )
432
438
y_log_proba = clf .predict_log_proba (X )
433
439
@@ -447,10 +453,11 @@ def test_sparse_matrices():
447
453
y = y_digits_binary [:50 ]
448
454
X_sparse = csr_matrix (X )
449
455
mlp = MLPClassifier (random_state = 1 , hidden_layer_sizes = 15 )
450
- mlp .fit (X , y )
451
- pred1 = mlp .decision_function (X )
452
- mlp .fit (X_sparse , y )
453
- pred2 = mlp .decision_function (X_sparse )
456
+ with ignore_warnings (category = ConvergenceWarning ):
457
+ mlp .fit (X , y )
458
+ pred1 = mlp .decision_function (X )
459
+ mlp .fit (X_sparse , y )
460
+ pred2 = mlp .decision_function (X_sparse )
454
461
assert_almost_equal (pred1 , pred2 )
455
462
pred1 = mlp .predict (X )
456
463
pred2 = mlp .predict (X_sparse )
@@ -476,7 +483,8 @@ def test_verbose_sgd():
476
483
old_stdout = sys .stdout
477
484
sys .stdout = output = StringIO ()
478
485
479
- clf .fit (X , y )
486
+ with ignore_warnings (category = ConvergenceWarning ):
487
+ clf .fit (X , y )
480
488
clf .partial_fit (X , y )
481
489
482
490
sys .stdout = old_stdout
0 commit comments