@@ -105,7 +105,9 @@ def test_predict_iris():
105
105
LogisticRegression (C = len (iris .data ), solver = 'lbfgs' ,
106
106
multi_class = 'multinomial' ),
107
107
LogisticRegression (C = len (iris .data ), solver = 'newton-cg' ,
108
- multi_class = 'multinomial' )]:
108
+ multi_class = 'multinomial' ),
109
+ LogisticRegression (C = len (iris .data ), solver = 'sag' ,
110
+ multi_class = 'ovr' )]:
109
111
clf .fit (iris .data , target )
110
112
assert_array_equal (np .unique (target ), clf .classes_ )
111
113
@@ -216,17 +218,17 @@ def test_consistency_path():
216
218
f = ignore_warnings
217
219
# can't test with fit_intercept=True since LIBLINEAR
218
220
# penalizes the intercept
219
- for method in ('lbfgs' , 'newton-cg' , 'liblinear' ):
221
+ for method in ('lbfgs' , 'newton-cg' , 'liblinear' , 'sag' ):
220
222
coefs , Cs = f (logistic_regression_path )(
221
- X , y , Cs = Cs , fit_intercept = False , tol = 1e-16 , solver = method )
223
+ X , y , Cs = Cs , fit_intercept = False , tol = 1e-5 , solver = method )
222
224
for i , C in enumerate (Cs ):
223
- lr = LogisticRegression (C = C , fit_intercept = False , tol = 1e-16 )
225
+ lr = LogisticRegression (C = C , fit_intercept = False , tol = 1e-5 )
224
226
lr .fit (X , y )
225
227
lr_coef = lr .coef_ .ravel ()
226
228
assert_array_almost_equal (lr_coef , coefs [i ], decimal = 4 )
227
229
228
230
# test for fit_intercept=True
229
- for method in ('lbfgs' , 'newton-cg' , 'liblinear' ):
231
+ for method in ('lbfgs' , 'newton-cg' , 'liblinear' , 'sag' ):
230
232
Cs = [1e3 ]
231
233
coefs , Cs = f (logistic_regression_path )(
232
234
X , y , Cs = Cs , fit_intercept = True , tol = 1e-4 , solver = method )
@@ -450,29 +452,43 @@ def test_ovr_multinomial_iris():
450
452
451
453
def test_logistic_regression_solvers ():
452
454
X , y = make_classification (n_features = 10 , n_informative = 5 , random_state = 0 )
453
- clf_n = LogisticRegression (solver = 'newton-cg' , fit_intercept = False )
454
- clf_n .fit (X , y )
455
+ clf_new = LogisticRegression (solver = 'newton-cg' , fit_intercept = False )
456
+ clf_new .fit (X , y )
455
457
clf_lbf = LogisticRegression (solver = 'lbfgs' , fit_intercept = False )
456
458
clf_lbf .fit (X , y )
459
+ clf_sag = LogisticRegression (solver = 'sag' , fit_intercept = False )
460
+ clf_sag .fit (X , y )
457
461
clf_lib = LogisticRegression (fit_intercept = False )
458
462
clf_lib .fit (X , y )
459
- assert_array_almost_equal (clf_n .coef_ , clf_lib .coef_ , decimal = 3 )
463
+ assert_array_almost_equal (clf_new .coef_ , clf_lib .coef_ , decimal = 3 )
460
464
assert_array_almost_equal (clf_lib .coef_ , clf_lbf .coef_ , decimal = 3 )
461
- assert_array_almost_equal (clf_n .coef_ , clf_lbf .coef_ , decimal = 3 )
465
+ assert_array_almost_equal (clf_new .coef_ , clf_lbf .coef_ , decimal = 3 )
466
+ assert_array_almost_equal (clf_sag .coef_ , clf_lib .coef_ , decimal = 3 )
467
+ assert_array_almost_equal (clf_sag .coef_ , clf_new .coef_ , decimal = 3 )
468
+ assert_array_almost_equal (clf_sag .coef_ , clf_lbf .coef_ , decimal = 3 )
462
469
463
470
464
471
def test_logistic_regression_solvers_multiclass ():
472
+ tol = 1e-6
465
473
X , y = make_classification (n_samples = 20 , n_features = 20 , n_informative = 10 ,
466
474
n_classes = 3 , random_state = 0 )
467
- clf_n = LogisticRegression (solver = 'newton-cg' , fit_intercept = False )
468
- clf_n .fit (X , y )
469
- clf_lbf = LogisticRegression (solver = 'lbfgs' , fit_intercept = False )
475
+ clf_new = LogisticRegression (solver = 'newton-cg' , fit_intercept = False ,
476
+ tol = tol )
477
+ clf_new .fit (X , y )
478
+ clf_lbf = LogisticRegression (solver = 'lbfgs' , fit_intercept = False ,
479
+ tol = tol )
470
480
clf_lbf .fit (X , y )
471
- clf_lib = LogisticRegression (fit_intercept = False )
481
+ clf_sag = LogisticRegression (solver = 'sag' , fit_intercept = False ,
482
+ tol = tol , max_iter = 1000 )
483
+ clf_sag .fit (X , y )
484
+ clf_lib = LogisticRegression (fit_intercept = False , tol = tol )
472
485
clf_lib .fit (X , y )
473
- assert_array_almost_equal (clf_n .coef_ , clf_lib .coef_ , decimal = 4 )
486
+ assert_array_almost_equal (clf_new .coef_ , clf_lib .coef_ , decimal = 4 )
474
487
assert_array_almost_equal (clf_lib .coef_ , clf_lbf .coef_ , decimal = 4 )
475
- assert_array_almost_equal (clf_n .coef_ , clf_lbf .coef_ , decimal = 4 )
488
+ assert_array_almost_equal (clf_new .coef_ , clf_lbf .coef_ , decimal = 4 )
489
+ assert_array_almost_equal (clf_sag .coef_ , clf_lib .coef_ , decimal = 4 )
490
+ assert_array_almost_equal (clf_sag .coef_ , clf_new .coef_ , decimal = 4 )
491
+ assert_array_almost_equal (clf_sag .coef_ , clf_lbf .coef_ , decimal = 4 )
476
492
477
493
478
494
def test_logistic_regressioncv_class_weights ():
@@ -499,7 +515,12 @@ def test_logistic_regressioncv_class_weights():
499
515
clf_lib = LogisticRegressionCV (solver = 'liblinear' , fit_intercept = False ,
500
516
class_weight = 'auto' )
501
517
clf_lib .fit (X , y )
518
+ clf_sag = LogisticRegressionCV (solver = 'sag' , fit_intercept = False ,
519
+ class_weight = 'auto' )
520
+ clf_sag .fit (X , y )
502
521
assert_array_almost_equal (clf_lib .coef_ , clf_lbf .coef_ , decimal = 4 )
522
+ assert_array_almost_equal (clf_sag .coef_ , clf_lbf .coef_ , decimal = 4 )
523
+ assert_array_almost_equal (clf_lib .coef_ , clf_sag .coef_ , decimal = 4 )
503
524
504
525
505
526
def test_logistic_regression_convergence_warnings ():
0 commit comments