
    0Ph/                         d Z ddlZddlZddlmZ ddlmZ ddlm	Z	 ddl
mZ dd	lmZ dd
lmZmZ 	 ddZ	 	 	 	 	 	 	 	 	 	 	 	 ddZdS )z<Solvers for Ridge and LogisticRegression using SAG algorithm    N   )ConvergenceWarning)check_array)	row_norms)_check_sample_weight   )make_dataset)sag32sag64Fc                     |dv rd| t          |          z   z  |z   }n.|dk    r| t          |          z   |z   }nt          d|z            |r"t          d|z  |z  |          }dd|z  |z   z  }nd|z  }|S )a(  Compute automatic step size for SAG solver.

    The step size is set to 1 / (alpha_scaled + L + fit_intercept) where L is
    the max sum of squares for over all samples.

    Parameters
    ----------
    max_squared_sum : float
        Maximum squared sum of X over samples.

    alpha_scaled : float
        Constant that multiplies the regularization term, scaled by
        1. / n_samples, the number of samples.

    loss : {'log', 'squared', 'multinomial'}
        The loss function used in SAG solver.

    fit_intercept : bool
        Specifies if a constant (a.k.a. bias or intercept) will be
        added to the decision function.

    n_samples : int, default=None
        Number of rows in X. Useful if is_saga=True.

    is_saga : bool, default=False
        Whether to return step size for the SAGA algorithm or the SAG
        algorithm.

    Returns
    -------
    step_size : float
        Step size used in SAG solver.

    References
    ----------
    Schmidt, M., Roux, N. L., & Bach, F. (2013).
    Minimizing finite sums with the stochastic average gradient
    https://hal.inria.fr/hal-00860051/document

    :arxiv:`Defazio, A., Bach F. & Lacoste-Julien S. (2014).
    "SAGA: A Fast Incremental Gradient Method With Support
    for Non-Strongly Convex Composite Objectives" <1407.0202>`
    )logmultinomialg      ?squaredzJUnknown loss function for SAG solver, got %s instead of 'log' or 'squared'r         ?)int
ValueErrormin)	max_squared_sumalpha_scaledlossfit_intercept	n_samplesis_sagaLmunsteps	            Y/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/sklearn/linear_model/_sag.pyget_auto_step_sizer      s    \ %%%Oc-&8&889LH			c-000<?X
 
 	
  	 !i-,.22a!eck"
 QwK    r   r             MbP?Tc                    |i }|d}|
r>t           j        t           j        g}t          | |dd          } t          ||dd          }| j        d         | j        d	         }}t          |          |z  }t          |          |z  }|d
k    r$t          |                                          d	z   nd	}t          || | j	                  }d|
                                v r	|d         }nt          j        ||f| j	        d          }|j        d         |d	z   k    }|r|dddf         }|ddddf         }nt          j        || j	                  }d|
                                v r	|d         }nt          j        || j	                  }d|
                                v r	|d         }nt          j        ||f| j	        d          }d|
                                v r	|d         }nt          j        ||f| j	        d          }d|
                                v r	|d         }n!t          j        |t           j        d          }d|
                                v r	|d         }nd}t          | |||	          \  }}|#t          | d                                          }t          ||||||          }||z  d	k    rt!          d          | j	        t           j        k    rt"          nt$          } ||||||||||||||||||||||          \  } }!|!|k    rt'          j        dt*                     |rt          j        ||f          }|||||| d}|d
k    r|j        }"n|dddf         }"|"|!|fS )a  SAG solver for Ridge and LogisticRegression.

    SAG stands for Stochastic Average Gradient: the gradient of the loss is
    estimated each sample at a time and the model is updated along the way with
    a constant learning rate.

    IMPORTANT NOTE: 'sag' solver converges faster on columns that are on the
    same scale. You can normalize the data by using
    sklearn.preprocessing.StandardScaler on your data before passing it to the
    fit method.

    This implementation works with data represented as dense numpy arrays or
    sparse scipy arrays of floating point values for the features. It will
    fit the data according to squared loss or log loss.

    The regularizer is a penalty added to the loss function that shrinks model
    parameters towards the zero vector using the squared euclidean norm L2.

    .. versionadded:: 0.17

    Parameters
    ----------
    X : {array-like, sparse matrix} of shape (n_samples, n_features)
        Training data.

    y : ndarray of shape (n_samples,)
        Target values. With loss='multinomial', y must be label encoded
        (see preprocessing.LabelEncoder). For loss='log' it must be in [0, 1].

    sample_weight : array-like of shape (n_samples,), default=None
        Weights applied to individual samples (1. for unweighted).

    loss : {'log', 'squared', 'multinomial'}, default='log'
        Loss function that will be optimized:
        -'log' is the binary logistic loss, as used in LogisticRegression.
        -'squared' is the squared loss, as used in Ridge.
        -'multinomial' is the multinomial logistic loss, as used in
         LogisticRegression.

        .. versionadded:: 0.18
           *loss='multinomial'*

    alpha : float, default=1.
        L2 regularization term in the objective function
        ``(0.5 * alpha * || W ||_F^2)``.

    beta : float, default=0.
        L1 regularization term in the objective function
        ``(beta * || W ||_1)``. Only applied if ``is_saga`` is set to True.

    max_iter : int, default=1000
        The max number of passes over the training data if the stopping
        criteria is not reached.

    tol : float, default=0.001
        The stopping criteria for the weights. The iterations will stop when
        max(change in weights) / max(weights) < tol.

    verbose : int, default=0
        The verbosity level.

    random_state : int, RandomState instance or None, default=None
        Used when shuffling the data. Pass an int for reproducible output
        across multiple function calls.
        See :term:`Glossary <random_state>`.

    check_input : bool, default=True
        If False, the input arrays X and y will not be checked.

    max_squared_sum : float, default=None
        Maximum squared sum of X over samples. If None, it will be computed,
        going through all the samples. The value should be precomputed
        to speed up cross validation.

    warm_start_mem : dict, default=None
        The initialization parameters used for warm starting. Warm starting is
        currently used in LogisticRegression but not in Ridge.
        It contains:
            - 'coef': the weight vector, with the intercept in last line
                if the intercept is fitted.
            - 'gradient_memory': the scalar gradient for all seen samples.
            - 'sum_gradient': the sum of gradient over all seen samples,
                for each feature.
            - 'intercept_sum_gradient': the sum of gradient over all seen
                samples, for the intercept.
            - 'seen': array of boolean describing the seen samples.
            - 'num_seen': the number of seen samples.

    is_saga : bool, default=False
        Whether to use the SAGA algorithm or the SAG algorithm. SAGA behaves
        better in the first epochs, and allow for l1 regularisation.

    Returns
    -------
    coef_ : ndarray of shape (n_features,)
        Weight vector.

    n_iter_ : int
        The number of full pass on all samples.

    warm_start_mem : dict
        Contains a 'coef' key with the fitted result, and possibly the
        fitted intercept at the end of the array. Contains also other keys
        used for warm starting.

    Examples
    --------
    >>> import numpy as np
    >>> from sklearn import linear_model
    >>> n_samples, n_features = 10, 5
    >>> rng = np.random.RandomState(0)
    >>> X = rng.randn(n_samples, n_features)
    >>> y = rng.randn(n_samples)
    >>> clf = linear_model.Ridge(solver='sag')
    >>> clf.fit(X, y)
    Ridge(solver='sag')

    >>> X = np.array([[-1, -1], [-2, -1], [1, 1], [2, 1]])
    >>> y = np.array([1, 1, 2, 2])
    >>> clf = linear_model.LogisticRegression(solver='sag')
    >>> clf.fit(X, y)
    LogisticRegression(solver='sag')

    References
    ----------
    Schmidt, M., Roux, N. L., & Bach, F. (2013).
    Minimizing finite sums with the stochastic average gradient
    https://hal.inria.fr/hal-00860051/document

    :arxiv:`Defazio, A., Bach F. & Lacoste-Julien S. (2014).
    "SAGA: A Fast Incremental Gradient Method With Support
    for Non-Strongly Convex Composite Objectives" <1407.0202>`

    See Also
    --------
    Ridge, SGDRegressor, ElasticNet, Lasso, SVR,
    LogisticRegression, SGDClassifier, LinearSVC, Perceptron
    Nr!   csrC)dtypeaccept_sparseorderF)r&   	ensure_2dr(   r   r   r   )r&   coef)r&   r(   intercept_sum_gradientgradient_memorysum_gradientseennum_seenT)r   )r   r   zQCurrent sag implementation does not handle the case step_size * alpha_scaled == 1z?The max_iter was reached which means the coef_ did not converge)r*   r.   r,   r-   r/   r0   )npfloat64float32r   shapefloatr   maxr   r&   keyszerosint32r	   r   r   ZeroDivisionErrorr   r
   warningswarnr   vstackT)#Xysample_weightr   alphabetamax_itertolverboserandom_statecheck_inputr   warm_start_memr   _dtyper   
n_featuresr   beta_scaled	n_classes	coef_initr   intercept_initr,   gradient_memory_initsum_gradient_init	seen_initnum_seen_initdatasetintercept_decay	step_sizesagr0   n_iter_coef_s#                                      r   
sag_solverrZ   W   s8   t  E*bj)uCHHH5DDDGAJ
zI<<)+L++	)K %)M$9$9AEEGGq  qI )IIIM$$&&&&"6*		 Hj)4AG3OOO	 OA&:>:M <"2qqq5)crc111f%		)17;;;>#6#6#8#888!/0H!I!#)17!C!C!CN//1111-.?@!x	"!' 
  
  
 ,,....*>:Hj)%<AGSVWWW$$&&&&"6*		HYbhcBBB	^((****&z2+Aq-NNG_#At44488::"  I <1$$5
 
 	

 7bj((%%eC+ Hg0 (M	
 	
 	

  ;Iy.9::	 )"8/ N }!!!Q$'>))r   )NF)Nr   r   r    r!   r"   r   NTNNF)__doc__r;   numpyr1   
exceptionsr   utilsr   utils.extmathr   utils.validationr   _baser	   	_sag_fastr
   r   r   rZ    r   r   <module>rd      s    B B
      + + + + + +       % % % % % % 3 3 3 3 3 3       # # # # # # # # QVB B B BP 	
	[* [* [* [* [* [*r   