
    0Ph                       d Z ddlZddlZddlmZmZ ddlmZ ddlmZm	Z	 ddl
ZddlmZmZmZ ddlmZ ddlmZ d	d
lmZmZmZmZ d	dlmZ d	dlmZmZ d	dlmZ d	dl m!Z! d	dl"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z) d	dl*m+Z+m,Z,m-Z-m.Z.m/Z/ d	dl0m1Z1m2Z2m3Z3m4Z4 d	dl5m6Z6m7Z7 d	dl8m9Z9 d	dl:m;Z;m<Z<m=Z=m>Z>m?Z? d	dl@mAZA d	dlBmCZCmDZDmEZE ddlFmGZGmHZHmIZImJZJ ddlKmLZL d ZM	 	 	 	 	 	 dRdZNddddddddZOd  ZPdSd"ZQdTd#ZR	 	 	 	 	 	 dUd$ZSd% ZT e4d&d'ejU        gd&g e2e	ddd()          d&g e2e	ddd*)          d&dg e3h d+          g e2eddd()          dg e2e	ddd()          gd,gd-gd.gd-gd-gd-gd/d0          dd1dddd!dd!d!dd2
d3            ZV	 	 	 	 	 	 	 	 	 	 	 	 	 	 dVd4ZWd5 ZXd6 ZY G d7 d8eHe9          ZZ G d: d;eeeZ          Z[ G d< d=eG          Z\ G d> d?e\eZ          Z]d@ Z^dA Z_ G dB dCej        jU                  Z` G dD dEej        jU                  Za G dF dGee          Zb G dH dIeGe          Zc G dJ dKeH          Zd G dL dMeH          Ze G dN dOeeee          Zf G dP dQe\ee          ZgdS )Wz
Ridge regression
    N)ABCMetaabstractmethod)partial)IntegralReal)linalgoptimizesparse)r   )BaseEstimator   )MultiOutputMixinRegressorMixin_fit_contextis_classifier)ConvergenceWarning)check_scoringget_scorer_names)GridSearchCV)LabelBinarizer)Bunchcheck_arraycheck_consistent_lengthcheck_scalarcolumn_or_1dcompute_sample_weight
deprecated)_is_numpy_namespace_raveldeviceget_namespaceget_namespace_and_device)HiddenInterval
StrOptionsvalidate_params)	row_normssafe_sparse_dot)_sparse_linalg_cg)MetadataRouterMethodMapping_raise_for_params_routing_enabledprocess_routing)mean_variance_axis)_check_sample_weightcheck_is_fittedvalidate_data   )LinearClassifierMixinLinearModel_preprocess_data_rescale_data)
sag_solverc                 v      fd} fd}t           j                             j        ||          }|S )zCreate LinearOperator for matrix products with implicit centering.

    Matrix product `LinearOperator @ coef` returns `(X - X_offset) @ coef`.
    c                 `                         |           |                                z  z
  S N)dotbXX_offsetsample_weight_sqrts    [/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/sklearn/linear_model/_ridge.pymatvecz&_get_rescaled_operator.<locals>.matvec@   s(    uuQxx,quuX>>>    c                 j    j                             |           |                               z  z
  S r:   )Tr;   r<   s    rA   rmatvecz'_get_rescaled_operator.<locals>.rmatvecC   s,    swwqzzHquu-?'@'@@@@rC   )shaperB   rF   )r
   r   LinearOperatorrG   )r>   r?   r@   rB   rF   X1s   ```   rA   _get_rescaled_operatorrJ   :   sw    ? ? ? ? ? ? ?A A A A A A A 
	%	%AGFG	%	T	TBIrC   -C6?c	                 j   |&t          j        | j        d         | j                  }| j        \  }	}
||t	          j        |           n||z  }t          | ||          t          j        |j        d         |
f| j                  }|
|	k    rfd}nfd}t          |j        d                   D ]}|d d |f         } |||                   }|
|	k    rLt	          j	        |	|	f|| j                  }t          |||          \  }}                    |          ||<   nL                    |          }t	          j	        |
|
f|| j                  }t          ||||          \  ||<   }|dk     rt          d	|z            |%|dk    r|rt          j        d
|z  t                     |S )Nr   dtyper2   c                       fd}|S )Nc                 `                                             |                     | z  z   S r:   )rB   rF   xrI   
curr_alphas    rA   _mvz0_solve_sparse_cg.<locals>.create_mv.<locals>._mve   s(    yyA//*q.@@rC    rS   rT   rI   s   ` rA   	create_mvz#_solve_sparse_cg.<locals>.create_mvd   /    A A A A A A JrC   c                       fd}|S )Nc                 `                                             |                     | z  z   S r:   )rF   rB   rQ   s    rA   rT   z0_solve_sparse_cg.<locals>.create_mv.<locals>._mvm   s(    zz"))A,,//*q.@@rC   rU   rV   s   ` rA   rW   z#_solve_sparse_cg.<locals>.create_mvl   rX   rC   )rB   rN   )rtol)maxiterr[   zFailed with error code %dz/sparse_cg did not converge after %d iterations.)nponesrG   rN   	sp_linalgaslinearoperatorrJ   emptyrangerH   r(   rF   
ValueErrorwarningswarnr   )r>   yalphamax_itertolverboser?   X_scaler@   	n_samples
n_featuresX_offset_scalecoefsrW   iy_columnmvCcoefinforI   s                       @rA   _solve_sparse_cgrv   J   s$    !WQWQZqw???GIz7?'**!G+#A~7IJJHagaj*-QW===EI	 	 	 	 	 		 	 	 	 	 171:  QQQT7YuQx  	!! (I&r  A +1hSAAAJD$zz$''E!HH zz(++H(Z(17  A /q(HSVWWWNE!Hd!8884?@@@qWMADH"  
 LrC   T)fit_interceptrh   ri   r?   rk   r@   c          	      X   |&t          j        | j        d         | j                  }t	          j        |           r|r||z  }	t          | |	|          }
n| }
| j        \  }}t          j        |j        d         |f| j                  }t          j        |j        d         t           j                  }t          j	        |          }t          |j        d                   D ]D}|dd|f         }t          j        |
|||         |||          }|d         ||<   |d         ||<   E||fS )a  Solve Ridge regression via LSQR.

    We expect that y is always mean centered.
    If X is dense, we expect it to be mean centered such that we can solve
        ||y - Xw||_2^2 + alpha * ||w||_2^2

    If X is sparse, we expect X_offset to be given such that we can solve
        ||y - (X - X_offset)w||_2^2 + alpha * ||w||_2^2

    With sample weights S=diag(sample_weight), this becomes
        ||sqrt(S) (y - (X - X_offset) w)||_2^2 + alpha * ||w||_2^2
    and we expect y and X to already be rescaled, i.e. sqrt(S) @ y, sqrt(S) @ X. In
    this case, X_offset is the sample_weight weighted mean of X before scaling by
    sqrt(S). The objective then reads
       ||y - (X - sqrt(S) X_offset) w)||_2^2 + alpha * ||w||_2^2
    Nr   rM   r2   )dampatolbtoliter_limr   )r]   r^   rG   rN   r
   issparserJ   ra   int32sqrtrb   r_   lsqr)r>   rf   rg   rw   rh   ri   r?   rk   r@   rn   rI   rl   rm   ro   n_iter
sqrt_alpharp   rq   ru   s                      rA   _solve_lsqrr      s:   8 !WQWQZqw???q m !G+#A~7IJJ GIzHagaj*-QW===EXagaj111F J171:  QQQT7~z!}3S8
 
 
 7aGq		&=rC   c                    | j         d         }|j         d         }t          | j        | d          }t          | j        |d          }t          j        |t          |          |d         gz            }|r>|j        d d |dz   xx         |d         z  cc<   t          j        ||dd          j        S t          j	        ||g| j
                  }t          ||j        |          D ]k\  }	}
}|j        d d |dz   xx         |z  cc<   t          j        ||
dd                                          |	d d <   |j        d d |dz   xx         |z  cc<   l|S )	Nr2   Tdense_outputr   posassume_aoverwrite_arM   F)rG   r'   rE   r]   array_equallenflatr   solvera   rN   zipravel)r>   rf   rg   rm   	n_targetsAXy	one_alpharo   rt   targetcurrent_alphas               rA   _solve_choleskyr      s   J
IQT222A	ad	3	3	3Buc%jjE!H:&=>>I 		  *q. !!!U1X-!!!|ArEtDDDFF)Z0@@@+.ubdE+B+B 	7 	7'D&-F$$j1n$%%%6%%%l1fu%PPPVVXXDGF$$j1n$%%%6%%%%rC   Fc                    | j         d         }|j         d         }|r|                                 } t          j        |          }||d         k                                    }t          |t          j                  p|dv}|rWt          j        t          j        |                    }	||	d d t          j        f         z  }| t          j	        |	|	          z  } |r| j
        d d |dz   xx         |d         z  cc<   	 t          j        | |dd          }
nI# t          j        j        $ r2 t          j        d           t          j        | |          d         }
Y nw xY w| j
        d d |dz   xx         |d         z  cc<   |r|
|	d d t          j        f         z  }
|
S t          j        ||g| j                  }t'          ||j        |          D ]k\  }
}}| j
        d d |dz   xx         |z  cc<   t          j        | |dd                                          |
d d <   | j
        d d |dz   xx         |z  cc<   l|r||	t          j        d d f         z  }|j        S )Nr   r2   )      ?Nr   Fr   zNSingular matrix in solving dual problem. Using least-squares solution instead.)rG   copyr]   
atleast_1dall
isinstancendarrayr   newaxisouterr   r   r   LinAlgErrorrd   re   lstsqra   rN   r   rE   r   )Krf   rg   sample_weightr   rl   r   r   has_swsw	dual_coef
dual_coefsr   r   s                 rA   _solve_cholesky_kernelr      s   
I
I FFHHM%  E%("''))Irz22Vm;6VF  WR]=1122111bj=!!	RXb" (	)a-   E!H,   
	. QEuMMMIIy$ 	. 	. 	.M2   Q**1-III	. 	
)a-   E!H,    	+AAArzM**I Xy)4ag>>
03JU0K0K 	6 	6,Iv}F##i!m#$$$5$$$!<6Eu  egg aaaL F##i!m#$$$5$$$$ 	,"RZ]++J|s   >D AEEc                 v   t          | |          \  }}|j                            | d          \  }}}|dk    }||         d d d f         }	|j        |z  }
|                    |j        d         |j        d         f| j        t          |                     }|	|	dz  |z   z  ||<   ||
z  }|j        |z  j        S )NxpFfull_matricesgV瞯<r   )rN   r   r   )r    r   svdrE   zerosrG   rN   r   )r>   rf   rg   r   _UsVtidxs_nnzUTydd_UT_ys                rA   
_solve_svdr     s    !###EBy}}Qe}44HAq"
e)CcF111d7OE
#'C
!'!*ek!n-QWVAYYOOAeQh&'AcFWFD6MrC   c	                 H     j         \  }	}
i }|||d<   d|d|d}|rdt          j        fg|
z  |d<   ||||z  nd&t          j         j         d          j                  t          j        |j         d	         |
f j                  }t          |j         d	                   D ]st          j        |
f          }|ddf          fd
}t          j	        ||fi |}|d         s t          j        d| d| t                     |d         |<   t|S )a4  Solve ridge regression with LBFGS.

    The main purpose is fitting with forcing coefficients to be positive.
    For unconstrained ridge regression, there are faster dedicated solver methods.
    Note that with positive bounds on the coefficients, LBFGS seems faster
    than scipy.optimize.lsq_linear.
    Nr\   zL-BFGS-BT)methodri   jacoptionsr   boundsrM   r2   c                 P                        |           	z
  }||                                z  z  }d|                     |          z  d         z  |                      |           z  z   }j        |z           | z  z   }||                               z  z  }||fS )Ng      ?)r;   rE   )
wresidualfgradr>   rn   rg   rp   r@   rq   s
       rA   funcz_solve_lbfgs.<locals>.funcY  s    uuQxx(*H).~1F1FFFhll8,,,sU1X~a/HHA3>E!HqL0D)6H)I)IIId7NrC   successzWThe lbfgs solver did not converge. Try increasing max_iter or tol. Currently: max_iter=z	 and tol=rR   )rG   r]   infr^   rN   ra   rb   r   r	   minimizerd   re   r   )r>   rf   rg   positiverh   ri   r?   rk   r@   rl   rm   r   configro   x0r   resultrn   rp   rq   s   ` `     `        @@@rA   _solve_lbfgsr   +  s   $ GIzG%		 F  6K=:5x 3!G+!WQWQZqw???Hagaj*-QW===E171:  Xzm$$QQQT7		 		 		 		 		 		 		 		 		 		 "466v66i  	ML3;L LFIL L #   #;aLrC   c                     | r|dv rdS g dS )N)autosagsagacsrr   csccoorU   )is_X_sparsesolvers     rA   _get_valid_accept_sparser   r  s&     %v!888u$$$$rC   
array-likezsparse matrixleftclosedneither>   r   r   r   r   r   lbfgscholesky	sparse_cgrj   booleanrandom_stater>   rf   rg   r   r   rh   ri   rj   r   r   return_n_iterreturn_interceptcheck_inputprefer_skip_nested_validationr   )
r   r   rh   ri   rj   r   r   r   r   r   c       
         >    t          | |||||||||	|
|dd|          S )a  Solve the ridge equation by the method of normal equations.

    Read more in the :ref:`User Guide <ridge_regression>`.

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

    y : array-like of shape (n_samples,) or (n_samples, n_targets)
        Target values.

    alpha : float or array-like of shape (n_targets,)
        Constant that multiplies the L2 term, controlling regularization
        strength. `alpha` must be a non-negative float i.e. in `[0, inf)`.

        When `alpha = 0`, the objective is equivalent to ordinary least
        squares, solved by the :class:`LinearRegression` object. For numerical
        reasons, using `alpha = 0` with the `Ridge` object is not advised.
        Instead, you should use the :class:`LinearRegression` object.

        If an array is passed, penalties are assumed to be specific to the
        targets. Hence they must correspond in number.

    sample_weight : float or array-like of shape (n_samples,), default=None
        Individual weights for each sample. If given a float, every sample
        will have the same weight. If sample_weight is not None and
        solver='auto', the solver will be set to 'cholesky'.

        .. versionadded:: 0.17

    solver : {'auto', 'svd', 'cholesky', 'lsqr', 'sparse_cg',             'sag', 'saga', 'lbfgs'}, default='auto'
        Solver to use in the computational routines:

        - 'auto' chooses the solver automatically based on the type of data.

        - 'svd' uses a Singular Value Decomposition of X to compute the Ridge
          coefficients. It is the most stable solver, in particular more stable
          for singular matrices than 'cholesky' at the cost of being slower.

        - 'cholesky' uses the standard scipy.linalg.solve function to
          obtain a closed-form solution via a Cholesky decomposition of
          dot(X.T, X)

        - 'sparse_cg' uses the conjugate gradient solver as found in
          scipy.sparse.linalg.cg. As an iterative algorithm, this solver is
          more appropriate than 'cholesky' for large-scale data
          (possibility to set `tol` and `max_iter`).

        - 'lsqr' uses the dedicated regularized least-squares routine
          scipy.sparse.linalg.lsqr. It is the fastest and uses an iterative
          procedure.

        - 'sag' uses a Stochastic Average Gradient descent, and 'saga' uses
          its improved, unbiased version named SAGA. Both methods also use an
          iterative procedure, and are often faster than other solvers when
          both n_samples and n_features are large. Note that 'sag' and
          'saga' fast convergence is only guaranteed on features with
          approximately the same scale. You can preprocess the data with a
          scaler from sklearn.preprocessing.

        - 'lbfgs' uses L-BFGS-B algorithm implemented in
          `scipy.optimize.minimize`. It can be used only when `positive`
          is True.

        All solvers except 'svd' support both dense and sparse data. However, only
        'lsqr', 'sag', 'sparse_cg', and 'lbfgs' support sparse input when
        `fit_intercept` is True.

        .. versionadded:: 0.17
           Stochastic Average Gradient descent solver.
        .. versionadded:: 0.19
           SAGA solver.

    max_iter : int, default=None
        Maximum number of iterations for conjugate gradient solver.
        For the 'sparse_cg' and 'lsqr' solvers, the default value is determined
        by scipy.sparse.linalg. For 'sag' and saga solver, the default value is
        1000. For 'lbfgs' solver, the default value is 15000.

    tol : float, default=1e-4
        Precision of the solution. Note that `tol` has no effect for solvers 'svd' and
        'cholesky'.

        .. versionchanged:: 1.2
           Default value changed from 1e-3 to 1e-4 for consistency with other linear
           models.

    verbose : int, default=0
        Verbosity level. Setting verbose > 0 will display additional
        information depending on the solver used.

    positive : bool, default=False
        When set to ``True``, forces the coefficients to be positive.
        Only 'lbfgs' solver is supported in this case.

    random_state : int, RandomState instance, default=None
        Used when ``solver`` == 'sag' or 'saga' to shuffle the data.
        See :term:`Glossary <random_state>` for details.

    return_n_iter : bool, default=False
        If True, the method also returns `n_iter`, the actual number of
        iteration performed by the solver.

        .. versionadded:: 0.17

    return_intercept : bool, default=False
        If True and if X is sparse, the method also returns the intercept,
        and the solver is automatically changed to 'sag'. This is only a
        temporary fix for fitting the intercept with sparse data. For dense
        data, use sklearn.linear_model._preprocess_data before your regression.

        .. versionadded:: 0.17

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

        .. versionadded:: 0.21

    Returns
    -------
    coef : ndarray of shape (n_features,) or (n_targets, n_features)
        Weight vector(s).

    n_iter : int, optional
        The actual number of iteration performed by the solver.
        Only returned if `return_n_iter` is True.

    intercept : float or ndarray of shape (n_targets,)
        The intercept of the model. Only returned if `return_intercept`
        is True and if X is a scipy sparse array.

    Notes
    -----
    This function won't compute the intercept.

    Regularization improves the conditioning of the problem and
    reduces the variance of the estimates. Larger values specify stronger
    regularization. Alpha corresponds to ``1 / (2C)`` in other linear
    models such as :class:`~sklearn.linear_model.LogisticRegression` or
    :class:`~sklearn.svm.LinearSVC`. If an array is passed, penalties are
    assumed to be specific to the targets. Hence they must correspond in
    number.

    Examples
    --------
    >>> import numpy as np
    >>> from sklearn.datasets import make_regression
    >>> from sklearn.linear_model import ridge_regression
    >>> rng = np.random.RandomState(0)
    >>> X = rng.randn(100, 4)
    >>> y = 2.0 * X[:, 0] - 1.0 * X[:, 1] + 0.1 * rng.standard_normal(100)
    >>> coef, intercept = ridge_regression(X, y, alpha=1.0, return_intercept=True)
    >>> list(coef)
    [np.float64(1.9...), np.float64(-1.0...), np.float64(-0.0...), np.float64(-0.0...)]
    >>> intercept
    np.float64(-0.0...)
    N)r   r   rh   ri   rj   r   r   r   r   rk   r?   r   )_ridge_regressionr   s                rA   ridge_regressionr   y  sI    T 		#!#)   rC   c                 t   t          | ||||          \  }}}t          |          }t          j        |           }|d u}t	          |||||          }|r|st          j        |           } |s!|dk    rt          d|j         d| d          |r|dk    rt          d| d          |dk    r|st          d          |r|d	k    rt          d
          |rI|j	        |j
        g}t          ||          }t          | ||d          } t          || j        dd           }t          | |           | j        \  }}|j        dk    r$t          dt#          |j                  z            |j        dk    r|                    |d          }|j        \  }}||k    rt          d||fz            |r0t'          || | j                  }|dvrt)          | ||          \  } }}|Ot+          |t-          |                    dg                              st/          |dt0          j        dd          }t5          |                    ||| j                  |          }|j        d         d|fvrt          d|j        d         |fz            |j        d         dk    r+|dk    r%|                    |f|d         |j        |          }d }|dk    rt9          | ||||||||r|nd  	  	        }nR|d!k    r!t;          | ||||||||r|nd "	  	        \  }}n+|d#k    r||k    r_t=          | | j        d$%          } 	 tA          | ||          }!t=          | j        |!d$%          j        }n# tB          j"        $ r d}Y nw xY w	 tG          | ||          }n# tB          j"        $ r d}Y nw xY w|dv rmtI          | d$&          %                                }"t          j&        |j        d         |f| j                  }t          j&        |j        d         t
          j'                  }t          j(        |j        d         f| j                  }#tS          tU          ||j                            D ]\  }$\  }%}&d't          j(        |tW          |          z   df| j                  i}'tY          | |&-                                |d(|%d||||	d|"|'|d)k    *          \  }(})}*|r|(d d+         ||$<   |(d+         |#|$<   n|(||$<   |)||$<   |#j        d         dk    r|#d         }#n"|dk    rt]          | ||||||||r|nd ,	  	        }|dk    r#|rt_          d-          ta          | |||          }|dk    rt5          |          }|                    |          }|
r|r|||#f}+n|r||#f}+n	|
r||f}+n|}+|rg |+|R n|+S ).Nr   z Array API dispatch to namespace z" only supports solver 'svd'. Got 'z'.r   zJWhen positive=True, only 'lbfgs' solver can be used. Please change solver z" to 'lbfgs' or set positive=False.N'lbfgs' solver can be used only when positive=True. Please use another solver.r   zxIn Ridge, only 'sag' solver can directly fit the intercept. Please change solver to 'sag' or set return_intercept=False.rs   )accept_sparserN   orderF)rN   	ensure_2dr   r   zTarget y has the wrong shape %sr2   )r2   z:Number of samples in X and y does not correspond: %d != %drM   )r   r           rg   r   target_typemin_valinclude_boundaries)r   rN   r   r   zENumber of targets and number of penalties do not correspond: %d != %d)rG   
fill_valuerN   r   r   )rh   ri   rj   r?   rk   r@   r   )rg   rw   rh   ri   r?   rk   r@   r   Tr   )squaredrt   r   r   )is_sagar   )r   ri   rh   r?   rk   r@   z3SVD solver does not support sparse inputs currently)1r!   r   r
   r}   resolve_solverr]   asarrayrc   __name__float64float32r   r   rN   r   rG   ndimstrreshaper/   r6   r   typer   numbersr   r   fullrv   r   r'   rE   r   r   r   r   r&   maxra   r~   r   	enumerater   intr7   r   r   	TypeErrorr   ),r>   rf   rg   r   r   rh   ri   rj   r   r   r   r   return_solverrk   r?   r   rw   r   is_array_api_compliantdevice_is_numpy_namespaceX_is_sparser   _dtype_accept_sparserl   rm   
n_samples_r   r@   r   rt   r   r   max_squared_sum	interceptrp   alpha_ir   initcoef_n_iter_r   ress,                                               rA   r   r   V  s}   & +C	1mWh+ +'B -R00/!$$K$&FFH.>RPPF + JqMM 
&E//-r{ - -"(- - -
 
 	

  
Fg%%%$*% % %
 
 	
 )
 
 	

  
FeOO&
 
 	
  G*bj)1+vFFvSQQQEFFFAq!!!GIzvzz:S\\IJJJv{{JJq'""GJ	JH*%&
 
 	

  J,]AQWMMM(( (5Q='I'I$Aq$ E4

C58I8I3J3J!K!K%
 
 
 2::eG17:CCKKKE{1~a^++S{1~y)*
 
 	

 {1~y1}},585;w  
 
 F5;E11

 

 

 
6		"'5;E11

 

 

ff 
:			!!13T:::A21a??	&qsIDIIIK%   &q!U33%    
?	"	"#At44488::xZ0@@@!'!*BH555Hagaj]!':::	$-c%oo$>$> 	  	  A *s3C/D/D"Da!HPQPWXXXD !+&(! ! !E7A     *Q$Ry	!QF1II?1""!!I	7		5;E11

 

 

  	SQRRR!Qr**A~~d||::dD ) FI%	 Io	 Fl*3>S>&>>>3s$   ;-M* *M?>M?N N+*N+c                     | dk    r| S t          |          }t          |||          }|r|S |rt          d|j         d          d} | |k    r&t	          j        d|j         d|  d| d|  d		           | S )
Nr   zYThe solvers that support positive fitting do not support Array API dispatch to namespace zc. Please either disable Array API dispatch, or use a numpy-like namespace, or set `positive=False`.r   z&Using Array API dispatch to namespace z7 with `solver='auto'` will result in using the solver 'zr'. The results may differ from those when using a Numpy array, because in that case the preferred solver would be z. Set `solver='z'` to suppress this warning.)r   resolve_solver_for_numpyrc   r   rd   re   )r   r   r   	is_sparser   r  auto_solver_nps          rA   r   r   ;  s    ,R00-h8H)TTN  
2/1{2 2 2
 
 	
 FAR[ A A@FA A CQA A #	A A A	
 	
 	
 MrC   c                     | rdS |rdS |sdS dS )Nr   r   r   r   rU   )r   r   r  s      rA   r  r  [  s.     w u z;rC   c            
           e Zd ZU  eeddd          ej        gdgdg eeddd          dg eeddd          g eh d          gdgdgd	Z	e
ed
<   e	 dddddddddd            ZddZdS )
_BaseRidger   Nr   r   r   r2   >   r   r   r   r   r   r   r   r   r   rg   rw   copy_Xrh   ri   r   r   r   _parameter_constraintsr   TrK   r   Frw   r   rh   ri   r   r   r   c                v    || _         || _        || _        || _        || _        || _        || _        || _        d S r:   r  )	selfrg   rw   r   rh   ri   r   r   r   s	            rA   __init__z_BaseRidge.__init__y  sD     
*  (rC   c                    t          |||          \  }}| j        dk    r| j        st          d          | j        r)| j        dvrt          d| j         d          | j        }nt	          j        |          r| j        rx| j        dvr't          d                    | j                            | j        dv r| j        }n>| j        d	k    r)| j        "| j	        d
k    rt          j        d           d	}n
d}n| j        }|t          |||j                  }t          ||| j        | j        |          \  }}}}}	|d	k    rzt	          j        |          rf| j        r_t!          ||| j        || j        | j	        d	| j        | j        dddd          \  | _        | _        | _        | _        | xj        |z  c_        nt	          j        |          r| j        r||	d}
ni }
t!          ||f| j        || j        | j	        || j        | j        dddd| j        d|
\  | _        | _        | _        |                     |||	           | S )Nr   r   )r   r   zsolver='zh' does not support positive fitting. Please set the solver to 'auto' or 'lbfgs', or set `positive=False`)r   r   r   r   r   zsolver='{}' does not support fitting the intercept on sparse data. Please set the solver to 'auto' or 'lsqr', 'sparse_cg', 'sag', 'lbfgs' or set `fit_intercept=False`)r   r   r   rK   z"sag" solver requires many iterations to fit an intercept with sparse inputs. Either set the solver to "auto" or "sparse_cg", or set a low "tol" and a high "max_iter" (especially if inputs are not standardized).r   rM   rw   r   r   TF)rg   r   rh   ri   r   r   r   r   r   r	  r   )r?   rk   )rg   r   rh   ri   r   r   r   r   r   r	  r   rw   )r    r   r   rc   r
   r}   rw   formatrh   ri   rd   re   r/   rN   r5   r   r   rg   r   r  r  
intercept_solver__set_intercept)r$  r>   rf   r   r   r
  r   r?   y_offsetrk   paramss              rA   fitz_BaseRidge.fit  s   %21a%G%G"";'!!$-!-  
 = 	!{"333 Tt{ T T T  
 _Q 	!D$6 	!{"OOO 3 4:6$+3F3F	   {///%%$-*?DHtOO)   $[F$0QQQM -=,'-
 -
 -
)1h' U??vq11?d6H?FWj+H!."!%"!G G GCDJdot|  OOx'OOO q!! d&8 &.7CC 5F6 j+H!."!&"!"06 6 6 62DJdl" (G<<<rC   r   r:   )r   
__module____qualname__r#   r   r]   r   r   r$   r!  dict__annotations__r   r%  r.  rU   rC   rA   r  r  i  s        (4D888"*E#+Xh4???Fq$v6667JXXX 

 K'($ $D     ) ) ) ) ) ^)*c c c c c crC   r  )	metaclassc            	       r     e Zd ZdZ	 ddddddddd fd	Z ed
          d fd	            Z fdZ xZS )RidgeaR  Linear least squares with l2 regularization.

    Minimizes the objective function::

    ||y - Xw||^2_2 + alpha * ||w||^2_2

    This model solves a regression model where the loss function is
    the linear least squares function and regularization is given by
    the l2-norm. Also known as Ridge Regression or Tikhonov regularization.
    This estimator has built-in support for multi-variate regression
    (i.e., when y is a 2d-array of shape (n_samples, n_targets)).

    Read more in the :ref:`User Guide <ridge_regression>`.

    Parameters
    ----------
    alpha : {float, ndarray of shape (n_targets,)}, default=1.0
        Constant that multiplies the L2 term, controlling regularization
        strength. `alpha` must be a non-negative float i.e. in `[0, inf)`.

        When `alpha = 0`, the objective is equivalent to ordinary least
        squares, solved by the :class:`LinearRegression` object. For numerical
        reasons, using `alpha = 0` with the `Ridge` object is not advised.
        Instead, you should use the :class:`LinearRegression` object.

        If an array is passed, penalties are assumed to be specific to the
        targets. Hence they must correspond in number.

    fit_intercept : bool, default=True
        Whether to fit the intercept for this model. If set
        to false, no intercept will be used in calculations
        (i.e. ``X`` and ``y`` are expected to be centered).

    copy_X : bool, default=True
        If True, X will be copied; else, it may be overwritten.

    max_iter : int, default=None
        Maximum number of iterations for conjugate gradient solver.
        For 'sparse_cg' and 'lsqr' solvers, the default value is determined
        by scipy.sparse.linalg. For 'sag' solver, the default value is 1000.
        For 'lbfgs' solver, the default value is 15000.

    tol : float, default=1e-4
        The precision of the solution (`coef_`) is determined by `tol` which
        specifies a different convergence criterion for each solver:

        - 'svd': `tol` has no impact.

        - 'cholesky': `tol` has no impact.

        - 'sparse_cg': norm of residuals smaller than `tol`.

        - 'lsqr': `tol` is set as atol and btol of scipy.sparse.linalg.lsqr,
          which control the norm of the residual vector in terms of the norms of
          matrix and coefficients.

        - 'sag' and 'saga': relative change of coef smaller than `tol`.

        - 'lbfgs': maximum of the absolute (projected) gradient=max|residuals|
          smaller than `tol`.

        .. versionchanged:: 1.2
           Default value changed from 1e-3 to 1e-4 for consistency with other linear
           models.

    solver : {'auto', 'svd', 'cholesky', 'lsqr', 'sparse_cg',             'sag', 'saga', 'lbfgs'}, default='auto'
        Solver to use in the computational routines:

        - 'auto' chooses the solver automatically based on the type of data.

        - 'svd' uses a Singular Value Decomposition of X to compute the Ridge
          coefficients. It is the most stable solver, in particular more stable
          for singular matrices than 'cholesky' at the cost of being slower.

        - 'cholesky' uses the standard scipy.linalg.solve function to
          obtain a closed-form solution.

        - 'sparse_cg' uses the conjugate gradient solver as found in
          scipy.sparse.linalg.cg. As an iterative algorithm, this solver is
          more appropriate than 'cholesky' for large-scale data
          (possibility to set `tol` and `max_iter`).

        - 'lsqr' uses the dedicated regularized least-squares routine
          scipy.sparse.linalg.lsqr. It is the fastest and uses an iterative
          procedure.

        - 'sag' uses a Stochastic Average Gradient descent, and 'saga' uses
          its improved, unbiased version named SAGA. Both methods also use an
          iterative procedure, and are often faster than other solvers when
          both n_samples and n_features are large. Note that 'sag' and
          'saga' fast convergence is only guaranteed on features with
          approximately the same scale. You can preprocess the data with a
          scaler from sklearn.preprocessing.

        - 'lbfgs' uses L-BFGS-B algorithm implemented in
          `scipy.optimize.minimize`. It can be used only when `positive`
          is True.

        All solvers except 'svd' support both dense and sparse data. However, only
        'lsqr', 'sag', 'sparse_cg', and 'lbfgs' support sparse input when
        `fit_intercept` is True.

        .. versionadded:: 0.17
           Stochastic Average Gradient descent solver.
        .. versionadded:: 0.19
           SAGA solver.

    positive : bool, default=False
        When set to ``True``, forces the coefficients to be positive.
        Only 'lbfgs' solver is supported in this case.

    random_state : int, RandomState instance, default=None
        Used when ``solver`` == 'sag' or 'saga' to shuffle the data.
        See :term:`Glossary <random_state>` for details.

        .. versionadded:: 0.17
           `random_state` to support Stochastic Average Gradient.

    Attributes
    ----------
    coef_ : ndarray of shape (n_features,) or (n_targets, n_features)
        Weight vector(s).

    intercept_ : float or ndarray of shape (n_targets,)
        Independent term in decision function. Set to 0.0 if
        ``fit_intercept = False``.

    n_iter_ : None or ndarray of shape (n_targets,)
        Actual number of iterations for each target. Available only for
        sag and lsqr solvers. Other solvers will return None.

        .. versionadded:: 0.17

    n_features_in_ : int
        Number of features seen during :term:`fit`.

        .. versionadded:: 0.24

    feature_names_in_ : ndarray of shape (`n_features_in_`,)
        Names of features seen during :term:`fit`. Defined only when `X`
        has feature names that are all strings.

        .. versionadded:: 1.0

    solver_ : str
        The solver that was used at fit time by the computational
        routines.

        .. versionadded:: 1.5

    See Also
    --------
    RidgeClassifier : Ridge classifier.
    RidgeCV : Ridge regression with built-in cross validation.
    :class:`~sklearn.kernel_ridge.KernelRidge` : Kernel ridge regression
        combines ridge regression with the kernel trick.

    Notes
    -----
    Regularization improves the conditioning of the problem and
    reduces the variance of the estimates. Larger values specify stronger
    regularization. Alpha corresponds to ``1 / (2C)`` in other linear
    models such as :class:`~sklearn.linear_model.LogisticRegression` or
    :class:`~sklearn.svm.LinearSVC`.

    Examples
    --------
    >>> from sklearn.linear_model import Ridge
    >>> import numpy as np
    >>> n_samples, n_features = 10, 5
    >>> rng = np.random.RandomState(0)
    >>> y = rng.randn(n_samples)
    >>> X = rng.randn(n_samples, n_features)
    >>> clf = Ridge(alpha=1.0)
    >>> clf.fit(X, y)
    Ridge()
    r   TNrK   r   Fr"  c          
      Z    t                                          ||||||||           d S Nr  )superr%  )
r$  rg   rw   r   rh   ri   r   r   r   	__class__s
            rA   r%  zRidge.__init__  sH     	'% 	 		
 		
 		
 		
 		
rC   r   c           
         t          t          j        |          | j                  }t	          |||          \  }}t          | ||||j        |j        gddd          \  }}t                      	                    |||          S )a%  Fit Ridge regression model.

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

        y : ndarray of shape (n_samples,) or (n_samples, n_targets)
            Target values.

        sample_weight : float or ndarray of shape (n_samples,), default=None
            Individual weights for each sample. If given a float, every sample
            will have the same weight.

        Returns
        -------
        self : object
            Fitted estimator.
        T)r   rN   force_writeablemulti_output	y_numericr   )
r   r
   r}   r   r    r1   r   r   r9  r.  )r$  r>   rf   r   r  r   r   r:  s          rA   r.  z	Ridge.fit  s    * 2&/!2D2DdkRRaM22A(:rz* 	
 	
 	
1 ww{{1a}{===rC   c                     t                                                      }d|_        | j        dk    o| j        dk    p| j         |j        _        |S )NTr   r   )r9  __sklearn_tags__array_api_supportr   rw   
input_tagsr
   r$  tagsr:  s     rA   rA  zRidge.__sklearn_tags__  sU    ww''))!%"&+"6 "
K:%?T-?)? 	 rC   r/  r:   )	r   r0  r1  __doc__r%  r   r.  rA  __classcell__r:  s   @rA   r6  r6    s        q qj 
 
 
 
 
 
 
 
. \555 >  >  >  >  > 65 >D        rC   r6  c                   D     e Zd Zd Z fdZed             Z fdZ xZS )_RidgeClassifierMixinc           	         t          t          j        |          |          }t          | |||ddd          \  }}t	          dd          | _        | j                            |          }| j        j                            d          st          |d          }t          |||j        	          }| j        r|t          | j        |          z  }||||fS )
a  Validate `X` and `y` and binarize `y`.

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

        y : ndarray of shape (n_samples,)
            Target values.

        sample_weight : float or ndarray of shape (n_samples,), default=None
            Individual weights for each sample. If given a float, every sample
            will have the same weight.

        solver : str
            The solver used in `Ridge` to know which sparse format to support.

        Returns
        -------
        X : {ndarray, sparse matrix} of shape (n_samples, n_features)
            Validated training data.

        y : ndarray of shape (n_samples,)
            Validated target values.

        sample_weight : ndarray of shape (n_samples,)
            Validated sample weights.

        Y : ndarray of shape (n_samples, n_classes)
            The binarized version of `y`.
        TF)r   r=  r>  r<  r2   r   )	pos_label	neg_label
multilabel)re   rM   )r   r
   r}   r1   r   _label_binarizerfit_transformy_type_
startswithr   r/   rN   class_weightr   )r$  r>   rf   r   r   r   Ys          rA   _prepare_dataz#_RidgeClassifierMixin._prepare_data  s    @ 11C1CVLL' 
 
 
1 !/b I I I!//22$,77EE 	+QT***A,]AQWMMM 	X),A$BSUV,W,WWM!]A%%rC   c                    t          | dg           | j        j                            d          r9d|                     |          dk    z  dz
  }| j                            |          S t                                          |          S )aI  Predict class labels for samples in `X`.

        Parameters
        ----------
        X : {array-like, spare matrix} of shape (n_samples, n_features)
            The data matrix for which we want to predict the targets.

        Returns
        -------
        y_pred : ndarray of shape (n_samples,) or (n_samples, n_outputs)
            Vector or matrix containing the predictions. In binary and
            multiclass problems, this is a vector containing `n_samples`. In
            a multilabel problem, it returns a matrix of shape
            `(n_samples, n_outputs)`.
        rO  )
attributesrN  r   r   r2   )r0   rO  rQ  rR  decision_functioninverse_transformr9  predict)r$  r>   scoresr:  s      rA   rZ  z_RidgeClassifierMixin.predict"  s      	*<)=>>>> (33LAA 	C $0033a781<F(::6BBBwwq!!!rC   c                     | j         j        S )zClasses labels.)rO  classes_r$  s    rA   r]  z_RidgeClassifierMixin.classes_;  s     $--rC   c                 `    t                                                      }d|j        _        |S NT)r9  rA  classifier_tagsmulti_labelrD  s     rA   rA  z&_RidgeClassifierMixin.__sklearn_tags__@  s(    ww''))+/(rC   )	r   r0  r1  rU  rZ  propertyr]  rA  rG  rH  s   @rA   rJ  rJ    s|        3& 3& 3&j" " " " "2 . . X.        rC   rJ  c            
            e Zd ZU dZi ej        de edh          dgiZeed<   	 ddddddd	d
dd fdZ	 e
d          d fd	            Z fdZ xZS )RidgeClassifiera  Classifier using Ridge regression.

    This classifier first converts the target values into ``{-1, 1}`` and
    then treats the problem as a regression task (multi-output regression in
    the multiclass case).

    Read more in the :ref:`User Guide <ridge_regression>`.

    Parameters
    ----------
    alpha : float, default=1.0
        Regularization strength; must be a positive float. Regularization
        improves the conditioning of the problem and reduces the variance of
        the estimates. Larger values specify stronger regularization.
        Alpha corresponds to ``1 / (2C)`` in other linear models such as
        :class:`~sklearn.linear_model.LogisticRegression` or
        :class:`~sklearn.svm.LinearSVC`.

    fit_intercept : bool, default=True
        Whether to calculate the intercept for this model. If set to false, no
        intercept will be used in calculations (e.g. data is expected to be
        already centered).

    copy_X : bool, default=True
        If True, X will be copied; else, it may be overwritten.

    max_iter : int, default=None
        Maximum number of iterations for conjugate gradient solver.
        The default value is determined by scipy.sparse.linalg.

    tol : float, default=1e-4
        The precision of the solution (`coef_`) is determined by `tol` which
        specifies a different convergence criterion for each solver:

        - 'svd': `tol` has no impact.

        - 'cholesky': `tol` has no impact.

        - 'sparse_cg': norm of residuals smaller than `tol`.

        - 'lsqr': `tol` is set as atol and btol of scipy.sparse.linalg.lsqr,
          which control the norm of the residual vector in terms of the norms of
          matrix and coefficients.

        - 'sag' and 'saga': relative change of coef smaller than `tol`.

        - 'lbfgs': maximum of the absolute (projected) gradient=max|residuals|
          smaller than `tol`.

        .. versionchanged:: 1.2
           Default value changed from 1e-3 to 1e-4 for consistency with other linear
           models.

    class_weight : dict or 'balanced', default=None
        Weights associated with classes in the form ``{class_label: weight}``.
        If not given, all classes are supposed to have weight one.

        The "balanced" mode uses the values of y to automatically adjust
        weights inversely proportional to class frequencies in the input data
        as ``n_samples / (n_classes * np.bincount(y))``.

    solver : {'auto', 'svd', 'cholesky', 'lsqr', 'sparse_cg',             'sag', 'saga', 'lbfgs'}, default='auto'
        Solver to use in the computational routines:

        - 'auto' chooses the solver automatically based on the type of data.

        - 'svd' uses a Singular Value Decomposition of X to compute the Ridge
          coefficients. It is the most stable solver, in particular more stable
          for singular matrices than 'cholesky' at the cost of being slower.

        - 'cholesky' uses the standard scipy.linalg.solve function to
          obtain a closed-form solution.

        - 'sparse_cg' uses the conjugate gradient solver as found in
          scipy.sparse.linalg.cg. As an iterative algorithm, this solver is
          more appropriate than 'cholesky' for large-scale data
          (possibility to set `tol` and `max_iter`).

        - 'lsqr' uses the dedicated regularized least-squares routine
          scipy.sparse.linalg.lsqr. It is the fastest and uses an iterative
          procedure.

        - 'sag' uses a Stochastic Average Gradient descent, and 'saga' uses
          its unbiased and more flexible version named SAGA. Both methods
          use an iterative procedure, and are often faster than other solvers
          when both n_samples and n_features are large. Note that 'sag' and
          'saga' fast convergence is only guaranteed on features with
          approximately the same scale. You can preprocess the data with a
          scaler from sklearn.preprocessing.

          .. versionadded:: 0.17
             Stochastic Average Gradient descent solver.
          .. versionadded:: 0.19
             SAGA solver.

        - 'lbfgs' uses L-BFGS-B algorithm implemented in
          `scipy.optimize.minimize`. It can be used only when `positive`
          is True.

    positive : bool, default=False
        When set to ``True``, forces the coefficients to be positive.
        Only 'lbfgs' solver is supported in this case.

    random_state : int, RandomState instance, default=None
        Used when ``solver`` == 'sag' or 'saga' to shuffle the data.
        See :term:`Glossary <random_state>` for details.

    Attributes
    ----------
    coef_ : ndarray of shape (1, n_features) or (n_classes, n_features)
        Coefficient of the features in the decision function.

        ``coef_`` is of shape (1, n_features) when the given problem is binary.

    intercept_ : float or ndarray of shape (n_targets,)
        Independent term in decision function. Set to 0.0 if
        ``fit_intercept = False``.

    n_iter_ : None or ndarray of shape (n_targets,)
        Actual number of iterations for each target. Available only for
        sag and lsqr solvers. Other solvers will return None.

    classes_ : ndarray of shape (n_classes,)
        The classes labels.

    n_features_in_ : int
        Number of features seen during :term:`fit`.

        .. versionadded:: 0.24

    feature_names_in_ : ndarray of shape (`n_features_in_`,)
        Names of features seen during :term:`fit`. Defined only when `X`
        has feature names that are all strings.

        .. versionadded:: 1.0

    solver_ : str
        The solver that was used at fit time by the computational
        routines.

        .. versionadded:: 1.5

    See Also
    --------
    Ridge : Ridge regression.
    RidgeClassifierCV :  Ridge classifier with built-in cross validation.

    Notes
    -----
    For multi-class classification, n_class classifiers are trained in
    a one-versus-all approach. Concretely, this is implemented by taking
    advantage of the multi-variate response support in Ridge.

    Examples
    --------
    >>> from sklearn.datasets import load_breast_cancer
    >>> from sklearn.linear_model import RidgeClassifier
    >>> X, y = load_breast_cancer(return_X_y=True)
    >>> clf = RidgeClassifier().fit(X, y)
    >>> clf.score(X, y)
    0.9595...
    rS  balancedNr!  r   TrK   r   F)rw   r   rh   ri   rS  r   r   r   c          
      h    t                                          ||||||||	           || _        d S r8  r9  r%  rS  )r$  rg   rw   r   rh   ri   rS  r   r   r   r:  s             rA   r%  zRidgeClassifier.__init__  sN     	'% 	 		
 		
 		
 )rC   r   c                     |                      |||| j                  \  }}}}t                                          |||           | S )as  Fit Ridge classifier model.

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

        y : ndarray of shape (n_samples,)
            Target values.

        sample_weight : float or ndarray of shape (n_samples,), default=None
            Individual weights for each sample. If given a float, every sample
            will have the same weight.

            .. versionadded:: 0.17
               *sample_weight* support to RidgeClassifier.

        Returns
        -------
        self : object
            Instance of the estimator.
        r?  )rU  r   r9  r.  )r$  r>   rf   r   rT  r:  s        rA   r.  zRidgeClassifier.fit	  sJ    0 "&!3!3Aq-!U!U1mQAq666rC   c                     t                                                      }| j        dk    o| j        dk    p| j         |j        _        |S )Nr   r   )r9  rA  r   rw   rC  r
   rD  s     rA   rA  z RidgeClassifier.__sklearn_tags__&  sM    ww''))"&+"6 "
K:%?T-?)? 	 rC   r/  r:   )r   r0  r1  rF  r  r!  r2  r$   r3  r%  r   r.  rA  rG  rH  s   @rA   re  re  F  s        b bH$

+$zz:,77>$ $D    ) ) ) ) ) ) ) )2 \555     658        rC   re  c                 N    |dv r|S | j         d         | j         d         k    rdS dS )N)eigenr   r   r2   r   rl  rG   )r>   gcv_modes     rA   _check_gcv_modero  .  s6    ### 	wqzAGAJu7rC   c                 |    t          j        |                     |                    }t          j        |          }|S )a  Find the column of vectors that is most aligned with the query.

    Both query and the columns of vectors must have their l2 norm equal to 1.

    Parameters
    ----------
    query : ndarray of shape (n_samples,)
        Normalized query vector.

    vectors : ndarray of shape (n_samples, n_features)
        Vectors to which we compare query, as columns. Must be normalized.
    )r]   absr;   argmax)queryvectors
abs_cosineindexs       rA   _find_smallest_anglerw  8  s3     		'**++JIj!!ELrC   c                   4     e Zd ZdZ fdZd Zd Zd Z xZS )_X_CenterStackOpzBehaves as centered and scaled X with an added intercept column.

    This operator behaves as
    np.hstack([X - sqrt_sw[:, None] * X_mean, sqrt_sw[:, None]])
    c                     |j         \  }}t                                          |j        ||dz   f           || _        || _        || _        d S Nr2   rG   r9  r%  rN   r>   X_meansqrt_swr$  r>   r}  r~  rl   rm   r:  s         rA   r%  z_X_CenterStackOp.__init__Q  sM     !	:9j1n"=>>>rC   c                     |                                 }t          | j        |d d         d          | j        | j                            |d d                   z  z
  |d         | j        z  z   S Nr   Tr   )r   r'   r>   r~  r}  r;   r$  vs     rA   _matvecz_X_CenterStackOp._matvecX  sh    GGIIDFAcrcF>>>lT[__QssV4445edl"#	
rC   c                     t          | j        |d d         d          | j        d d d f         | j                            |d d                   z  z
  |d         | j        d d d f         z  z   S r  )r'   r>   r~  r}  r;   r  s     rA   _matmatz_X_CenterStackOp._matmat`  su    DFAcrcF>>>l111d7#dkooaf&=&==>edl111d7++,	
rC   c                 B    t          | j        | j        | j                  S r:   )_XT_CenterStackOpr>   r}  r~  r^  s    rA   
_transposez_X_CenterStackOp._transposeg  s     dlCCCrC   )	r   r0  r1  rF  r%  r  r  r  rG  rH  s   @rA   ry  ry  J  sv             
 
 

 
 
D D D D D D DrC   ry  c                   .     e Zd ZdZ fdZd Zd Z xZS )r  zBehaves as transposed centered and scaled X with an intercept column.

    This operator behaves as
    np.hstack([X - sqrt_sw[:, None] * X_mean, sqrt_sw[:, None]]).T
    c                     |j         \  }}t                                          |j        |dz   |f           || _        || _        || _        d S r{  r|  r  s         rA   r%  z_XT_CenterStackOp.__init__r  sM     !	::>9"=>>>rC   c                 J   |                                 }| j        d         }t          j        || j        j                  }t          | j        j        |d          | j        | j	        
                    |          z  z
  |d d<   t          j
        || j	                  |d<   |S )Nr   rM   Tr   r   )r   rG   r]   ra   r>   rN   r'   rE   r}  r~  r;   r$  r  rm   r  s       rA   r  z_XT_CenterStackOp._matvecy  s    GGIIZ]
hz666"468QTBBBK$,**1---
CRC &DL))B
rC   c                 P   | j         d         }t          j        ||j         d         f| j        j                  }t          | j        j        |d          | j        d d d f         | j        	                    |          z  z
  |d d<   t          j	        | j        |          |d<   |S )Nr   r2   rM   Tr   r   )
rG   r]   ra   r>   rN   r'   rE   r}  r~  r;   r  s       rA   r  z_XT_CenterStackOp._matmat  s    Z]
h
AGAJ/tv|DDD"468QTBBBT[AAtGF
LQF   CRC &q))B
rC   )r   r0  r1  rF  r%  r  r  rG  rH  s   @rA   r  r  k  s`                     rC   r  c                       e Zd ZdZd Zd ZdS )_IdentityRegressorz9Fake regressor which will directly output the prediction.c                     |S r:   rU   r$  	y_predicts     rA   rX  z$_IdentityRegressor.decision_function      rC   c                     |S r:   rU   r  s     rA   rZ  z_IdentityRegressor.predict  r  rC   N)r   r0  r1  rF  rX  rZ  rU   rC   rA   r  r    s8        CC      rC   r  c                       e Zd ZdZd Zd ZdS )_IdentityClassifierzFake classifier which will directly output the prediction.

    We inherit from LinearClassifierMixin to get the proper shape for the
    output `y`.
    c                     || _         d S r:   )r]  )r$  classess     rA   r%  z_IdentityClassifier.__init__  s    rC   c                     |S r:   rU   r  s     rA   rX  z%_IdentityClassifier.decision_function  r  rC   N)r   r0  r1  rF  r%  rX  rU   rC   rA   r  r    s<                  rC   r  c            	            e Zd ZdZ	 ddddddddddZed             Zed	             Zd
 Zd Z	d Z
d Zd Zd Zd Zd Zd Zd Zd ZddZd Zd Z fdZ xZS )	_RidgeGCVa	  Ridge regression with built-in Leave-one-out Cross-Validation.

    This class is not intended to be used directly. Use RidgeCV instead.

    `_RidgeGCV` uses a Generalized Cross-Validation for model selection. It's an
    efficient approximation of leave-one-out cross-validation (LOO-CV), where instead of
    computing multiple models by excluding one data point at a time, it uses an
    algebraic shortcut to approximate the LOO-CV error, making it faster and
    computationally more efficient.

    Using a naive grid-search approach with a leave-one-out cross-validation in contrast
    requires to fit `n_samples` models to compute the prediction error for each sample
    and then to repeat this process for each alpha in the grid.

    Here, the prediction error for each sample is computed by solving a **single**
    linear system (in other words a single model) via a matrix factorization (i.e.
    eigendecomposition or SVD) solving the problem stated in the Notes section. Finally,
    we need to repeat this process for each alpha in the grid. The detailed complexity
    is further discussed in Sect. 4 in [1].

    This algebraic approach is only applicable for regularized least squares
    problems. It could potentially be extended to kernel ridge regression.

    See the Notes section and references for more details regarding the formulation
    and the linear system that is solved.

    Notes
    -----

    We want to solve (K + alpha*Id)c = y,
    where K = X X^T is the kernel matrix.

    Let G = (K + alpha*Id).

    Dual solution: c = G^-1y
    Primal solution: w = X^T c

    Compute eigendecomposition K = Q V Q^T.
    Then G^-1 = Q (V + alpha*Id)^-1 Q^T,
    where (V + alpha*Id) is diagonal.
    It is thus inexpensive to inverse for many alphas.

    Let loov be the vector of prediction values for each example
    when the model was fitted with all examples but this example.

    loov = (KG^-1Y - diag(KG^-1)Y) / diag(I-KG^-1)

    Let looe be the vector of prediction errors for each example
    when the model was fitted with all examples but this example.

    looe = y - loov = c / diag(G^-1)

    The best score (negative mean squared error or user-provided scoring) is
    stored in the `best_score_` attribute, and the selected hyperparameter in
    `alpha_`.

    References
    ----------
    [1] http://cbcl.mit.edu/publications/ps/MIT-CSAIL-TR-2007-025.pdf
    [2] https://www.mit.edu/~9.520/spring07/Classes/rlsslides.pdf
    g?r   g      $@TNF)rw   scoringr   rn  store_cv_resultsis_clfalpha_per_targetc                v    || _         || _        || _        || _        || _        || _        || _        || _        d S r:   )alphasrw   r  r   rn  r  r  r  )	r$  r  rw   r  r   rn  r  r  r  s	            rA   r%  z_RidgeGCV.__init__  sE     *  0 0rC   c                 :    | |dz  z                       d          S )Nr   r   axis)sum)v_primeQs     rA   _decomp_diagz_RidgeGCV._decomp_diag  s#     !Q$###,,,rC   c                     t          |j                  dk    r<| t          d           ft          j        ft          |j                  dz
  z  z            } | |z  S r{  )r   rG   slicer]   r   )DBs     rA   	_diag_dotz_RidgeGCV._diag_dot  sM     qw<<!5;;.BJ=CLL14D#EEFA1urC   c                 |   | j         ot          j        |          }|s?t          j        |j        d         |j                  }t          ||j        d          |fS |j        d         }t          j	        |df||f          }|
                    |          }t          |d          \  }}|||
                    |          z  z  }|dddf         t          ||j        d          z  }	t          j        ||          t          j
        ||          z  }
t          ||j        d          |
z   |	z
  |	j        z
  |fS )	a  Computes the Gram matrix XX^T with possible centering.

        Parameters
        ----------
        X : {ndarray, sparse matrix} of shape (n_samples, n_features)
            The preprocessed design matrix.

        sqrt_sw : ndarray of shape (n_samples,)
            square roots of sample weights

        Returns
        -------
        gram : ndarray of shape (n_samples, n_samples)
            The Gram matrix.
        X_mean : ndarray of shape (n_feature,)
            The weighted mean of ``X`` for each feature.

        Notes
        -----
        When X is dense the centering has been done in preprocessing
        so the mean is 0 and we just compute XX^T.

        When X is sparse it has not been centered in preprocessing, but it has
        been scaled by sqrt(sample weights).

        When self.fit_intercept is False no centering is done.

        The centered X is never actually computed because centering would break
        the sparsity of X.
        r2   rM   Tr   r   rm  r  N)rw   r
   r}   r]   r   rG   rN   r'   rE   
dia_matrixr;   r.   r   )r$  r>   r~  centerr}  rl   sample_weight_matrix
X_weightedr   X_mXX_mX_ms              rA   _compute_gramz_RidgeGCV._compute_gram  sQ   > #:(:(: 	F Xagaj888F"1ac===vEEGAJ	%0aLI 6 
  
  
 *--a00
&z:::	)gkk'2222qqq$w/&!#D"Q"Q"QQ'7++bfVV.D.DDAqs666?$FO
 	
rC   c                    | j         s?t          j        |j        d         |j                  }t          |j        |d          |fS |j        d         }t          j        |df||f          }|	                    |          }t          |d          \  }}||z  |	                    |          z  }|	                    |          }t          |j        |d          |t          j        ||          z  z
  |fS )a\  Computes covariance matrix X^TX with possible centering.

        Parameters
        ----------
        X : sparse matrix of shape (n_samples, n_features)
            The preprocessed design matrix.

        sqrt_sw : ndarray of shape (n_samples,)
            square roots of sample weights

        Returns
        -------
        covariance : ndarray of shape (n_features, n_features)
            The covariance matrix.
        X_mean : ndarray of shape (n_feature,)
            The weighted mean of ``X`` for each feature.

        Notes
        -----
        Since X is sparse it has not been centered in preprocessing, but it has
        been scaled by sqrt(sample weights).

        When self.fit_intercept is False no centering is done.

        The centered X is never actually computed because centering would break
        the sparsity of X.
        r2   rM   Tr   r   rm  r  )rw   r]   r   rG   rN   r'   rE   r
   r  r;   r.   r   )	r$  r>   r~  r}  rl   r  r  r   
weight_sums	            rA   _compute_covariancez_RidgeGCV._compute_covariance:  s   8 ! 	F Xagaj888F"13===vEEGAJ	%0aLI 6 
  
  
 *--a00
&z:::	)#gkk'&:&::[[))
AC66628FF3334
 	
rC   c           	         |x}}|j         d         }t          j        |j         d         |j                  }t	          d|j         d         |          D ]
}	t          |	t          |j         d         |	|z             d          }
t          j        ||
         j         d         |j         d         | j        z   f|j                  }| j        rI||
                                         |||
         dddf         z  z
  |ddddf<   ||
         |dddf<   n||
                                         }|	                    |          |z  
                    d          ||
<   |S )a9  Compute the diagonal of (X - X_mean).dot(A).dot((X - X_mean).T)
        without explicitly centering X nor computing X.dot(A)
        when X is sparse.

        Parameters
        ----------
        X : sparse matrix of shape (n_samples, n_features)

        A : ndarray of shape (n_features, n_features)

        X_mean : ndarray of shape (n_features,)

        sqrt_sw : ndarray of shape (n_features,)
            square roots of sample weights

        Returns
        -------
        diag : np.ndarray, shape (n_samples,)
            The computed diagonal.
        r2   r   rM   Nr   r  )rG   r]   ra   rN   rb   r  minrw   toarrayr;   r  )r$  r>   r   r}  r~  intercept_colscale
batch_sizediagstartbatchX_batchs               rA   _sparse_multidot_diagz_RidgeGCV._sparse_multidot_diagj  sd   * !('WQZ
x
!'2221agaj*55 
	A 
	AE%QWQZ1C!D!DaHHEh5"AGAJ1C$CDAG  G ! -"#E("2"2"4"4veQQQPTW@U7U"U3B3!.u!52E(**,,";;q>>G388a8@@DKKrC   c                     |                      ||          \  }}| j        r|t          j        ||          z  }t	          j        |          \  }}t          j        |j        |          }||||fS )z?Eigendecomposition of X.X^T, used when n_samples <= n_features.)r  rw   r]   r   r   eighr;   rE   )	r$  r>   rf   r~  r   r}  eigvalsr  QT_ys	            rA   _eigen_decompose_gramz_RidgeGCV._eigen_decompose_gram  sq     &&q'22	6 	,
 '7+++A[^^
vac1~~w4''rC   c                 p   d||z   z  }| j         r7|t          j                            |          z  }	t	          |	|          }
d||
<   t          j        ||                     ||                    }|                     ||          }t          |j	                  dk    r|ddt          j
        f         }||fS )zCompute dual coefficients and diagonal of G^-1.

        Used when we have a decomposition of X.X^T (n_samples <= n_features).
        r   r   r2   Nrw   r]   r   normrw  r;   r  r  r   rG   r   )r$  rg   rf   r~  r}  r  r  r  r   normalized_swintercept_dimcG_inverse_diags                rA   _solve_eigen_gramz_RidgeGCV._solve_eigen_gram  s    
 7U?# 	! $binnW&=&==M0BBM AmF1dnnQ--..**1a00qw<<1+AAArzM:Nq  rC   c                    |j         \  }}t          j        |dz   |dz   f|j                  }|                     ||          \  |ddddf<   }| j        s|ddddf         }n&d|d<   d|dddf<   |                    |          |d<   t          d||z
            }t          j	        |          \  }	}
|	|d         }	|
dd|df         }
||	|
|fS )z_Eigendecomposition of X^T.X, used when n_samples > n_features
        and X is sparse.
        r2   rM   Nr   r   )r   r   )
rG   r]   ra   rN   r  rw   r;   r  r   r  )r$  r>   rf   r~  rl   rm   covr}  nullspace_dimr  Vs              rA   _eigen_decompose_covariancez%_RidgeGCV._eigen_decompose_covariance  s    !"	:h
Q
Q7qwGGG $ 8 8G D DCRC"Hv! 
	/crc3B3h-CC CGC2J!++g..CKAzI566[%%
-..)aaa w1$$rC   c                 p   d||z   z  }||z                       |j                  }	|	                     t          |j        |d                    }
t          ||
d          }|                     ||	||          }t	          |j                  dk    r|ddt          j        f         }d|z
  |z  ||z
  |z  fS )zCompute dual coefficients and diagonal of G^-1.

        Used when we have a decomposition of X^T.X
        (n_samples > n_features and X is sparse), and not fitting an intercept.
        r2   Tr   N)r;   rE   r'   r  r   rG   r]   r   )r$  rg   rf   r~  r}  r  r  r>   r   r   AXyy_hathat_diags                rA   $_solve_eigen_covariance_no_interceptz._RidgeGCV._solve_eigen_covariance_no_intercept  s     5!UKKeeOAC>>>??3T:::--aFGDDqw<<12:.HH%E	U':::rC   c                 "   t          j        |j        d                   }d|d<   t          ||          }	d||z   z  }
d||	         z  |
|	<   ||
z                      |j                  }t          |||          }|                    |j                            |                    }|                    |          }|                     ||||          }t          |j                  dk    r|ddt           j	        f         }d|z
  |z  ||z
  |z  fS )zCompute dual coefficients and diagonal of G^-1.

        Used when we have a decomposition of X^T.X
        (n_samples > n_features and X is sparse),
        and we are fitting an intercept.
        r   r2   r   N)
r]   r   rG   rw  r;   rE   ry  r  r   r   )r$  rg   rf   r~  r}  r  r  r>   intercept_svr  r   r   X_opr  r  r  s                   rA   !_solve_eigen_covariance_interceptz+_RidgeGCV._solve_eigen_covariance_intercept  s    x
++R,\1==5!w}55-UKK6733eeDFJJqMM""--aFGDDqw<<12:.HH%E	U':::rC   c           	      |    | j         r|                     |||||||          S |                     |||||||          S )zCompute dual coefficients and diagonal of G^-1.

        Used when we have a decomposition of X^T.X
        (n_samples > n_features and X is sparse).
        )rw   r  r  )r$  rg   rf   r~  r}  r  r  r>   s           rA   _solve_eigen_covariancez!_RidgeGCV._solve_eigen_covariance  s_      	99q'67Aq   881gvw1
 
 	
rC   c                    t          j        |j        d         |j                  }| j        r"|d d d f         }t          j        ||f          }t          j        |d          \  }}}|dz  }	t          j        |j	        |          }
||	||
fS )Nr2   rM   r   r   r   )
r]   r   rG   rN   rw   hstackr   r   r;   rE   )r$  r>   rf   r~  r}  intercept_columnr   singvalsr   singvals_sqUT_ys              rA   _svd_decompose_design_matrixz&_RidgeGCV._svd_decompose_design_matrix  s    !'!*AG444 	1  'qqq$w/	1./00AAQ7778Qkvac1~~{At++rC   c                    ||z   dz  |dz  z
  }| j         r;|t          j                            |          z  }	t	          |	|          }
|dz   ||
<   t          j        ||                     ||                    |dz  |z  z   }|                     ||          |dz  z   }t          |j	                  dk    r|ddt          j
        f         }||fS )zCompute dual coefficients and diagonal of G^-1.

        Used when we have an SVD decomposition of X
        (n_samples > n_features and X is dense).
        r   r2   Nr  )r$  rg   rf   r~  r}  r  r   r  r   r  r  r  r  s                rA   _solve_svd_design_matrixz"_RidgeGCV._solve_svd_design_matrix  s     E!b(UBY7 	,#binnW&=&==M0BBM!&|AmF1dnnQ--..%)q@**1a00E2I>qw<<1+AAArzM:Nq  rC   c           	         t          | ||g dt          j        gdd          \  }}| j        r	| j        rJ |t          |||j                  }t          j        | j                  | _        |}t          ||| j
        | j        |          \  }}}}}t          || j                  }	|	dk    r| j        }
| j        }n7|	dk    r1t!          j        |          r| j        }
| j        }n| j        }
| j        }|j        d	         }|t/          |||          \  }}}nt          j        ||j                  } |
|||          ^}}t3          |j                  d
k    rd
n|j        d
         }t          j        | j                  d	k    rd
nt3          | j                  }| j        r%t          j        ||z  |f|j                  | _        d\  }}}t=          t          j        | j                            D ]p\  }} |tA          |          |||g|R  \  }}| j!        F||z  dz  }| "                    |          }| j        r |#                                | j        dd|f<   ny|||z  z
  }| |j        d
k    r||dddf         z  }n||z  }||z  }| j        r |#                                | j        dd|f<   |pi }| $                    |||| j!        |          }|B| j        r3|d
k    r-|}t          j        |          }t          j%        ||          }$|}|}|},| j        r1|d
k    r+||k    }|dd|f         |dd|f<   ||         ||<   |||<   d||k    r|||}}}r|| _&        || _'        || _(        tS          | j(        j*        |          | _+        |j        d
k    s|j        d
         d
k    r| j+        #                                | _+        t!          j        |          r||z  }n|||z  z  }| ,                    |||           | j        rAt3          |j                  d
k    r||f}n|||f}| j        -                    |          | _        | S )a  Fit Ridge regression model with gcv.

        Parameters
        ----------
        X : {ndarray, sparse matrix} of shape (n_samples, n_features)
            Training data. Will be cast to float64 if necessary.

        y : ndarray of shape (n_samples,) or (n_samples, n_targets)
            Target values. Will be cast to float64 if necessary.

        sample_weight : float or ndarray of shape (n_samples,), default=None
            Individual weights for each sample. If given a float, every sample
            will have the same weight. Note that the scale of `sample_weight`
            has an impact on the loss; i.e. multiplying all weights by `k`
            is equivalent to setting `alpha / k`.

        score_params : dict, default=None
            Parameters to be passed to the underlying scorer.

            .. versionadded:: 1.5
                See :ref:`Metadata Routing User Guide <metadata_routing>` for
                more details.

        Returns
        -------
        self : object
        r   T)r   rN   r=  r>  NrM   r'  rl  r   r   r2   )NNNr   )squared_errors)predictionsrf   n_yscorerscore_params).r1   r]   r   r  r  r/   rN   r   r  r5   rw   r   ro  rn  r  r  r
   r}   r  r  r  r  rG   r6   r^   r   r   r  ra   cv_results_r  r   floatr  _score_without_scorerr   _scorer  alpha_best_score_
dual_coef_r'   rE   r  r+  r  )r$  r>   rf   r   r  
unscaled_yr?   r,  rk   rn  	decomposer   rl   r~  r}  decompositionr  n_alphas	best_coef
best_score
best_alpharp   rg   r  r  r  alpha_scorer  	to_updatecv_results_shapes                                 rA   r.  z_RidgeGCV.fit.  s   8 ///:,
 
 
1 K:D$9:::$0QQQMj--
,<,'-
 -
 -
)1h' #1dm44w2I*EEq!! 6 <	4 =	5GAJ	$)!Q>>MAq''giqw777G!*1a!9!9qw<<1$$aa!'!*,,1111s4;7G7G  	T!xS((C17SSSD,<)	:z!"-"<"<== 0	N 0	NHAu %eEllAw W W W WNA|#"#n"4!:"888WW( D-;-A-A-C-CD$QQQT*1~#56 ,"'!++#wqqq$w'77#w.x'( A-8->->-@-@D$QQQT*+1r"kk + <!- *   !( 'S1WW !I!#{!;!;J!#e!4!4JJ !I!,J!&JJ ( NS1WW +j 8I./9oIaaal+,7	,BJy),1Jy)) :--89;:zI %#$T_%6::
6Q;;!'!*//))++DJ?1 	)'HH((HHh888  	J17||q  #,h#6  #,c8#; #/778HIIDrC   c                 n    | j         r|                    d           }n|                                 }|S )z>Performs scoring using squared errors when the scorer is None.r   r  )r  mean)r$  r  r  s      rA   r  z_RidgeGCV._score_without_scorer  sA      	,$))q)111FF$))+++FrC   c                F   | j         rBt          t          j        |                                         d          fi }nRt                      | j        r2t          j        fdt          |          D                       }n fi }|S )zhPerforms scoring with the specified scorer using the
        predictions and the true y values.
        )r  r2   r  c           	      N    g | ]!} d d |f         d d |f         fi "S r:   rU   ).0jidentity_estimatorr  r  r  rf   s     rA   
<listcomp>z$_RidgeGCV._score.<locals>.<listcomp>  sh         .'1-aaadG  +	   rC   )	r  r  r]   arangerr  r  r  arrayrb   )r$  r  rf   r  r  r  r  r	  s    `` `` @rA   r  z_RidgeGCV._score  s     ; 	T!4RYs^^!L!L!LV"a    	 FF "4!5!5$ T        "'s  
 
   2KSSlSSrC   c                 `    t                                                      }d|j        _        |S r`  )r9  rA  target_tagsrequiredrD  s     rA   rA  z_RidgeGCV.__sklearn_tags__  s(    ww''))$(!rC   r  )NN)r   r0  r1  rF  r%  staticmethodr  r  r  r  r  r  r  r  r  r  r  r  r  r.  r  r  rA  rG  rH  s   @rA   r  r    s       < <@  1 1 1 1 1 1* - - \-   \2
 2
 2
h.
 .
 .
`# # #J( ( (! ! !.% % %2; ; ;$; ; ;>
 
 
, , ,! ! !(] ] ] ]~    >        rC   r  c                   `    e Zd ZU d eeddd          gdg e e e                                edgdg eh d          dgd e	d          gdgd e	 ed	h                    gd
Z
eed<   	 dddddddd	ddZddZd Zd Z ed          ed                         Z fdZ xZS )_BaseRidgeCVr   r   Nr   r   r   	cv_object>   r   r   rl  r   r  rw   r  cvrn  r  r  store_cv_valuesr!  r  TF)rw   r  r  rn  r  r  r  c                v    || _         || _        || _        || _        || _        || _        || _        || _        d S r:   r  )	r$  r  rw   r  r  rn  r  r  r  s	            rA   r%  z_BaseRidgeCV.__init__	  sF     *  0 0.rC   c           
      L   t          || d           | j        }|                                 }| j        dk    r=| j        t          d          t          j        dt                     | j        | _	        n| j        d| _	        n| j        | _	        |#t          t          t          j        dd	          }n"t          t          t          j        dd
	          }t          | j        t           j        t$          t&          f          rt!          j        | j                  dk    rdnt+          | j                  }|dk    r+t-          | j                  D ]\  }	}
 ||
d|	 d          }
n || j        d         d          | j        d<   t!          j        | j                  }|||d<   |t1                      rt3          | dfi |}n/t5          t5          i                     }|||j        j        d<   | j        d}t=          || j        || j         | j	        tC          |           | j"                  }|#                    ||||j        j                   |j$        | _$        |j%        | _%        | j	        r|j&        | _&        n| j	        rt          d          | j"        rt          d          d|i}tO          j(        |          rdnd}tC          |           rtR          ntT          } || j        |          }t1                      r|+                    d           tY          ||||          } |j#        ||fi | |j-        }|j-        j.        | _$        |j%        | _%        |j/        | _/        |j0        | _0        |j1        | _1        te          |d          r|j3        | _3        | S )a  Fit Ridge regression model with cv.

        Parameters
        ----------
        X : ndarray of shape (n_samples, n_features)
            Training data. If using GCV, will be cast to float64
            if necessary.

        y : ndarray of shape (n_samples,) or (n_samples, n_targets)
            Target values. Will be cast to X's dtype if necessary.

        sample_weight : float or ndarray of shape (n_samples,), default=None
            Individual weights for each sample. If given a float, every sample
            will have the same weight.

        **params : dict, default=None
            Extra parameters for the underlying scorer.

            .. versionadded:: 1.5
                Only available if `enable_metadata_routing=True`,
                which can be set by using
                ``sklearn.set_config(enable_metadata_routing=True)``.
                See :ref:`Metadata Routing User Guide <metadata_routing>` for
                more details.

        Returns
        -------
        self : object
            Fitted estimator.

        Notes
        -----
        When sample_weight is provided, the selected hyperparameter may depend
        on whether we use leave-one-out cross-validation (cv=None or cv='auto')
        or another form of cross-validation, because only leave-one-out
        cross-validation takes the sample weights into account when computing
        the validation score.
        r.  r   NzBoth 'store_cv_values' and 'store_cv_results' were set. 'store_cv_values' is deprecated in version 1.5 and will be removed in 1.7. To avoid this error, only set 'store_cv_results'.zj'store_cv_values' is deprecated in version 1.5 and will be removed in 1.7. Use 'store_cv_results' instead.Fr   r   r   r   r   r2   zalphas[]r  r   )score)r  )rw   r  rn  r  r  r  )r   r  z3cv!=None and store_cv_results=True are incompatiblez3cv!=None and alpha_per_target=True are incompatiblerg   r   r   )rw   r   Tr?  )r  r  feature_names_in_)4r+   r  _get_scorerr  r  rc   rd   re   FutureWarning_store_cv_resultsr   r   r  r   r   r  r]   r   listtupler   r   r  r   r,   r-   r   r  r  r  r  rw   rn  r   r  r.  r  r  r  r
   r}   re  r6  set_fit_requestr   best_estimator_rg   r  r)  n_features_in_hasattrr  )r$  r>   rf   r   r-  r  r  check_scalar_alphar  rv  rg   r  routed_params	estimator
parametersr   modelgrid_searchs                     rA   r.  z_BaseRidgeCV.fit	  s@   N 	&$...W!!## <//$0 X  
 MF    &*%9D"""*%*D""%)%:D" :!(#L#,	" " " ")#L#)	" " " dkBJe#<== 	NGDK00A55qq3t{;K;KH1}}$-dk$:$: J JLE5..u6H6H6H6HIIEEJ "4!3DKNH!M!MADK(($&3F?#:!! 	P /! ! ! ! !&U___ = = = ,BOM(.? |#!"0!%!7$T**!%!6  I MM+*17	     $*DK(4D% 9#,#8 % X !VWWW$ X !VWWW!6*J$*OA$6$6B[[FF'4T':':EOOE"0  I  !! >)))===&	  K KOAq++F+++#3I%5;DK*6D_
#.'69122 	A%.%@D"rC   c                 L   t          | j        j                                      |                               | j        t                                          dd                                        | j        t                                          dd                    }|S )aj  Get metadata routing of this object.

        Please check :ref:`User Guide <metadata_routing>` on how the routing
        mechanism works.

        .. versionadded:: 1.5

        Returns
        -------
        routing : MetadataRouter
            A :class:`~sklearn.utils.metadata_routing.MetadataRouter` encapsulating
            routing information.
        )ownerr.  r  )callercallee)r  method_mappingsplit)splitterr0  )r)   r:  r   add_self_requestaddr  r*   r  )r$  routers     rA   get_metadata_routingz!_BaseRidgeCV.get_metadata_routing	  s     !8999d##S|,22%2PP    S,22%2PP    	 rC   c                     t          | | j        d          }t                      r| j        |                    d           |S )NT)r(  r  
allow_noner?  )r   r  r,   set_score_request)r$  r  s     rA   r  z_BaseRidgeCV._get_scorer	  sL    t|PTUUU 	9$,"6
 $$4$888rC   zjAttribute `cv_values_` is deprecated in version 1.5 and will be removed in 1.7. Use `cv_results_` instead.c                     | j         S r:   )r  r^  s    rA   
cv_values_z_BaseRidgeCV.cv_values_	  s     rC   c                 `    t                                                      }d|j        _        |S r`  )r9  rA  rC  r
   rD  s     rA   rA  z_BaseRidgeCV.__sklearn_tags__	  s'    ww''))!%rC   r  r:   )r   r0  r1  r#   r   r$   setr   callabler"   r!  r2  r3  r%  r.  r6  r  r   rc  r;  rA  rG  rH  s   @rA   r  r    s        $4	!J!J!JK#Jss#3#3#5#566774HmZ 8 8 8994@&t5&K%vvjj,.H.H'I'IJ	$ 	$D 	 	 	  / $/ / / / /*b b b bH  8   Z	-      X	 
         rC   r  c                   H     e Zd ZdZ ed          d fd	            Z xZS )RidgeCVa  Ridge regression with built-in cross-validation.

    See glossary entry for :term:`cross-validation estimator`.

    By default, it performs efficient Leave-One-Out Cross-Validation.

    Read more in the :ref:`User Guide <ridge_regression>`.

    Parameters
    ----------
    alphas : array-like of shape (n_alphas,), default=(0.1, 1.0, 10.0)
        Array of alpha values to try.
        Regularization strength; must be a positive float. Regularization
        improves the conditioning of the problem and reduces the variance of
        the estimates. Larger values specify stronger regularization.
        Alpha corresponds to ``1 / (2C)`` in other linear models such as
        :class:`~sklearn.linear_model.LogisticRegression` or
        :class:`~sklearn.svm.LinearSVC`.
        If using Leave-One-Out cross-validation, alphas must be strictly positive.

    fit_intercept : bool, default=True
        Whether to calculate the intercept for this model. If set
        to false, no intercept will be used in calculations
        (i.e. data is expected to be centered).

    scoring : str, callable, default=None
        A string (see :ref:`scoring_parameter`) or a scorer callable object /
        function with signature ``scorer(estimator, X, y)``. If None, the
        negative mean squared error if cv is 'auto' or None (i.e. when using
        leave-one-out cross-validation), and r2 score otherwise.

    cv : int, cross-validation generator or an iterable, default=None
        Determines the cross-validation splitting strategy.
        Possible inputs for cv are:

        - None, to use the efficient Leave-One-Out cross-validation
        - integer, to specify the number of folds.
        - :term:`CV splitter`,
        - An iterable yielding (train, test) splits as arrays of indices.

        For integer/None inputs, if ``y`` is binary or multiclass,
        :class:`~sklearn.model_selection.StratifiedKFold` is used, else,
        :class:`~sklearn.model_selection.KFold` is used.

        Refer :ref:`User Guide <cross_validation>` for the various
        cross-validation strategies that can be used here.

    gcv_mode : {'auto', 'svd', 'eigen'}, default='auto'
        Flag indicating which strategy to use when performing
        Leave-One-Out Cross-Validation. Options are::

            'auto' : use 'svd' if n_samples > n_features, otherwise use 'eigen'
            'svd' : force use of singular value decomposition of X when X is
                dense, eigenvalue decomposition of X^T.X when X is sparse.
            'eigen' : force computation via eigendecomposition of X.X^T

        The 'auto' mode is the default and is intended to pick the cheaper
        option of the two depending on the shape of the training data.

    store_cv_results : bool, default=False
        Flag indicating if the cross-validation values corresponding to
        each alpha should be stored in the ``cv_results_`` attribute (see
        below). This flag is only compatible with ``cv=None`` (i.e. using
        Leave-One-Out Cross-Validation).

        .. versionchanged:: 1.5
            Parameter name changed from `store_cv_values` to `store_cv_results`.

    alpha_per_target : bool, default=False
        Flag indicating whether to optimize the alpha value (picked from the
        `alphas` parameter list) for each target separately (for multi-output
        settings: multiple prediction targets). When set to `True`, after
        fitting, the `alpha_` attribute will contain a value for each target.
        When set to `False`, a single alpha is used for all targets.

        .. versionadded:: 0.24

    store_cv_values : bool
        Flag indicating if the cross-validation values corresponding to
        each alpha should be stored in the ``cv_values_`` attribute (see
        below). This flag is only compatible with ``cv=None`` (i.e. using
        Leave-One-Out Cross-Validation).

        .. deprecated:: 1.5
            `store_cv_values` is deprecated in version 1.5 in favor of
            `store_cv_results` and will be removed in version 1.7.

    Attributes
    ----------
    cv_results_ : ndarray of shape (n_samples, n_alphas) or             shape (n_samples, n_targets, n_alphas), optional
        Cross-validation values for each alpha (only available if
        ``store_cv_results=True`` and ``cv=None``). After ``fit()`` has been
        called, this attribute will contain the mean squared errors if
        `scoring is None` otherwise it will contain standardized per point
        prediction values.

        .. versionchanged:: 1.5
            `cv_values_` changed to `cv_results_`.

    coef_ : ndarray of shape (n_features) or (n_targets, n_features)
        Weight vector(s).

    intercept_ : float or ndarray of shape (n_targets,)
        Independent term in decision function. Set to 0.0 if
        ``fit_intercept = False``.

    alpha_ : float or ndarray of shape (n_targets,)
        Estimated regularization parameter, or, if ``alpha_per_target=True``,
        the estimated regularization parameter for each target.

    best_score_ : float or ndarray of shape (n_targets,)
        Score of base estimator with best alpha, or, if
        ``alpha_per_target=True``, a score for each target.

        .. versionadded:: 0.23

    n_features_in_ : int
        Number of features seen during :term:`fit`.

        .. versionadded:: 0.24

    feature_names_in_ : ndarray of shape (`n_features_in_`,)
        Names of features seen during :term:`fit`. Defined only when `X`
        has feature names that are all strings.

        .. versionadded:: 1.0

    See Also
    --------
    Ridge : Ridge regression.
    RidgeClassifier : Classifier based on ridge regression on {-1, 1} labels.
    RidgeClassifierCV : Ridge classifier with built-in cross validation.

    Examples
    --------
    >>> from sklearn.datasets import load_diabetes
    >>> from sklearn.linear_model import RidgeCV
    >>> X, y = load_diabetes(return_X_y=True)
    >>> clf = RidgeCV(alphas=[1e-3, 1e-2, 1e-1, 1]).fit(X, y)
    >>> clf.score(X, y)
    0.5166...
    Tr   Nc                 B     t                      j        ||fd|i| | S )a  Fit Ridge regression model with cv.

        Parameters
        ----------
        X : ndarray of shape (n_samples, n_features)
            Training data. If using GCV, will be cast to float64
            if necessary.

        y : ndarray of shape (n_samples,) or (n_samples, n_targets)
            Target values. Will be cast to X's dtype if necessary.

        sample_weight : float or ndarray of shape (n_samples,), default=None
            Individual weights for each sample. If given a float, every sample
            will have the same weight.

        **params : dict, default=None
            Parameters to be passed to the underlying scorer.

            .. versionadded:: 1.5
                Only available if `enable_metadata_routing=True`,
                which can be set by using
                ``sklearn.set_config(enable_metadata_routing=True)``.
                See :ref:`Metadata Routing User Guide <metadata_routing>` for
                more details.

        Returns
        -------
        self : object
            Fitted estimator.

        Notes
        -----
        When sample_weight is provided, the selected hyperparameter may depend
        on whether we use leave-one-out cross-validation (cv=None or cv='auto')
        or another form of cross-validation, because only leave-one-out
        cross-validation takes the sample weights into account when computing
        the validation score.
        r   )r9  r.  )r$  r>   rf   r   r-  r:  s        rA   r.  zRidgeCV.fit
  s0    P 	Aq@@@@@@rC   r:   )r   r0  r1  rF  r   r.  rG  rH  s   @rA   r@  r@  	  sd        N N` \555( ( ( ( ( 65( ( ( ( (rC   r@  c                        e Zd ZU dZi ej        de edh          dgiZeed<   dD ]Z	e
                    e	           	 ddddddd	d
 fdZ ed          d fd	            Z xZS )RidgeClassifierCVa  Ridge classifier with built-in cross-validation.

    See glossary entry for :term:`cross-validation estimator`.

    By default, it performs Leave-One-Out Cross-Validation. Currently,
    only the n_features > n_samples case is handled efficiently.

    Read more in the :ref:`User Guide <ridge_regression>`.

    Parameters
    ----------
    alphas : array-like of shape (n_alphas,), default=(0.1, 1.0, 10.0)
        Array of alpha values to try.
        Regularization strength; must be a positive float. Regularization
        improves the conditioning of the problem and reduces the variance of
        the estimates. Larger values specify stronger regularization.
        Alpha corresponds to ``1 / (2C)`` in other linear models such as
        :class:`~sklearn.linear_model.LogisticRegression` or
        :class:`~sklearn.svm.LinearSVC`.
        If using Leave-One-Out cross-validation, alphas must be strictly positive.

    fit_intercept : bool, default=True
        Whether to calculate the intercept for this model. If set
        to false, no intercept will be used in calculations
        (i.e. data is expected to be centered).

    scoring : str, callable, default=None
        A string (see :ref:`scoring_parameter`) or a scorer callable object /
        function with signature ``scorer(estimator, X, y)``.

    cv : int, cross-validation generator or an iterable, default=None
        Determines the cross-validation splitting strategy.
        Possible inputs for cv are:

        - None, to use the efficient Leave-One-Out cross-validation
        - integer, to specify the number of folds.
        - :term:`CV splitter`,
        - An iterable yielding (train, test) splits as arrays of indices.

        Refer :ref:`User Guide <cross_validation>` for the various
        cross-validation strategies that can be used here.

    class_weight : dict or 'balanced', default=None
        Weights associated with classes in the form ``{class_label: weight}``.
        If not given, all classes are supposed to have weight one.

        The "balanced" mode uses the values of y to automatically adjust
        weights inversely proportional to class frequencies in the input data
        as ``n_samples / (n_classes * np.bincount(y))``.

    store_cv_results : bool, default=False
        Flag indicating if the cross-validation results corresponding to
        each alpha should be stored in the ``cv_results_`` attribute (see
        below). This flag is only compatible with ``cv=None`` (i.e. using
        Leave-One-Out Cross-Validation).

        .. versionchanged:: 1.5
            Parameter name changed from `store_cv_values` to `store_cv_results`.

    store_cv_values : bool
        Flag indicating if the cross-validation values corresponding to
        each alpha should be stored in the ``cv_values_`` attribute (see
        below). This flag is only compatible with ``cv=None`` (i.e. using
        Leave-One-Out Cross-Validation).

        .. deprecated:: 1.5
            `store_cv_values` is deprecated in version 1.5 in favor of
            `store_cv_results` and will be removed in version 1.7.

    Attributes
    ----------
    cv_results_ : ndarray of shape (n_samples, n_targets, n_alphas), optional
        Cross-validation results for each alpha (only if ``store_cv_results=True`` and
        ``cv=None``). After ``fit()`` has been called, this attribute will
        contain the mean squared errors if `scoring is None` otherwise it
        will contain standardized per point prediction values.

        .. versionchanged:: 1.5
            `cv_values_` changed to `cv_results_`.

    coef_ : ndarray of shape (1, n_features) or (n_targets, n_features)
        Coefficient of the features in the decision function.

        ``coef_`` is of shape (1, n_features) when the given problem is binary.

    intercept_ : float or ndarray of shape (n_targets,)
        Independent term in decision function. Set to 0.0 if
        ``fit_intercept = False``.

    alpha_ : float
        Estimated regularization parameter.

    best_score_ : float
        Score of base estimator with best alpha.

        .. versionadded:: 0.23

    classes_ : ndarray of shape (n_classes,)
        The classes labels.

    n_features_in_ : int
        Number of features seen during :term:`fit`.

        .. versionadded:: 0.24

    feature_names_in_ : ndarray of shape (`n_features_in_`,)
        Names of features seen during :term:`fit`. Defined only when `X`
        has feature names that are all strings.

        .. versionadded:: 1.0

    See Also
    --------
    Ridge : Ridge regression.
    RidgeClassifier : Ridge classifier.
    RidgeCV : Ridge regression with built-in cross validation.

    Notes
    -----
    For multi-class classification, n_class classifiers are trained in
    a one-versus-all approach. Concretely, this is implemented by taking
    advantage of the multi-variate response support in Ridge.

    Examples
    --------
    >>> from sklearn.datasets import load_breast_cancer
    >>> from sklearn.linear_model import RidgeClassifierCV
    >>> X, y = load_breast_cancer(return_X_y=True)
    >>> clf = RidgeClassifierCV(alphas=[1e-3, 1e-2, 1e-1, 1]).fit(X, y)
    >>> clf.score(X, y)
    0.9630...
    rS  rf  Nr!  )rn  r  r  Tr   )rw   r  r  rS  r  r  c                d    t                                          ||||||           || _        d S )N)r  rw   r  r  r  r  rh  )	r$  r  rw   r  r  rS  r  r  r:  s	           rA   r%  zRidgeClassifierCV.__init__A  sH     	'-+ 	 	
 	
 	
 )rC   r   c                     |                      |||d          \  }}}}| j        |n|} t                      j        ||fd|i| | S )ar  Fit Ridge classifier with cv.

        Parameters
        ----------
        X : ndarray of shape (n_samples, n_features)
            Training vectors, where `n_samples` is the number of samples
            and `n_features` is the number of features. When using GCV,
            will be cast to float64 if necessary.

        y : ndarray of shape (n_samples,)
            Target values. Will be cast to X's dtype if necessary.

        sample_weight : float or ndarray of shape (n_samples,), default=None
            Individual weights for each sample. If given a float, every sample
            will have the same weight.

        **params : dict, default=None
            Parameters to be passed to the underlying scorer.

            .. versionadded:: 1.5
                Only available if `enable_metadata_routing=True`,
                which can be set by using
                ``sklearn.set_config(enable_metadata_routing=True)``.
                See :ref:`Metadata Routing User Guide <metadata_routing>` for
                more details.

        Returns
        -------
        self : object
            Fitted estimator.
        rl  )r   Nr   )rU  r  r9  r.  )r$  r>   rf   r   r-  rT  r   r:  s          rA   r.  zRidgeClassifierCV.fitV  sg    H "&!3!3Aq-PW!3!X!X1mQ go1AvEE]EfEEErC   r  r:   )r   r0  r1  rF  r  r!  r2  r$   r3  parampopr%  r   r.  rG  rH  s   @rA   rC  rC  
  s        C CJ$

-$zz:,77>$ $D    2 * *""5))))  ) $) ) ) ) ) ) )* \555, , , , , 65, , , , ,rC   rC  )NrK   r   NNN)NFr:   )TNrK   NNN)Nr   NrK   r   FNFFFNNTF)hrF  r  rd   abcr   r   	functoolsr   r   r   numpyr]   scipyr   r	   r
   scipy.sparser_   sklearn.baser   baser   r   r   r   
exceptionsr   metricsr   r   model_selectionr   preprocessingr   utilsr   r   r   r   r   r   r   utils._array_apir   r   r   r    r!   utils._param_validationr"   r#   r$   r%   utils.extmathr&   r'   utils.fixesr(   utils.metadata_routingr)   r*   r+   r,   r-   utils.sparsefuncsr.   utils.validationr/   r0   r1   _baser3   r4   r5   r6   _sagr7   rJ   rv   r   r   r   r   r   r   rH   r   r   r   r  r  r6  rJ  re  ro  rw  ry  r  r  r  r  r  r@  rC  rU   rC   rA   <module>r]     s      ' ' ' ' ' ' ' '       " " " " " " " "     * * * * * * * * * * , , , , , , & & & & & & P P P P P P P P P P P P + + + + + + 5 5 5 5 5 5 5 5 * * * * * * * * * * * *                               T S S S S S S S S S S S 6 6 6 6 6 6 6 6 + + + + + +              3 2 2 2 2 2 S S S S S S S S S S V V V V V V V V V V V V        ( F F F F\ 5 5 5 5 5p  ,; ; ; ;|	 	 	 	  D D D DN% % % OY-EF^(4D888,GHT4i888
 JXXX 

 Xh4???Fq$v6667;K'(#&K!{+ . #'1  > @ @ @ @5 4@N #b4 b4 b4 b4J  @  I I I I I I I I IXt t t t tnj t t tnW W W W W1 W W Wte e e e e+Z e e eP    $D D D D Dv}3 D D DB    4   D           /   T	 T	 T	 T	 T	 T	 T	 T	nx x x x x; x x xvz z z z z z z zzO O O O O-| O O O O OrC   