<s> import numpy as np <EOL> import scipy . sparse as sp <EOL> from scipy import linalg , optimize , sparse <EOL> from sklearn . utils . testing import assert_almost_equal <EOL> from sklearn . utils . testing import assert_array_equal <EOL> from sklearn . utils . testing import assert_array_almost_equal <EOL> from sklearn . utils . testing import assert_equal <EOL> from sklearn . utils . testing import assert_greater <EOL> from sklearn . utils . testing import assert_raises <EOL> from sklearn . utils . testing import assert_true <EOL> from sklearn . utils . testing import assert_warns <EOL> from sklearn . utils . testing import assert_warns_message <EOL> from sklearn . utils . testing import raises <EOL> from sklearn . utils . testing import ignore_warnings <EOL> from sklearn . utils . testing import assert_raise_message <EOL> from sklearn . exceptions import ConvergenceWarning <EOL> from sklearn . utils import compute_class_weight <EOL> from sklearn . utils . fixes import sp_version <EOL> from sklearn . linear_model . logistic import ( <EOL> LogisticRegression , <EOL> logistic_regression_path , LogisticRegressionCV , <EOL> _logistic_loss_and_grad , _logistic_grad_hess , <EOL> _multinomial_grad_hess , _logistic_loss , <EOL> ) <EOL> from sklearn . model_selection import StratifiedKFold <EOL> from sklearn . datasets import load_iris , make_classification <EOL> from sklearn . metrics import log_loss <EOL> X = [ [ - <NUM_LIT:1> , <NUM_LIT:0> ] , [ <NUM_LIT:0> , <NUM_LIT:1> ] , [ <NUM_LIT:1> , <NUM_LIT:1> ] ] <EOL> X_sp = sp . csr_matrix ( X ) <EOL> Y1 = [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> ] <EOL> Y2 = [ <NUM_LIT:2> , <NUM_LIT:1> , <NUM_LIT:0> ] <EOL> iris = load_iris ( ) <EOL> def check_predictions ( clf , X , y ) : <EOL> """<STR_LIT>""" <EOL> n_samples = len ( y ) <EOL> classes = np . unique ( y ) <EOL> n_classes = classes . shape [ <NUM_LIT:0> ] <EOL> predicted = clf . fit ( X , y ) . predict ( X ) <EOL> assert_array_equal ( clf . classes_ , classes ) <EOL> assert_equal ( predicted . shape , ( n_samples , ) ) <EOL> assert_array_equal ( predicted , y ) <EOL> probabilities = clf . predict_proba ( X ) <EOL> assert_equal ( probabilities . shape , ( n_samples , n_classes ) ) <EOL> assert_array_almost_equal ( probabilities . sum ( axis = <NUM_LIT:1> ) , np . ones ( n_samples ) ) <EOL> assert_array_equal ( probabilities . argmax ( axis = <NUM_LIT:1> ) , y ) <EOL> def test_predict_2_classes ( ) : <EOL> check_predictions ( LogisticRegression ( random_state = <NUM_LIT:0> ) , X , Y1 ) <EOL> check_predictions ( LogisticRegression ( random_state = <NUM_LIT:0> ) , X_sp , Y1 ) <EOL> check_predictions ( LogisticRegression ( C = <NUM_LIT:100> , random_state = <NUM_LIT:0> ) , X , Y1 ) <EOL> check_predictions ( LogisticRegression ( C = <NUM_LIT:100> , random_state = <NUM_LIT:0> ) , X_sp , Y1 ) <EOL> check_predictions ( LogisticRegression ( fit_intercept = False , <EOL> random_state = <NUM_LIT:0> ) , X , Y1 ) <EOL> check_predictions ( LogisticRegression ( fit_intercept = False , <EOL> random_state = <NUM_LIT:0> ) , X_sp , Y1 ) <EOL> def test_error ( ) : <EOL> msg = "<STR_LIT>" <EOL> assert_raise_message ( ValueError , msg , <EOL> LogisticRegression ( C = - <NUM_LIT:1> ) . fit , X , Y1 ) <EOL> assert_raise_message ( ValueError , msg , <EOL> LogisticRegression ( C = "<STR_LIT:test>" ) . fit , X , Y1 ) <EOL> for LR in [ LogisticRegression , LogisticRegressionCV ] : <EOL> msg = "<STR_LIT>" <EOL> assert_raise_message ( ValueError , msg , LR ( tol = - <NUM_LIT:1> ) . fit , X , Y1 ) <EOL> assert_raise_message ( ValueError , msg , LR ( tol = "<STR_LIT:test>" ) . fit , X , Y1 ) <EOL> msg = "<STR_LIT>" <EOL> assert_raise_message ( ValueError , msg , LR ( max_iter = - <NUM_LIT:1> ) . fit , X , Y1 ) <EOL> assert_raise_message ( ValueError , msg , LR ( max_iter = "<STR_LIT:test>" ) . fit , X , Y1 ) <EOL> def test_predict_3_classes ( ) : <EOL> check_predictions ( LogisticRegression ( C = <NUM_LIT:10> ) , X , Y2 ) <EOL> check_predictions ( LogisticRegression ( C = <NUM_LIT:10> ) , X_sp , Y2 ) <EOL> def test_predict_iris ( ) : <EOL> n_samples , n_features = iris . data . shape <EOL> target = iris . target_names [ iris . target ] <EOL> for clf in [ LogisticRegression ( C = len ( iris . data ) ) , <EOL> LogisticRegression ( C = len ( iris . data ) , solver = '<STR_LIT>' , <EOL> multi_class = '<STR_LIT>' ) , <EOL> LogisticRegression ( C = len ( iris . data ) , solver = '<STR_LIT>' , <EOL> multi_class = '<STR_LIT>' ) , <EOL> LogisticRegression ( C = len ( iris . data ) , solver = '<STR_LIT>' , tol = <NUM_LIT> , <EOL> multi_class = '<STR_LIT>' , random_state = <NUM_LIT> ) ] : <EOL> clf . fit ( iris . data , target ) <EOL> assert_array_equal ( np . unique ( target ) , clf . classes_ ) <EOL> pred = clf . predict ( iris . data ) <EOL> assert_greater ( np . mean ( pred == target ) , <NUM_LIT> ) <EOL> probabilities = clf . predict_proba ( iris . data ) <EOL> assert_array_almost_equal ( probabilities . sum ( axis = <NUM_LIT:1> ) , <EOL> np . ones ( n_samples ) ) <EOL> pred = iris . target_names [ probabilities . argmax ( axis = <NUM_LIT:1> ) ] <EOL> assert_greater ( np . mean ( pred == target ) , <NUM_LIT> ) <EOL> def test_multinomial_validation ( ) : <EOL> for solver in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> lr = LogisticRegression ( C = - <NUM_LIT:1> , solver = solver , multi_class = '<STR_LIT>' ) <EOL> assert_raises ( ValueError , lr . fit , [ [ <NUM_LIT:0> , <NUM_LIT:1> ] , [ <NUM_LIT:1> , <NUM_LIT:0> ] ] , [ <NUM_LIT:0> , <NUM_LIT:1> ] ) <EOL> def test_check_solver_option ( ) : <EOL> X , y = iris . data , iris . target <EOL> for LR in [ LogisticRegression , LogisticRegressionCV ] : <EOL> msg = ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> lr = LR ( solver = "<STR_LIT>" ) <EOL> assert_raise_message ( ValueError , msg , lr . fit , X , y ) <EOL> msg = "<STR_LIT>" <EOL> lr = LR ( solver = '<STR_LIT>' , multi_class = "<STR_LIT>" ) <EOL> assert_raise_message ( ValueError , msg , lr . fit , X , y ) <EOL> msg = "<STR_LIT>" <EOL> lr = LR ( solver = '<STR_LIT>' , multi_class = '<STR_LIT>' ) <EOL> assert_raise_message ( ValueError , msg , lr . fit , X , y ) <EOL> for solver in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> msg = ( "<STR_LIT>" % <EOL> solver ) <EOL> lr = LR ( solver = solver , penalty = '<STR_LIT>' ) <EOL> assert_raise_message ( ValueError , msg , lr . fit , X , y ) <EOL> msg = ( "<STR_LIT>" % <EOL> solver ) <EOL> lr = LR ( solver = solver , dual = True ) <EOL> assert_raise_message ( ValueError , msg , lr . fit , X , y ) <EOL> def test_multinomial_binary ( ) : <EOL> target = ( iris . target > <NUM_LIT:0> ) . astype ( np . intp ) <EOL> target = np . array ( [ "<STR_LIT>" , "<STR_LIT>" ] ) [ target ] <EOL> for solver in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> clf = LogisticRegression ( solver = solver , multi_class = '<STR_LIT>' , <EOL> random_state = <NUM_LIT> , max_iter = <NUM_LIT> ) <EOL> clf . fit ( iris . data , target ) <EOL> assert_equal ( clf . coef_ . shape , ( <NUM_LIT:1> , iris . data . shape [ <NUM_LIT:1> ] ) ) <EOL> assert_equal ( clf . intercept_ . shape , ( <NUM_LIT:1> , ) ) <EOL> assert_array_equal ( clf . predict ( iris . data ) , target ) <EOL> mlr = LogisticRegression ( solver = solver , multi_class = '<STR_LIT>' , <EOL> random_state = <NUM_LIT> , fit_intercept = False ) <EOL> mlr . fit ( iris . data , target ) <EOL> pred = clf . classes_ [ np . argmax ( clf . predict_log_proba ( iris . data ) , <EOL> axis = <NUM_LIT:1> ) ] <EOL> assert_greater ( np . mean ( pred == target ) , <NUM_LIT> ) <EOL> def test_sparsify ( ) : <EOL> n_samples , n_features = iris . data . shape <EOL> target = iris . target_names [ iris . target ] <EOL> clf = LogisticRegression ( random_state = <NUM_LIT:0> ) . fit ( iris . data , target ) <EOL> pred_d_d = clf . decision_function ( iris . data ) <EOL> clf . sparsify ( ) <EOL> assert_true ( sp . issparse ( clf . coef_ ) ) <EOL> pred_s_d = clf . decision_function ( iris . data ) <EOL> sp_data = sp . coo_matrix ( iris . data ) <EOL> pred_s_s = clf . decision_function ( sp_data ) <EOL> clf . densify ( ) <EOL> pred_d_s = clf . decision_function ( sp_data ) <EOL> assert_array_almost_equal ( pred_d_d , pred_s_d ) <EOL> assert_array_almost_equal ( pred_d_d , pred_s_s ) <EOL> assert_array_almost_equal ( pred_d_d , pred_d_s ) <EOL> def test_inconsistent_input ( ) : <EOL> rng = np . random . RandomState ( <NUM_LIT:0> ) <EOL> X_ = rng . random_sample ( ( <NUM_LIT:5> , <NUM_LIT:10> ) ) <EOL> y_ = np . ones ( X_ . shape [ <NUM_LIT:0> ] ) <EOL> y_ [ <NUM_LIT:0> ] = <NUM_LIT:0> <EOL> clf = LogisticRegression ( random_state = <NUM_LIT:0> ) <EOL> y_wrong = y_ [ : - <NUM_LIT:1> ] <EOL> assert_raises ( ValueError , clf . fit , X , y_wrong ) <EOL> assert_raises ( ValueError , clf . fit ( X_ , y_ ) . predict , <EOL> rng . random_sample ( ( <NUM_LIT:3> , <NUM_LIT:12> ) ) ) <EOL> def test_write_parameters ( ) : <EOL> clf = LogisticRegression ( random_state = <NUM_LIT:0> ) <EOL> clf . fit ( X , Y1 ) <EOL> clf . coef_ [ : ] = <NUM_LIT:0> <EOL> clf . intercept_ [ : ] = <NUM_LIT:0> <EOL> assert_array_almost_equal ( clf . decision_function ( X ) , <NUM_LIT:0> ) <EOL> @ raises ( ValueError ) <EOL> def test_nan ( ) : <EOL> Xnan = np . array ( X , dtype = np . float64 ) <EOL> Xnan [ <NUM_LIT:0> , <NUM_LIT:1> ] = np . nan <EOL> LogisticRegression ( random_state = <NUM_LIT:0> ) . fit ( Xnan , Y1 ) <EOL> def test_consistency_path ( ) : <EOL> rng = np . random . RandomState ( <NUM_LIT:0> ) <EOL> X = np . concatenate ( ( rng . randn ( <NUM_LIT:100> , <NUM_LIT:2> ) + [ <NUM_LIT:1> , <NUM_LIT:1> ] , rng . randn ( <NUM_LIT:100> , <NUM_LIT:2> ) ) ) <EOL> y = [ <NUM_LIT:1> ] * <NUM_LIT:100> + [ - <NUM_LIT:1> ] * <NUM_LIT:100> <EOL> Cs = np . logspace ( <NUM_LIT:0> , <NUM_LIT:4> , <NUM_LIT:10> ) <EOL> f = ignore_warnings <EOL> for solver in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> coefs , Cs , _ = f ( logistic_regression_path ) ( <EOL> X , y , Cs = Cs , fit_intercept = False , tol = <NUM_LIT> , solver = solver , <EOL> random_state = <NUM_LIT:0> ) <EOL> for i , C in enumerate ( Cs ) : <EOL> lr = LogisticRegression ( C = C , fit_intercept = False , tol = <NUM_LIT> , <EOL> random_state = <NUM_LIT:0> ) <EOL> lr . fit ( X , y ) <EOL> lr_coef = lr . coef_ . ravel ( ) <EOL> assert_array_almost_equal ( lr_coef , coefs [ i ] , decimal = <NUM_LIT:4> , <EOL> err_msg = "<STR_LIT>" % solver ) <EOL> for solver in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> Cs = [ <NUM_LIT> ] <EOL> coefs , Cs , _ = f ( logistic_regression_path ) ( <EOL> X , y , Cs = Cs , fit_intercept = True , tol = <NUM_LIT> , solver = solver , <EOL> intercept_scaling = <NUM_LIT> , random_state = <NUM_LIT:0> ) <EOL> lr = LogisticRegression ( C = Cs [ <NUM_LIT:0> ] , fit_intercept = True , tol = <NUM_LIT> , <EOL> intercept_scaling = <NUM_LIT> , random_state = <NUM_LIT:0> ) <EOL> lr . fit ( X , y ) <EOL> lr_coef = np . concatenate ( [ lr . coef_ . ravel ( ) , lr . intercept_ ] ) <EOL> assert_array_almost_equal ( lr_coef , coefs [ <NUM_LIT:0> ] , decimal = <NUM_LIT:4> , <EOL> err_msg = "<STR_LIT>" % solver ) <EOL> def test_liblinear_dual_random_state ( ) : <EOL> X , y = make_classification ( n_samples = <NUM_LIT:20> ) <EOL> lr1 = LogisticRegression ( random_state = <NUM_LIT:0> , dual = True , max_iter = <NUM_LIT:1> , tol = <NUM_LIT> ) <EOL> lr1 . fit ( X , y ) <EOL> lr2 = LogisticRegression ( random_state = <NUM_LIT:0> , dual = True , max_iter = <NUM_LIT:1> , tol = <NUM_LIT> ) <EOL> lr2 . fit ( X , y ) <EOL> lr3 = LogisticRegression ( random_state = <NUM_LIT:8> , dual = True , max_iter = <NUM_LIT:1> , tol = <NUM_LIT> ) <EOL> lr3 . fit ( X , y ) <EOL> assert_array_almost_equal ( lr1 . coef_ , lr2 . coef_ ) <EOL> msg = "<STR_LIT>" <EOL> assert_raise_message ( AssertionError , msg , <EOL> assert_array_almost_equal , lr1 . coef_ , lr3 . coef_ ) <EOL> def test_logistic_loss_and_grad ( ) : <EOL> X_ref , y = make_classification ( n_samples = <NUM_LIT:20> ) <EOL> n_features = X_ref . shape [ <NUM_LIT:1> ] <EOL> X_sp = X_ref . copy ( ) <EOL> X_sp [ X_sp < <NUM_LIT> ] = <NUM_LIT:0> <EOL> X_sp = sp . csr_matrix ( X_sp ) <EOL> for X in ( X_ref , X_sp ) : <EOL> w = np . zeros ( n_features ) <EOL> loss , grad = _logistic_loss_and_grad ( w , X , y , alpha = <NUM_LIT:1.> ) <EOL> approx_grad = optimize . approx_fprime ( <EOL> w , lambda w : _logistic_loss_and_grad ( w , X , y , alpha = <NUM_LIT:1.> ) [ <NUM_LIT:0> ] , <NUM_LIT> <EOL> ) <EOL> assert_array_almost_equal ( grad , approx_grad , decimal = <NUM_LIT:2> ) <EOL> w = np . zeros ( n_features + <NUM_LIT:1> ) <EOL> loss_interp , grad_interp = _logistic_loss_and_grad ( <EOL> w , X , y , alpha = <NUM_LIT:1.> <EOL> ) <EOL> assert_array_almost_equal ( loss , loss_interp ) <EOL> approx_grad = optimize . approx_fprime ( <EOL> w , lambda w : _logistic_loss_and_grad ( w , X , y , alpha = <NUM_LIT:1.> ) [ <NUM_LIT:0> ] , <NUM_LIT> <EOL> ) <EOL> assert_array_almost_equal ( grad_interp , approx_grad , decimal = <NUM_LIT:2> ) <EOL> def test_logistic_grad_hess ( ) : <EOL> rng = np . random . RandomState ( <NUM_LIT:0> ) <EOL> n_samples , n_features = <NUM_LIT:50> , <NUM_LIT:5> <EOL> X_ref = rng . randn ( n_samples , n_features ) <EOL> y = np . sign ( X_ref . dot ( <NUM_LIT:5> * rng . randn ( n_features ) ) ) <EOL> X_ref -= X_ref . mean ( ) <EOL> X_ref /= X_ref . std ( ) <EOL> X_sp = X_ref . copy ( ) <EOL> X_sp [ X_sp < <NUM_LIT> ] = <NUM_LIT:0> <EOL> X_sp = sp . csr_matrix ( X_sp ) <EOL> for X in ( X_ref , X_sp ) : <EOL> w = <NUM_LIT> * np . ones ( n_features ) <EOL> loss , grad = _logistic_loss_and_grad ( w , X , y , alpha = <NUM_LIT:1.> ) <EOL> grad_2 , hess = _logistic_grad_hess ( w , X , y , alpha = <NUM_LIT:1.> ) <EOL> assert_array_almost_equal ( grad , grad_2 ) <EOL> vector = np . zeros_like ( grad ) <EOL> vector [ <NUM_LIT:1> ] = <NUM_LIT:1> <EOL> hess_col = hess ( vector ) <EOL> e = <NUM_LIT> <EOL> d_x = np . linspace ( - e , e , <NUM_LIT:30> ) <EOL> d_grad = np . array ( [ <EOL> _logistic_loss_and_grad ( w + t * vector , X , y , alpha = <NUM_LIT:1.> ) [ <NUM_LIT:1> ] <EOL> for t in d_x <EOL> ] ) <EOL> d_grad -= d_grad . mean ( axis = <NUM_LIT:0> ) <EOL> approx_hess_col = linalg . lstsq ( d_x [ : , np . newaxis ] , d_grad ) [ <NUM_LIT:0> ] . ravel ( ) <EOL> assert_array_almost_equal ( approx_hess_col , hess_col , decimal = <NUM_LIT:3> ) <EOL> w = np . zeros ( n_features + <NUM_LIT:1> ) <EOL> loss_interp , grad_interp = _logistic_loss_and_grad ( w , X , y , alpha = <NUM_LIT:1.> ) <EOL> loss_interp_2 = _logistic_loss ( w , X , y , alpha = <NUM_LIT:1.> ) <EOL> grad_interp_2 , hess = _logistic_grad_hess ( w , X , y , alpha = <NUM_LIT:1.> ) <EOL> assert_array_almost_equal ( loss_interp , loss_interp_2 ) <EOL> assert_array_almost_equal ( grad_interp , grad_interp_2 ) <EOL> def test_logistic_cv ( ) : <EOL> n_samples , n_features = <NUM_LIT:50> , <NUM_LIT:5> <EOL> rng = np . random . RandomState ( <NUM_LIT:0> ) <EOL> X_ref = rng . randn ( n_samples , n_features ) <EOL> y = np . sign ( X_ref . dot ( <NUM_LIT:5> * rng . randn ( n_features ) ) ) <EOL> X_ref -= X_ref . mean ( ) <EOL> X_ref /= X_ref . std ( ) <EOL> lr_cv = LogisticRegressionCV ( Cs = [ <NUM_LIT:1.> ] , fit_intercept = False , <EOL> solver = '<STR_LIT>' ) <EOL> lr_cv . fit ( X_ref , y ) <EOL> lr = LogisticRegression ( C = <NUM_LIT:1.> , fit_intercept = False ) <EOL> lr . fit ( X_ref , y ) <EOL> assert_array_almost_equal ( lr . coef_ , lr_cv . coef_ ) <EOL> assert_array_equal ( lr_cv . coef_ . shape , ( <NUM_LIT:1> , n_features ) ) <EOL> assert_array_equal ( lr_cv . classes_ , [ - <NUM_LIT:1> , <NUM_LIT:1> ] ) <EOL> assert_equal ( len ( lr_cv . classes_ ) , <NUM_LIT:2> ) <EOL> coefs_paths = np . asarray ( list ( lr_cv . coefs_paths_ . values ( ) ) ) <EOL> assert_array_equal ( coefs_paths . shape , ( <NUM_LIT:1> , <NUM_LIT:3> , <NUM_LIT:1> , n_features ) ) <EOL> assert_array_equal ( lr_cv . Cs_ . shape , ( <NUM_LIT:1> , ) ) <EOL> scores = np . asarray ( list ( lr_cv . scores_ . values ( ) ) ) <EOL> assert_array_equal ( scores . shape , ( <NUM_LIT:1> , <NUM_LIT:3> , <NUM_LIT:1> ) ) <EOL> def test_logistic_cv_sparse ( ) : <EOL> X , y = make_classification ( n_samples = <NUM_LIT:50> , n_features = <NUM_LIT:5> , <EOL> random_state = <NUM_LIT:0> ) <EOL> X [ X < <NUM_LIT:1.0> ] = <NUM_LIT:0.0> <EOL> csr = sp . csr_matrix ( X ) <EOL> clf = LogisticRegressionCV ( fit_intercept = True ) <EOL> clf . fit ( X , y ) <EOL> clfs = LogisticRegressionCV ( fit_intercept = True ) <EOL> clfs . fit ( csr , y ) <EOL> assert_array_almost_equal ( clfs . coef_ , clf . coef_ ) <EOL> assert_array_almost_equal ( clfs . intercept_ , clf . intercept_ ) <EOL> assert_equal ( clfs . C_ , clf . C_ ) <EOL> def test_intercept_logistic_helper ( ) : <EOL> n_samples , n_features = <NUM_LIT:10> , <NUM_LIT:5> <EOL> X , y = make_classification ( n_samples = n_samples , n_features = n_features , <EOL> random_state = <NUM_LIT:0> ) <EOL> alpha = <NUM_LIT:1.> <EOL> w = np . ones ( n_features + <NUM_LIT:1> ) <EOL> grad_interp , hess_interp = _logistic_grad_hess ( w , X , y , alpha ) <EOL> loss_interp = _logistic_loss ( w , X , y , alpha ) <EOL> X_ = np . hstack ( ( X , np . ones ( <NUM_LIT:10> ) [ : , np . newaxis ] ) ) <EOL> grad , hess = _logistic_grad_hess ( w , X_ , y , alpha ) <EOL> loss = _logistic_loss ( w , X_ , y , alpha ) <EOL> assert_almost_equal ( loss_interp + <NUM_LIT:0.5> * ( w [ - <NUM_LIT:1> ] ** <NUM_LIT:2> ) , loss ) <EOL> assert_array_almost_equal ( grad_interp [ : n_features ] , grad [ : n_features ] ) <EOL> assert_almost_equal ( grad_interp [ - <NUM_LIT:1> ] + alpha * w [ - <NUM_LIT:1> ] , grad [ - <NUM_LIT:1> ] ) <EOL> rng = np . random . RandomState ( <NUM_LIT:0> ) <EOL> grad = rng . rand ( n_features + <NUM_LIT:1> ) <EOL> hess_interp = hess_interp ( grad ) <EOL> hess = hess ( grad ) <EOL> assert_array_almost_equal ( hess_interp [ : n_features ] , hess [ : n_features ] ) <EOL> assert_almost_equal ( hess_interp [ - <NUM_LIT:1> ] + alpha * grad [ - <NUM_LIT:1> ] , hess [ - <NUM_LIT:1> ] ) <EOL> def test_ovr_multinomial_iris ( ) : <EOL> train , target = iris . data , iris . target <EOL> n_samples , n_features = train . shape <EOL> n_cv = <NUM_LIT:2> <EOL> cv = StratifiedKFold ( n_cv ) <EOL> precomputed_folds = list ( cv . split ( train , target ) ) <EOL> clf = LogisticRegressionCV ( cv = precomputed_folds ) <EOL> clf . fit ( train , target ) <EOL> clf1 = LogisticRegressionCV ( cv = precomputed_folds ) <EOL> target_copy = target . copy ( ) <EOL> target_copy [ target_copy == <NUM_LIT:0> ] = <NUM_LIT:1> <EOL> clf1 . fit ( train , target_copy ) <EOL> assert_array_almost_equal ( clf . scores_ [ <NUM_LIT:2> ] , clf1 . scores_ [ <NUM_LIT:2> ] ) <EOL> assert_array_almost_equal ( clf . intercept_ [ <NUM_LIT:2> : ] , clf1 . intercept_ ) <EOL> assert_array_almost_equal ( clf . coef_ [ <NUM_LIT:2> ] [ np . newaxis , : ] , clf1 . coef_ ) <EOL> assert_equal ( clf . coef_ . shape , ( <NUM_LIT:3> , n_features ) ) <EOL> assert_array_equal ( clf . classes_ , [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> ] ) <EOL> coefs_paths = np . asarray ( list ( clf . coefs_paths_ . values ( ) ) ) <EOL> assert_array_almost_equal ( coefs_paths . shape , ( <NUM_LIT:3> , n_cv , <NUM_LIT:10> , n_features + <NUM_LIT:1> ) ) <EOL> assert_equal ( clf . Cs_ . shape , ( <NUM_LIT:10> , ) ) <EOL> scores = np . asarray ( list ( clf . scores_ . values ( ) ) ) <EOL> assert_equal ( scores . shape , ( <NUM_LIT:3> , n_cv , <NUM_LIT:10> ) ) <EOL> for solver in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> max_iter = <NUM_LIT:100> if solver == '<STR_LIT>' else <NUM_LIT:15> <EOL> clf_multi = LogisticRegressionCV ( <EOL> solver = solver , multi_class = '<STR_LIT>' , max_iter = max_iter , <EOL> random_state = <NUM_LIT> , tol = <NUM_LIT> , cv = <NUM_LIT:2> ) <EOL> clf_multi . fit ( train , target ) <EOL> multi_score = clf_multi . score ( train , target ) <EOL> ovr_score = clf . score ( train , target ) <EOL> assert_greater ( multi_score , ovr_score ) <EOL> assert_equal ( clf . coef_ . shape , clf_multi . coef_ . shape ) <EOL> assert_array_equal ( clf_multi . classes_ , [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> ] ) <EOL> coefs_paths = np . asarray ( list ( clf_multi . coefs_paths_ . values ( ) ) ) <EOL> assert_array_almost_equal ( coefs_paths . shape , ( <NUM_LIT:3> , n_cv , <NUM_LIT:10> , <EOL> n_features + <NUM_LIT:1> ) ) <EOL> assert_equal ( clf_multi . Cs_ . shape , ( <NUM_LIT:10> , ) ) <EOL> scores = np . asarray ( list ( clf_multi . scores_ . values ( ) ) ) <EOL> assert_equal ( scores . shape , ( <NUM_LIT:3> , n_cv , <NUM_LIT:10> ) ) <EOL> def test_logistic_regression_solvers ( ) : <EOL> X , y = make_classification ( n_features = <NUM_LIT:10> , n_informative = <NUM_LIT:5> , random_state = <NUM_LIT:0> ) <EOL> ncg = LogisticRegression ( solver = '<STR_LIT>' , fit_intercept = False ) <EOL> lbf = LogisticRegression ( solver = '<STR_LIT>' , fit_intercept = False ) <EOL> lib = LogisticRegression ( fit_intercept = False ) <EOL> sag = LogisticRegression ( solver = '<STR_LIT>' , fit_intercept = False , <EOL> random_state = <NUM_LIT> ) <EOL> ncg . fit ( X , y ) <EOL> lbf . fit ( X , y ) <EOL> sag . fit ( X , y ) <EOL> lib . fit ( X , y ) <EOL> assert_array_almost_equal ( ncg . coef_ , lib . coef_ , decimal = <NUM_LIT:3> ) <EOL> assert_array_almost_equal ( lib . coef_ , lbf . coef_ , decimal = <NUM_LIT:3> ) <EOL> assert_array_almost_equal ( ncg . coef_ , lbf . coef_ , decimal = <NUM_LIT:3> ) <EOL> assert_array_almost_equal ( sag . coef_ , lib . coef_ , decimal = <NUM_LIT:3> ) <EOL> assert_array_almost_equal ( sag . coef_ , ncg . coef_ , decimal = <NUM_LIT:3> ) <EOL> assert_array_almost_equal ( sag . coef_ , lbf . coef_ , decimal = <NUM_LIT:3> ) <EOL> def test_logistic_regression_solvers_multiclass ( ) : <EOL> X , y = make_classification ( n_samples = <NUM_LIT:20> , n_features = <NUM_LIT:20> , n_informative = <NUM_LIT:10> , <EOL> n_classes = <NUM_LIT:3> , random_state = <NUM_LIT:0> ) <EOL> tol = <NUM_LIT> <EOL> ncg = LogisticRegression ( solver = '<STR_LIT>' , fit_intercept = False , tol = tol ) <EOL> lbf = LogisticRegression ( solver = '<STR_LIT>' , fit_intercept = False , tol = tol ) <EOL> lib = LogisticRegression ( fit_intercept = False , tol = tol ) <EOL> sag = LogisticRegression ( solver = '<STR_LIT>' , fit_intercept = False , tol = tol , <EOL> max_iter = <NUM_LIT:1000> , random_state = <NUM_LIT> ) <EOL> ncg . fit ( X , y ) <EOL> lbf . fit ( X , y ) <EOL> sag . fit ( X , y ) <EOL> lib . fit ( X , y ) <EOL> assert_array_almost_equal ( ncg . coef_ , lib . coef_ , decimal = <NUM_LIT:4> ) <EOL> assert_array_almost_equal ( lib . coef_ , lbf . coef_ , decimal = <NUM_LIT:4> ) <EOL> assert_array_almost_equal ( ncg . coef_ , lbf . coef_ , decimal = <NUM_LIT:4> ) <EOL> assert_array_almost_equal ( sag . coef_ , lib . coef_ , decimal = <NUM_LIT:4> ) <EOL> assert_array_almost_equal ( sag . coef_ , ncg . coef_ , decimal = <NUM_LIT:4> ) <EOL> assert_array_almost_equal ( sag . coef_ , lbf . coef_ , decimal = <NUM_LIT:4> ) <EOL> def test_logistic_regressioncv_class_weights ( ) : <EOL> X , y = make_classification ( n_samples = <NUM_LIT:20> , n_features = <NUM_LIT:20> , n_informative = <NUM_LIT:10> , <EOL> n_classes = <NUM_LIT:3> , random_state = <NUM_LIT:0> ) <EOL> msg = ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> clf_lib = LogisticRegressionCV ( class_weight = { <NUM_LIT:0> : <NUM_LIT:0.1> , <NUM_LIT:1> : <NUM_LIT> } , <EOL> solver = '<STR_LIT>' ) <EOL> assert_raise_message ( ValueError , msg , clf_lib . fit , X , y ) <EOL> y_ = y . copy ( ) <EOL> y_ [ y == <NUM_LIT:2> ] = <NUM_LIT:1> <EOL> clf_lib . fit ( X , y_ ) <EOL> assert_array_equal ( clf_lib . classes_ , [ <NUM_LIT:0> , <NUM_LIT:1> ] ) <EOL> X , y = make_classification ( n_samples = <NUM_LIT:20> , n_features = <NUM_LIT:20> , n_informative = <NUM_LIT:10> , <EOL> random_state = <NUM_LIT:0> ) <EOL> clf_lbf = LogisticRegressionCV ( solver = '<STR_LIT>' , fit_intercept = False , <EOL> class_weight = '<STR_LIT>' ) <EOL> clf_lbf . fit ( X , y ) <EOL> clf_lib = LogisticRegressionCV ( solver = '<STR_LIT>' , fit_intercept = False , <EOL> class_weight = '<STR_LIT>' ) <EOL> clf_lib . fit ( X , y ) <EOL> clf_sag = LogisticRegressionCV ( solver = '<STR_LIT>' , fit_intercept = False , <EOL> class_weight = '<STR_LIT>' , max_iter = <NUM_LIT> ) <EOL> clf_sag . fit ( X , y ) <EOL> assert_array_almost_equal ( clf_lib . coef_ , clf_lbf . coef_ , decimal = <NUM_LIT:4> ) <EOL> assert_array_almost_equal ( clf_sag . coef_ , clf_lbf . coef_ , decimal = <NUM_LIT:4> ) <EOL> assert_array_almost_equal ( clf_lib . coef_ , clf_sag . coef_ , decimal = <NUM_LIT:4> ) <EOL> def test_logistic_regression_sample_weights ( ) : <EOL> X , y = make_classification ( n_samples = <NUM_LIT:20> , n_features = <NUM_LIT:5> , n_informative = <NUM_LIT:3> , <EOL> n_classes = <NUM_LIT:2> , random_state = <NUM_LIT:0> ) <EOL> sample_weight = y + <NUM_LIT:1> <EOL> for LR in [ LogisticRegression , LogisticRegressionCV ] : <EOL> for solver in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> clf_sw_none = LR ( solver = solver , fit_intercept = False ) <EOL> clf_sw_none . fit ( X , y ) <EOL> clf_sw_ones = LR ( solver = solver , fit_intercept = False ) <EOL> clf_sw_ones . fit ( X , y , sample_weight = np . ones ( y . shape [ <NUM_LIT:0> ] ) ) <EOL> assert_array_almost_equal ( <EOL> clf_sw_none . coef_ , clf_sw_ones . coef_ , decimal = <NUM_LIT:4> ) <EOL> clf_sw_lbfgs = LR ( solver = '<STR_LIT>' , fit_intercept = False ) <EOL> clf_sw_lbfgs . fit ( X , y , sample_weight = sample_weight ) <EOL> clf_sw_n = LR ( solver = '<STR_LIT>' , fit_intercept = False ) <EOL> clf_sw_n . fit ( X , y , sample_weight = sample_weight ) <EOL> clf_sw_sag = LR ( solver = '<STR_LIT>' , fit_intercept = False , tol = <NUM_LIT> ) <EOL> with ignore_warnings ( ) : <EOL> clf_sw_sag . fit ( X , y , sample_weight = sample_weight ) <EOL> clf_sw_liblinear = LR ( solver = '<STR_LIT>' , fit_intercept = False ) <EOL> clf_sw_liblinear . fit ( X , y , sample_weight = sample_weight ) <EOL> assert_array_almost_equal ( <EOL> clf_sw_lbfgs . coef_ , clf_sw_n . coef_ , decimal = <NUM_LIT:4> ) <EOL> assert_array_almost_equal ( <EOL> clf_sw_lbfgs . coef_ , clf_sw_sag . coef_ , decimal = <NUM_LIT:4> ) <EOL> assert_array_almost_equal ( <EOL> clf_sw_lbfgs . coef_ , clf_sw_liblinear . coef_ , decimal = <NUM_LIT:4> ) <EOL> for solver in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> clf_cw_12 = LR ( solver = solver , fit_intercept = False , <EOL> class_weight = { <NUM_LIT:0> : <NUM_LIT:1> , <NUM_LIT:1> : <NUM_LIT:2> } ) <EOL> clf_cw_12 . fit ( X , y ) <EOL> clf_sw_12 = LR ( solver = solver , fit_intercept = False ) <EOL> clf_sw_12 . fit ( X , y , sample_weight = sample_weight ) <EOL> assert_array_almost_equal ( <EOL> clf_cw_12 . coef_ , clf_sw_12 . coef_ , decimal = <NUM_LIT:4> ) <EOL> clf_cw = LogisticRegression ( <EOL> solver = "<STR_LIT>" , fit_intercept = False , class_weight = { <NUM_LIT:0> : <NUM_LIT:1> , <NUM_LIT:1> : <NUM_LIT:2> } , <EOL> penalty = "<STR_LIT>" ) <EOL> clf_cw . fit ( X , y ) <EOL> clf_sw = LogisticRegression ( <EOL> solver = "<STR_LIT>" , fit_intercept = False , penalty = "<STR_LIT>" ) <EOL> clf_sw . fit ( X , y , sample_weight ) <EOL> assert_array_almost_equal ( clf_cw . coef_ , clf_sw . coef_ , decimal = <NUM_LIT:4> ) <EOL> clf_cw = LogisticRegression ( <EOL> solver = "<STR_LIT>" , fit_intercept = False , class_weight = { <NUM_LIT:0> : <NUM_LIT:1> , <NUM_LIT:1> : <NUM_LIT:2> } , <EOL> penalty = "<STR_LIT>" , dual = True ) <EOL> clf_cw . fit ( X , y ) <EOL> clf_sw = LogisticRegression ( <EOL> solver = "<STR_LIT>" , fit_intercept = False , penalty = "<STR_LIT>" , dual = True ) <EOL> clf_sw . fit ( X , y , sample_weight ) <EOL> assert_array_almost_equal ( clf_cw . coef_ , clf_sw . coef_ , decimal = <NUM_LIT:4> ) <EOL> def _compute_class_weight_dictionary ( y ) : <EOL> classes = np . unique ( y ) <EOL> class_weight = compute_class_weight ( "<STR_LIT>" , classes , y ) <EOL> class_weight_dict = dict ( zip ( classes , class_weight ) ) <EOL> return class_weight_dict <EOL> def test_logistic_regression_class_weights ( ) : <EOL> X = iris . data [ <NUM_LIT> : , : ] <EOL> y = iris . target [ <NUM_LIT> : ] <EOL> solvers = ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> class_weight_dict = _compute_class_weight_dictionary ( y ) <EOL> for solver in solvers : <EOL> clf1 = LogisticRegression ( solver = solver , multi_class = "<STR_LIT>" , <EOL> class_weight = "<STR_LIT>" ) <EOL> clf2 = LogisticRegression ( solver = solver , multi_class = "<STR_LIT>" , <EOL> class_weight = class_weight_dict ) <EOL> clf1 . fit ( X , y ) <EOL> clf2 . fit ( X , y ) <EOL> assert_array_almost_equal ( clf1 . coef_ , clf2 . coef_ , decimal = <NUM_LIT:4> ) <EOL> X = iris . data [ <NUM_LIT> : <NUM_LIT:100> , : ] <EOL> y = iris . target [ <NUM_LIT> : <NUM_LIT:100> ] <EOL> solvers = ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> class_weight_dict = _compute_class_weight_dictionary ( y ) <EOL> for solver in solvers : <EOL> clf1 = LogisticRegression ( solver = solver , multi_class = "<STR_LIT>" , <EOL> class_weight = "<STR_LIT>" ) <EOL> clf2 = LogisticRegression ( solver = solver , multi_class = "<STR_LIT>" , <EOL> class_weight = class_weight_dict ) <EOL> clf1 . fit ( X , y ) <EOL> clf2 . fit ( X , y ) <EOL> assert_array_almost_equal ( clf1 . coef_ , clf2 . coef_ , decimal = <NUM_LIT:6> ) <EOL> def test_multinomial_logistic_regression_with_classweight_auto ( ) : <EOL> X , y = iris . data , iris . target <EOL> model = LogisticRegression ( multi_class = '<STR_LIT>' , <EOL> class_weight = '<STR_LIT>' , solver = '<STR_LIT>' ) <EOL> assert_warns_message ( DeprecationWarning , <EOL> "<STR_LIT>" , <EOL> model . fit , X , y ) <EOL> def test_logistic_regression_convergence_warnings ( ) : <EOL> X , y = make_classification ( n_samples = <NUM_LIT:20> , n_features = <NUM_LIT:20> ) <EOL> clf_lib = LogisticRegression ( solver = '<STR_LIT>' , max_iter = <NUM_LIT:2> , verbose = <NUM_LIT:1> ) <EOL> assert_warns ( ConvergenceWarning , clf_lib . fit , X , y ) <EOL> assert_equal ( clf_lib . n_iter_ , <NUM_LIT:2> ) <EOL> def test_logistic_regression_multinomial ( ) : <EOL> n_samples , n_features , n_classes = <NUM_LIT:50> , <NUM_LIT:20> , <NUM_LIT:3> <EOL> X , y = make_classification ( n_samples = n_samples , <EOL> n_features = n_features , <EOL> n_informative = <NUM_LIT:10> , <EOL> n_classes = n_classes , random_state = <NUM_LIT:0> ) <EOL> solver = '<STR_LIT>' <EOL> ref_i = LogisticRegression ( solver = solver , multi_class = '<STR_LIT>' ) <EOL> ref_w = LogisticRegression ( solver = solver , multi_class = '<STR_LIT>' , <EOL> fit_intercept = False ) <EOL> ref_i . fit ( X , y ) <EOL> ref_w . fit ( X , y ) <EOL> assert_array_equal ( ref_i . coef_ . shape , ( n_classes , n_features ) ) <EOL> assert_array_equal ( ref_w . coef_ . shape , ( n_classes , n_features ) ) <EOL> for solver in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> clf_i = LogisticRegression ( solver = solver , multi_class = '<STR_LIT>' , <EOL> random_state = <NUM_LIT> , max_iter = <NUM_LIT:1000> , tol = <NUM_LIT> ) <EOL> clf_w = LogisticRegression ( solver = solver , multi_class = '<STR_LIT>' , <EOL> random_state = <NUM_LIT> , max_iter = <NUM_LIT:1000> , tol = <NUM_LIT> , <EOL> fit_intercept = False ) <EOL> clf_i . fit ( X , y ) <EOL> clf_w . fit ( X , y ) <EOL> assert_array_equal ( clf_i . coef_ . shape , ( n_classes , n_features ) ) <EOL> assert_array_equal ( clf_w . coef_ . shape , ( n_classes , n_features ) ) <EOL> assert_almost_equal ( ref_i . coef_ , clf_i . coef_ , decimal = <NUM_LIT:3> ) <EOL> assert_almost_equal ( ref_w . coef_ , clf_w . coef_ , decimal = <NUM_LIT:3> ) <EOL> assert_almost_equal ( ref_i . intercept_ , clf_i . intercept_ , decimal = <NUM_LIT:3> ) <EOL> for solver in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> clf_path = LogisticRegressionCV ( solver = solver , max_iter = <NUM_LIT> , tol = <NUM_LIT> , <EOL> multi_class = '<STR_LIT>' , Cs = [ <NUM_LIT:1.> ] ) <EOL> clf_path . fit ( X , y ) <EOL> assert_array_almost_equal ( clf_path . coef_ , ref_i . coef_ , decimal = <NUM_LIT:3> ) <EOL> assert_almost_equal ( clf_path . intercept_ , ref_i . intercept_ , decimal = <NUM_LIT:3> ) <EOL> def test_multinomial_grad_hess ( ) : <EOL> rng = np . random . RandomState ( <NUM_LIT:0> ) <EOL> n_samples , n_features , n_classes = <NUM_LIT:100> , <NUM_LIT:5> , <NUM_LIT:3> <EOL> X = rng . randn ( n_samples , n_features ) <EOL> w = rng . rand ( n_classes , n_features ) <EOL> Y = np . zeros ( ( n_samples , n_classes ) ) <EOL> ind = np . argmax ( np . dot ( X , w . T ) , axis = <NUM_LIT:1> ) <EOL> Y [ range ( <NUM_LIT:0> , n_samples ) , ind ] = <NUM_LIT:1> <EOL> w = w . ravel ( ) <EOL> sample_weights = np . ones ( X . shape [ <NUM_LIT:0> ] ) <EOL> grad , hessp = _multinomial_grad_hess ( w , X , Y , alpha = <NUM_LIT:1.> , <EOL> sample_weight = sample_weights ) <EOL> vec = np . zeros ( n_features * n_classes ) <EOL> vec [ <NUM_LIT:0> ] = <NUM_LIT:1> <EOL> hess_col = hessp ( vec ) <EOL> e = <NUM_LIT> <EOL> d_x = np . linspace ( - e , e , <NUM_LIT:30> ) <EOL> d_grad = np . array ( [ <EOL> _multinomial_grad_hess ( w + t * vec , X , Y , alpha = <NUM_LIT:1.> , <EOL> sample_weight = sample_weights ) [ <NUM_LIT:0> ] <EOL> for t in d_x <EOL> ] ) <EOL> d_grad -= d_grad . mean ( axis = <NUM_LIT:0> ) <EOL> approx_hess_col = linalg . lstsq ( d_x [ : , np . newaxis ] , d_grad ) [ <NUM_LIT:0> ] . ravel ( ) <EOL> assert_array_almost_equal ( hess_col , approx_hess_col ) <EOL> def test_liblinear_decision_function_zero ( ) : <EOL> X , y = make_classification ( n_samples = <NUM_LIT:5> , n_features = <NUM_LIT:5> ) <EOL> clf = LogisticRegression ( fit_intercept = False ) <EOL> clf . fit ( X , y ) <EOL> X = np . zeros ( ( <NUM_LIT:5> , <NUM_LIT:5> ) ) <EOL> assert_array_equal ( clf . predict ( X ) , np . zeros ( <NUM_LIT:5> ) ) <EOL> def test_liblinear_logregcv_sparse ( ) : <EOL> X , y = make_classification ( n_samples = <NUM_LIT:10> , n_features = <NUM_LIT:5> ) <EOL> clf = LogisticRegressionCV ( solver = '<STR_LIT>' ) <EOL> clf . fit ( sparse . csr_matrix ( X ) , y ) <EOL> def test_logreg_intercept_scaling ( ) : <EOL> for i in [ - <NUM_LIT:1> , <NUM_LIT:0> ] : <EOL> clf = LogisticRegression ( intercept_scaling = i ) <EOL> msg = ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % clf . intercept_scaling ) <EOL> assert_raise_message ( ValueError , msg , clf . fit , X , Y1 ) <EOL> def test_logreg_intercept_scaling_zero ( ) : <EOL> clf = LogisticRegression ( fit_intercept = False ) <EOL> clf . fit ( X , Y1 ) <EOL> assert_equal ( clf . intercept_ , <NUM_LIT:0.> ) <EOL> def test_logreg_cv_penalty ( ) : <EOL> X , y = make_classification ( n_samples = <NUM_LIT:50> , n_features = <NUM_LIT:20> , random_state = <NUM_LIT:0> ) <EOL> lr_cv = LogisticRegressionCV ( penalty = "<STR_LIT>" , Cs = [ <NUM_LIT:1.0> ] , solver = '<STR_LIT>' ) <EOL> lr_cv . fit ( X , y ) <EOL> lr = LogisticRegression ( penalty = "<STR_LIT>" , C = <NUM_LIT:1.0> , solver = '<STR_LIT>' ) <EOL> lr . fit ( X , y ) <EOL> assert_equal ( np . count_nonzero ( lr_cv . coef_ ) , np . count_nonzero ( lr . coef_ ) ) <EOL> def test_logreg_predict_proba_multinomial ( ) : <EOL> X , y = make_classification ( n_samples = <NUM_LIT:10> , n_features = <NUM_LIT:20> , random_state = <NUM_LIT:0> , <EOL> n_classes = <NUM_LIT:3> , n_informative = <NUM_LIT:10> ) <EOL> clf_multi = LogisticRegression ( multi_class = "<STR_LIT>" , solver = "<STR_LIT>" ) <EOL> clf_multi . fit ( X , y ) <EOL> clf_multi_loss = log_loss ( y , clf_multi . predict_proba ( X ) ) <EOL> clf_ovr = LogisticRegression ( multi_class = "<STR_LIT>" , solver = "<STR_LIT>" ) <EOL> clf_ovr . fit ( X , y ) <EOL> clf_ovr_loss = log_loss ( y , clf_ovr . predict_proba ( X ) ) <EOL> assert_greater ( clf_ovr_loss , clf_multi_loss ) <EOL> clf_multi_loss = log_loss ( y , clf_multi . predict_proba ( X ) ) <EOL> clf_wrong_loss = log_loss ( y , clf_multi . _predict_proba_lr ( X ) ) <EOL> assert_greater ( clf_wrong_loss , clf_multi_loss ) <EOL> @ ignore_warnings <EOL> def test_max_iter ( ) : <EOL> X , y_bin = iris . data , iris . target . copy ( ) <EOL> y_bin [ y_bin == <NUM_LIT:2> ] = <NUM_LIT:0> <EOL> solvers = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> if sp_version >= ( <NUM_LIT:0> , <NUM_LIT:12> ) : <EOL> solvers . append ( '<STR_LIT>' ) <EOL> for max_iter in range ( <NUM_LIT:1> , <NUM_LIT:5> ) : <EOL> for solver in solvers : <EOL> for multi_class in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> if solver == '<STR_LIT>' and multi_class == '<STR_LIT>' : <EOL> continue <EOL> lr = LogisticRegression ( max_iter = max_iter , tol = <NUM_LIT> , <EOL> multi_class = multi_class , <EOL> random_state = <NUM_LIT:0> , solver = solver ) <EOL> lr . fit ( X , y_bin ) <EOL> assert_equal ( lr . n_iter_ [ <NUM_LIT:0> ] , max_iter ) <EOL> def test_n_iter ( ) : <EOL> X , y = iris . data , iris . target <EOL> y_bin = y . copy ( ) <EOL> y_bin [ y_bin == <NUM_LIT:2> ] = <NUM_LIT:0> <EOL> n_Cs = <NUM_LIT:4> <EOL> n_cv_fold = <NUM_LIT:2> <EOL> for solver in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> n_classes = <NUM_LIT:1> if solver == '<STR_LIT>' else np . unique ( y ) . shape [ <NUM_LIT:0> ] <EOL> clf = LogisticRegression ( tol = <NUM_LIT> , multi_class = '<STR_LIT>' , <EOL> solver = solver , C = <NUM_LIT:1.> , <EOL> random_state = <NUM_LIT> , max_iter = <NUM_LIT:100> ) <EOL> clf . fit ( X , y ) <EOL> assert_equal ( clf . n_iter_ . shape , ( n_classes , ) ) <EOL> n_classes = np . unique ( y ) . shape [ <NUM_LIT:0> ] <EOL> clf = LogisticRegressionCV ( tol = <NUM_LIT> , multi_class = '<STR_LIT>' , <EOL> solver = solver , Cs = n_Cs , cv = n_cv_fold , <EOL> random_state = <NUM_LIT> , max_iter = <NUM_LIT:100> ) <EOL> clf . fit ( X , y ) <EOL> assert_equal ( clf . n_iter_ . shape , ( n_classes , n_cv_fold , n_Cs ) ) <EOL> clf . fit ( X , y_bin ) <EOL> assert_equal ( clf . n_iter_ . shape , ( <NUM_LIT:1> , n_cv_fold , n_Cs ) ) <EOL> n_classes = <NUM_LIT:1> <EOL> if solver in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> break <EOL> clf = LogisticRegression ( tol = <NUM_LIT> , multi_class = '<STR_LIT>' , <EOL> solver = solver , C = <NUM_LIT:1.> , <EOL> random_state = <NUM_LIT> , max_iter = <NUM_LIT:100> ) <EOL> clf . fit ( X , y ) <EOL> assert_equal ( clf . n_iter_ . shape , ( n_classes , ) ) <EOL> clf = LogisticRegressionCV ( tol = <NUM_LIT> , multi_class = '<STR_LIT>' , <EOL> solver = solver , Cs = n_Cs , cv = n_cv_fold , <EOL> random_state = <NUM_LIT> , max_iter = <NUM_LIT:100> ) <EOL> clf . fit ( X , y ) <EOL> assert_equal ( clf . n_iter_ . shape , ( n_classes , n_cv_fold , n_Cs ) ) <EOL> clf . fit ( X , y_bin ) <EOL> assert_equal ( clf . n_iter_ . shape , ( <NUM_LIT:1> , n_cv_fold , n_Cs ) ) <EOL> @ ignore_warnings <EOL> def test_warm_start ( ) : <EOL> X , y = iris . data , iris . target <EOL> solvers = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> if sp_version >= ( <NUM_LIT:0> , <NUM_LIT:12> ) : <EOL> solvers . append ( '<STR_LIT>' ) <EOL> for warm_start in [ True , False ] : <EOL> for fit_intercept in [ True , False ] : <EOL> for solver in solvers : <EOL> for multi_class in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> clf = LogisticRegression ( tol = <NUM_LIT> , multi_class = multi_class , <EOL> warm_start = warm_start , <EOL> solver = solver , <EOL> random_state = <NUM_LIT> , max_iter = <NUM_LIT:100> , <EOL> fit_intercept = fit_intercept ) <EOL> clf . fit ( X , y ) <EOL> coef_1 = clf . coef_ <EOL> clf . max_iter = <NUM_LIT:1> <EOL> with ignore_warnings ( ) : <EOL> clf . fit ( X , y ) <EOL> cum_diff = np . sum ( np . abs ( coef_1 - clf . coef_ ) ) <EOL> msg = ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % ( solver , multi_class , str ( fit_intercept ) , <EOL> str ( warm_start ) ) ) <EOL> if warm_start : <EOL> assert_greater ( <NUM_LIT> , cum_diff , msg ) <EOL> else : <EOL> assert_greater ( cum_diff , <NUM_LIT> , msg ) </s>
