#NEWLINE# def logistic_regression_path( X , y , pos_class = None , Cs = 10 , fit_intercept = True , max_iter = 100 , tol = 0.0001 , verbose = 0 , solver = 'lbfgs' , coef = None , copy = False , class_weight = None , dual = False , penalty = 'l2' , intercept_scaling = 1.0 , multi_class = 'ovr' , random_state = None , check_input = True , max_squared_sum = None , sample_weight = None ) : #NEWLINE# #INDENT# 'Compute a Logistic Regression model for a list of regularization\n    parameters.\n\n    This is an implementation that uses the result of the previous model\n    to speed up computations along the set of solutions, making it faster\n    than sequentially calling LogisticRegression for the different parameters.\n\n    Read more in the :ref:`User Guide <logistic_regression>`.\n\n    Parameters\n    ----------\n    X : array-like or sparse matrix, shape (n_samples, n_features)\n        Input data.\n\n    y : array-like, shape (n_samples,)\n        Input data, target values.\n\n    Cs : int | array-like, shape (n_cs,)\n        List of values for the regularization parameter or integer specifying\n        the number of regularization parameters that should be used. In this\n        case, the parameters will be chosen in a logarithmic scale between\n        1e-4 and 1e4.\n\n    pos_class : int, None\n        The class with respect to which we perform a one-vs-all fit.\n        If None, then it is assumed that the given problem is binary.\n\n    fit_intercept : bool\n        Whether to fit an intercept for the model. In this case the shape of\n        the returned array is (n_cs, n_features + 1).\n\n    max_iter : int\n        Maximum number of iterations for the solver.\n\n    tol : float\n        Stopping criterion. For the newton-cg and lbfgs solvers, the iteration\n        will stop when ``max{|g_i | i = 1, ..., n} <= tol``\n        where ``g_i`` is the i-th component of the gradient.\n\n    verbose : int\n        For the liblinear and lbfgs solvers set verbose to any positive\n        number for verbosity.\n\n    solver : {\'lbfgs\', \'newton-cg\', \'liblinear\', \'sag\'}\n        Numerical solver to use.\n\n    coef : array-like, shape (n_features,), default None\n        Initialization value for coefficients of logistic regression.\n        Useless for liblinear solver.\n\n    copy : bool, default False\n        Whether or not to produce a copy of the data. A copy is not required\n        anymore. This parameter is deprecated and will be removed in 0.19.\n\n    class_weight : dict or \'balanced\', optional\n        Weights associated with classes in the form ``{class_label: weight}``.\n        If not given, all classes are supposed to have weight one.\n\n        The "balanced" mode uses the values of y to automatically adjust\n        weights inversely proportional to class frequencies in the input data\n        as ``n_samples / (n_classes * np.bincount(y))``.\n\n        Note that these weights will be multiplied with sample_weight (passed\n        through the fit method) if sample_weight is specified.\n\n    dual : bool\n        Dual or primal formulation. Dual formulation is only implemented for\n        l2 penalty with liblinear solver. Prefer dual=False when\n        n_samples > n_features.\n\n    penalty : str, \'l1\' or \'l2\'\n        Used to specify the norm used in the penalization. The \'newton-cg\',\n        \'sag\' and \'lbfgs\' solvers support only l2 penalties.\n\n    intercept_scaling : float, default 1.\n        Useful only when the solver \'liblinear\' is used\n        and self.fit_intercept is set to True. In this case, x becomes\n        [x, self.intercept_scaling],\n        i.e. a "synthetic" feature with constant value equal to\n        intercept_scaling is appended to the instance vector.\n        The intercept becomes ``intercept_scaling * synthetic_feature_weight``.\n\n        Note! the synthetic feature weight is subject to l1/l2 regularization\n        as all other features.\n        To lessen the effect of regularization on synthetic feature weight\n        (and therefore on the intercept) intercept_scaling has to be increased.\n\n    multi_class : str, {\'ovr\', \'multinomial\'}\n        Multiclass option can be either \'ovr\' or \'multinomial\'. If the option\n        chosen is \'ovr\', then a binary problem is fit for each label. Else\n        the loss minimised is the multinomial loss fit across\n        the entire probability distribution. Works only for the \'lbfgs\' and\n        \'newton-cg\' solvers.\n\n    random_state : int seed, RandomState instance, or None (default)\n        The seed of the pseudo random number generator to use when\n        shuffling the data. Used only in solvers \'sag\' and \'liblinear\'.\n\n    check_input : bool, default True\n        If False, the input arrays X and y will not be checked.\n\n    max_squared_sum : float, default None\n        Maximum squared sum of X over samples. Used only in SAG solver.\n        If None, it will be computed, going through all the samples.\n        The value should be precomputed to speed up cross validation.\n\n    sample_weight : array-like, shape(n_samples,) optional\n        Array of weights that are assigned to individual samples.\n        If not provided, then each sample is given unit weight.\n\n    Returns\n    -------\n    coefs : ndarray, shape (n_cs, n_features) or (n_cs, n_features + 1)\n        List of coefficients for the Logistic Regression model. If\n        fit_intercept is set to True then the second dimension will be\n        n_features + 1, where the last item represents the intercept.\n\n    Cs : ndarray\n        Grid of Cs used for cross-validation.\n\n    n_iter : array, shape (n_cs,)\n        Actual number of iteration for each Cs.\n\n    Notes\n    -----\n    You might get slightly different results with the solver liblinear than\n    with the others since this uses LIBLINEAR which penalizes the intercept.\n    ' #NEWLINE# if copy : #NEWLINE# #INDENT# warnings . warn ( "A copy is not required anymore. The 'copy' parameter is deprecated and will be removed in 0.19." , DeprecationWarning ) #NEWLINE# #UNINDENT# if isinstance ( Cs , numbers . Integral ) : #NEWLINE# #INDENT# Cs = np . logspace ( ( - 4 ) , 4 , Cs ) #NEWLINE# #UNINDENT# _check_solver_option ( solver , multi_class , penalty , dual ) #NEWLINE# if ( check_input or copy ) : #NEWLINE# #INDENT# X = check_array ( X , accept_sparse = 'csr' , dtype = np . float64 ) #NEWLINE# y = check_array ( y , ensure_2d = False , copy = copy , dtype = None ) #NEWLINE# check_consistent_length ( X , y ) #NEWLINE# #UNINDENT# ( _ , n_features ) = X . shape #NEWLINE# classes = np . unique ( y ) #NEWLINE# random_state = check_random_state ( random_state ) #NEWLINE# if ( ( pos_class is None ) and ( multi_class != 'multinomial' ) ) : #NEWLINE# #INDENT# if ( classes . size > 2 ) : #NEWLINE# #INDENT# raise ValueError ( 'To fit OvR, use the pos_class argument' ) #NEWLINE# #UNINDENT# pos_class = classes [ 1 ] #NEWLINE# #UNINDENT# if ( sample_weight is not None ) : #NEWLINE# #INDENT# sample_weight = np . array ( sample_weight , dtype = np . float64 , order = 'C' ) #NEWLINE# check_consistent_length ( y , sample_weight ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# sample_weight = np . ones ( X . shape [ 0 ] ) #NEWLINE# #UNINDENT# le = LabelEncoder ( ) #NEWLINE# if ( isinstance ( class_weight , dict ) or ( multi_class == 'multinomial' ) ) : #NEWLINE# #INDENT# if ( solver == 'liblinear' ) : #NEWLINE# #INDENT# if ( classes . size == 2 ) : #NEWLINE# #INDENT# temp = { #NEWLINE# #INDENT# 1 : class_weight [ pos_class ] , #NEWLINE# ( - 1 ) : class_weight [ classes [ 0 ] ] , #NEWLINE# #UNINDENT# } #NEWLINE# class_weight = temp . copy ( ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# raise ValueError ( "In LogisticRegressionCV the liblinear solver cannot handle multiclass with class_weight of type dict. Use the lbfgs, newton-cg or sag solvers or set class_weight='balanced'" ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# class_weight_ = compute_class_weight ( class_weight , classes , y ) #NEWLINE# sample_weight *= class_weight_ [ le . fit_transform ( y ) ] #NEWLINE# #UNINDENT# #UNINDENT# if ( multi_class == 'ovr' ) : #NEWLINE# #INDENT# w0 = np . zeros ( ( n_features + int ( fit_intercept ) ) ) #NEWLINE# mask_classes = np . array ( [ ( - 1 ) , 1 ] ) #NEWLINE# mask = ( y == pos_class ) #NEWLINE# y_bin = np . ones ( y . shape , dtype = np . float64 ) #NEWLINE# y_bin [ ( ~ mask ) ] = ( - 1.0 ) #NEWLINE# if ( class_weight in ( 'auto' , 'balanced' ) ) : #NEWLINE# #INDENT# class_weight_ = compute_class_weight ( class_weight , mask_classes , y_bin ) #NEWLINE# sample_weight *= class_weight_ [ le . fit_transform ( y_bin ) ] #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if ( solver != 'sag' ) : #NEWLINE# #INDENT# lbin = LabelBinarizer ( ) #NEWLINE# Y_multi = lbin . fit_transform ( y ) #NEWLINE# if ( Y_multi . shape [ 1 ] == 1 ) : #NEWLINE# #INDENT# Y_multi = np . hstack ( [ ( 1 - Y_multi ) , Y_multi ] ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# le = LabelEncoder ( ) #NEWLINE# Y_multi = le . fit_transform ( y ) #NEWLINE# #UNINDENT# w0 = np . zeros ( ( classes . size , ( n_features + int ( fit_intercept ) ) ) , order = 'F' ) #NEWLINE# #UNINDENT# if ( coef is not None ) : #NEWLINE# #INDENT# if ( multi_class == 'ovr' ) : #NEWLINE# #INDENT# if ( coef . size not in ( n_features , w0 . size ) ) : #NEWLINE# #INDENT# raise ValueError ( ( 'Initialization coef is of shape %d, expected shape %d or %d' % ( coef . size , n_features , w0 . size ) ) ) #NEWLINE# #UNINDENT# w0 [ : coef . size ] = coef #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# n_classes = classes . size #NEWLINE# if ( n_classes == 2 ) : #NEWLINE# #INDENT# n_classes = 1 #NEWLINE# #UNINDENT# if ( ( coef . shape [ 0 ] != n_classes ) or ( coef . shape [ 1 ] not in ( n_features , ( n_features + 1 ) ) ) ) : #NEWLINE# #INDENT# raise ValueError ( ( 'Initialization coef is of shape (%d, %d), expected shape (%d, %d) or (%d, %d)' % ( coef . shape [ 0 ] , coef . shape [ 1 ] , classes . size , n_features , classes . size , ( n_features + 1 ) ) ) ) #NEWLINE# #UNINDENT# w0 [ : , : coef . shape [ 1 ] ] = coef #NEWLINE# #UNINDENT# #UNINDENT# if ( multi_class == 'multinomial' ) : #NEWLINE# #INDENT# if ( solver in [ 'lbfgs' , 'newton-cg' ] ) : #NEWLINE# #INDENT# w0 = w0 . ravel ( ) #NEWLINE# #UNINDENT# target = Y_multi #NEWLINE# if ( solver == 'lbfgs' ) : #NEWLINE# #INDENT# func = ( lambda x , * args : _multinomial_loss_grad ( x , * args ) [ 0 : 2 ] ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( solver == 'newton-cg' ) : #NEWLINE# #INDENT# func = ( lambda x , * args : _multinomial_loss ( x , * args ) [ 0 ] ) #NEWLINE# grad = ( lambda x , * args : _multinomial_loss_grad ( x , * args ) [ 1 ] ) #NEWLINE# hess = _multinomial_grad_hess #NEWLINE# #UNINDENT# #UNINDENT# warm_start_sag = { #NEWLINE# #INDENT# 'coef' : w0 . T , #NEWLINE# #UNINDENT# } #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# target = y_bin #NEWLINE# if ( solver == 'lbfgs' ) : #NEWLINE# #INDENT# func = _logistic_loss_and_grad #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( solver == 'newton-cg' ) : #NEWLINE# #INDENT# func = _logistic_loss #NEWLINE# grad = ( lambda x , * args : _logistic_loss_and_grad ( x , * args ) [ 1 ] ) #NEWLINE# hess = _logistic_grad_hess #NEWLINE# #UNINDENT# #UNINDENT# warm_start_sag = { #NEWLINE# #INDENT# 'coef' : np . expand_dims ( w0 , axis = 1 ) , #NEWLINE# #UNINDENT# } #NEWLINE# #UNINDENT# coefs = list ( ) #NEWLINE# n_iter = np . zeros ( len ( Cs ) , dtype = np . int32 ) #NEWLINE# for ( i , C ) in enumerate ( Cs ) : #NEWLINE# #INDENT# if ( solver == 'lbfgs' ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# ( w0 , loss , info ) = optimize . fmin_l_bfgs_b ( func , w0 , fprime = None , args = ( X , target , ( 1.0 / C ) , sample_weight ) , iprint = ( ( verbose > 0 ) - 1 ) , pgtol = tol , maxiter = max_iter ) #NEWLINE# #UNINDENT# except TypeError : #NEWLINE# #INDENT# ( w0 , loss , info ) = optimize . fmin_l_bfgs_b ( func , w0 , fprime = None , args = ( X , target , ( 1.0 / C ) , sample_weight ) , iprint = ( ( verbose > 0 ) - 1 ) , pgtol = tol ) #NEWLINE# #UNINDENT# if ( ( info [ 'warnflag' ] == 1 ) and ( verbose > 0 ) ) : #NEWLINE# #INDENT# warnings . warn ( 'lbfgs failed to converge. Increase the number of iterations.' ) #NEWLINE# #UNINDENT# try : #NEWLINE# #INDENT# n_iter_i = ( info [ 'nit' ] - 1 ) #NEWLINE# #UNINDENT# except : #NEWLINE# #INDENT# n_iter_i = ( info [ 'funcalls' ] - 1 ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if ( solver == 'newton-cg' ) : #NEWLINE# #INDENT# args = ( X , target , ( 1.0 / C ) , sample_weight ) #NEWLINE# ( w0 , n_iter_i ) = newton_cg ( hess , func , grad , w0 , args = args , maxiter = max_iter , tol = tol ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( solver == 'liblinear' ) : #NEWLINE# #INDENT# ( coef_ , intercept_ , n_iter_i ) = _fit_liblinear ( X , target , C , fit_intercept , intercept_scaling , class_weight , penalty , dual , verbose , max_iter , tol , random_state , sample_weight = sample_weight ) #NEWLINE# if fit_intercept : #NEWLINE# #INDENT# w0 = np . concatenate ( [ coef_ . ravel ( ) , intercept_ ] ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# w0 = coef_ . ravel ( ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if ( solver == 'sag' ) : #NEWLINE# #INDENT# if ( multi_class == 'multinomial' ) : #NEWLINE# #INDENT# target = target . astype ( np . float64 ) #NEWLINE# loss = 'multinomial' #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# loss = 'log' #NEWLINE# #UNINDENT# ( w0 , n_iter_i , warm_start_sag ) = sag_solver ( X , target , sample_weight , loss , ( 1.0 / C ) , max_iter , tol , verbose , random_state , False , max_squared_sum , warm_start_sag ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# raise ValueError ( ( "solver must be one of {'liblinear', 'lbfgs', 'newton-cg', 'sag'}, got '%s' instead" % solver ) ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# if ( multi_class == 'multinomial' ) : #NEWLINE# #INDENT# multi_w0 = np . reshape ( w0 , ( classes . size , ( - 1 ) ) ) #NEWLINE# if ( classes . size == 2 ) : #NEWLINE# #INDENT# multi_w0 = multi_w0 [ 1 ] [ np . newaxis , : ] #NEWLINE# #UNINDENT# coefs . append ( multi_w0 ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# coefs . append ( w0 . copy ( ) ) #NEWLINE# #UNINDENT# n_iter [ i ] = n_iter_i #NEWLINE# #UNINDENT# return ( coefs , np . array ( Cs ) , n_iter )