@@ -391,6 +391,28 @@ def hessp(v):
391
391
return grad , hessp
392
392
393
393
394
+ def _check_solver_option (solver , multi_class , penalty , dual ):
395
+ if solver not in ['liblinear' , 'newton-cg' , 'lbfgs' ]:
396
+ raise ValueError ("Logistic Regression supports only liblinear,"
397
+ " newton-cg and lbfgs solvers, got %s" % solver )
398
+
399
+ if multi_class not in ['multinomial' , 'ovr' ]:
400
+ raise ValueError ("multi_class should be either multinomial or "
401
+ "ovr, got %s" % multi_class )
402
+
403
+ if multi_class == 'multinomial' and solver == 'liblinear' :
404
+ raise ValueError ("Solver %s does not support "
405
+ "a multinomial backend." % solver )
406
+
407
+ if solver != 'liblinear' :
408
+ if penalty != 'l2' :
409
+ raise ValueError ("Solver %s supports only l2 penalties, "
410
+ "got %s penalty." % (solver , penalty ))
411
+ if dual :
412
+ raise ValueError ("Solver %s supports only "
413
+ "dual=False, got dual=%s" % (solver , dual ))
414
+
415
+
394
416
def logistic_regression_path (X , y , pos_class = None , Cs = 10 , fit_intercept = True ,
395
417
max_iter = 100 , tol = 1e-4 , verbose = 0 ,
396
418
solver = 'lbfgs' , coef = None , copy = True ,
@@ -501,25 +523,8 @@ def logistic_regression_path(X, y, pos_class=None, Cs=10, fit_intercept=True,
501
523
if isinstance (Cs , numbers .Integral ):
502
524
Cs = np .logspace (- 4 , 4 , Cs )
503
525
504
- if multi_class not in ['multinomial' , 'ovr' ]:
505
- raise ValueError ("multi_class can be either 'multinomial' or 'ovr'"
506
- "got %s" % multi_class )
507
-
508
- if solver not in ['liblinear' , 'newton-cg' , 'lbfgs' ]:
509
- raise ValueError ("Logistic Regression supports only liblinear,"
510
- " newton-cg and lbfgs solvers. got %s" % solver )
511
-
512
- if multi_class == 'multinomial' and solver == 'liblinear' :
513
- raise ValueError ("Solver %s cannot solve problems with "
514
- "a multinomial backend." % solver )
526
+ _check_solver_option (solver , multi_class , penalty , dual )
515
527
516
- if solver != 'liblinear' :
517
- if penalty != 'l2' :
518
- raise ValueError ("newton-cg and lbfgs solvers support only "
519
- "l2 penalties, got %s penalty." % penalty )
520
- if dual :
521
- raise ValueError ("newton-cg and lbfgs solvers support only "
522
- "dual=False, got dual=%s" % dual )
523
528
# Preprocessing.
524
529
X = check_array (X , accept_sparse = 'csr' , dtype = np .float64 )
525
530
y = check_array (y , ensure_2d = False , copy = copy , dtype = None )
@@ -781,6 +786,7 @@ def _log_reg_scoring_path(X, y, train, test, pos_class=None, Cs=10,
781
786
scores : ndarray, shape (n_cs,)
782
787
Scores obtained for each Cs.
783
788
"""
789
+ _check_solver_option (solver , multi_class , penalty , dual )
784
790
785
791
log_reg = LogisticRegression (fit_intercept = fit_intercept )
786
792
@@ -1015,18 +1021,9 @@ def fit(self, X, y):
1015
1021
1016
1022
X , y = check_X_y (X , y , accept_sparse = 'csr' , dtype = np .float64 , order = "C" )
1017
1023
self .classes_ = np .unique (y )
1018
- if self .solver not in ['liblinear' , 'newton-cg' , 'lbfgs' ]:
1019
- raise ValueError (
1020
- "Logistic Regression supports only liblinear, newton-cg and "
1021
- "lbfgs solvers, Got solver=%s" % self .solver
1022
- )
1023
1024
1024
- if self .solver == 'liblinear' and self .multi_class == 'multinomial' :
1025
- raise ValueError ("Solver %s does not support a multinomial "
1026
- "backend." % self .solver )
1027
- if self .multi_class not in ['ovr' , 'multinomial' ]:
1028
- raise ValueError ("multi_class should be either ovr or multinomial "
1029
- "got %s" % self .multi_class )
1025
+ _check_solver_option (self .solver , self .multi_class , self .penalty ,
1026
+ self .dual )
1030
1027
1031
1028
if self .solver == 'liblinear' :
1032
1029
self .coef_ , self .intercept_ , self .n_iter_ = _fit_liblinear (
@@ -1308,22 +1305,19 @@ def fit(self, X, y):
1308
1305
self : object
1309
1306
Returns self.
1310
1307
"""
1311
- if self .solver != 'liblinear' :
1312
- if self .penalty != 'l2' :
1313
- raise ValueError ("newton-cg and lbfgs solvers support only "
1314
- "l2 penalties." )
1315
- if self .dual :
1316
- raise ValueError ("newton-cg and lbfgs solvers support only "
1317
- "the primal form." )
1308
+ _check_solver_option (self .solver , self .multi_class , self .penalty ,
1309
+ self .dual )
1310
+
1311
+ if not isinstance (self .max_iter , numbers .Number ) or self .max_iter < 0 :
1312
+ raise ValueError ("Maximum number of iteration must be positive;"
1313
+ " got (max_iter=%r)" % self .max_iter )
1314
+ if not isinstance (self .tol , numbers .Number ) or self .tol < 0 :
1315
+ raise ValueError ("Tolerance for stopping criteria must be "
1316
+ "positive; got (tol=%r)" % self .tol )
1318
1317
1319
1318
X = check_array (X , accept_sparse = 'csr' , dtype = np .float64 )
1320
1319
y = check_array (y , ensure_2d = False , dtype = None )
1321
1320
1322
- if self .multi_class not in ['ovr' , 'multinomial' ]:
1323
- raise ValueError ("multi_class backend should be either "
1324
- "'ovr' or 'multinomial'"
1325
- " got %s" % self .multi_class )
1326
-
1327
1321
if y .ndim == 2 and y .shape [1 ] == 1 :
1328
1322
warnings .warn (
1329
1323
"A column-vector y was passed when a 1d array was"
0 commit comments