
\c        	   @   sy  d  Z  d d l m Z m Z d d l Z d d l Z d d l m Z d d l m	 Z	 d d l
 m Z d d l m Z m Z m Z d d l m Z d	 d
 l m Z d	 d l m Z d	 d l m Z d	 d l m Z d	 d l m Z d	 d l m Z d	 d l m Z d	 d l m Z d	 d l m Z d	 d l m Z d	 d l  m! Z! d	 d l" m# Z# d	 d l$ m% Z% e& d d d  Z' e& d d  Z( d   Z) e& e* d  Z+ d   Z, e& d e& d d e& e* e* d  Z- d  e! j. e e  f d!     YZ/ d" e/ e f d#     YZ0 d$ e e/ f d%     YZ1 d& e f d'     YZ2 d( e f d)     YZ3 d* e3 e f d+     YZ4 d, e e3 f d-     YZ5 d S(.   s   
Ridge regression
i(   t   ABCMetat   abstractmethodN(   t   linalg(   t   sparsei   (   t   LinearClassifierMixint   LinearModelt   _rescale_data(   t
   sag_solveri   (   t   RegressorMixin(   t   safe_sparse_dot(   t	   row_norms(   t	   check_X_y(   t   check_array(   t   check_consistent_length(   t   compute_sample_weight(   t   column_or_1d(   t   LabelBinarizer(   t   GridSearchCV(   t   six(   t   check_scoring(   t   ConvergenceWarninggMbP?i    c      
      sM  |  j  \ } } t j |     t j | j  d | f d |  j } | | k ra   f d   }	 n   f d   }	 xt | j  d  D]}
 | d  d   |
 f } |	 | |
  } | | k rPt j | | f d | d |  j } y( t j | | d | d d \ } } Wn/ t	 k
 r9t j | | d | \ } } n X  j
 |  | |
 <n   j
 |  } t j | | f d | d |  j } y2 t j | | d	 | d | d d \ | |
 <} Wn9 t	 k
 rt j | | d	 | d | \ | |
 <} n X| d
 k  rt d |   n  | d  k r | d
 k r | r t j d | t  q q W| S(   Ni   t   dtypec            s      f d   } | S(   Nc            s     j    j |     |  S(   N(   t   matvect   rmatvec(   t   x(   t   X1t
   curr_alpha(    s9   lib/python2.7/site-packages/sklearn/linear_model/ridge.pyt   _mv,   s    (    (   R   R   (   R   (   R   s9   lib/python2.7/site-packages/sklearn/linear_model/ridge.pyt	   create_mv+   s    c            s      f d   } | S(   Nc            s     j    j |     |  S(   N(   R   R   (   R   (   R   R   (    s9   lib/python2.7/site-packages/sklearn/linear_model/ridge.pyR   1   s    (    (   R   R   (   R   (   R   s9   lib/python2.7/site-packages/sklearn/linear_model/ridge.pyR   0   s    R   t   tolt   atolt   legacyt   maxiteri    s   Failed with error code %ds/   sparse_cg did not converge after %d iterations.(   t   shapet	   sp_linalgt   aslinearoperatort   npt   emptyR   t   ranget   LinearOperatort   cgt	   TypeErrorR   t
   ValueErrort   Nonet   warningst   warnR   (   t   Xt   yt   alphat   max_iterR   t   verboset	   n_samplest
   n_featurest   coefsR   t   it   y_columnt   mvt   Ct   coeft   info(    (   R   s9   lib/python2.7/site-packages/sklearn/linear_model/ridge.pyt   _solve_sparse_cg%   s@    %("	c         C   s   |  j  \ } } t j | j  d | f d |  j } t j | j  d d t j } t j |  }	 x{ t | j  d  D]f }
 | d  d   |
 f } t j |  | d |	 |
 d | d | d | } | d | |
 <| d | |
 <qv W| | f S(	   Ni   R   t   dampR   t   btolt   iter_limi    i   (	   R!   R$   R%   R   t   int32t   sqrtR&   R"   t   lsqr(   R.   R/   R0   R1   R   R3   R4   R5   t   n_itert
   sqrt_alphaR6   R7   R;   (    (    s9   lib/python2.7/site-packages/sklearn/linear_model/ridge.pyt   _solve_lsqr^   s    %c         C   sl  |  j  \ } } | j  d } t |  j |  d t } t |  j | d t } t j | t |  | d g  } | r | j d  d  | d  c | d 7<t j	 | | d t d t j St j
 | | g d |  j }	 x t |	 | j |  D]t \ }
 } } | j d  d  | d  c | 7<t j	 | | d t d t j   |
 (| j d  d  | d  c | 8<q W|	 Sd  S(   Ni   t   dense_outputi    t   sym_post   overwrite_aR   (   R!   R	   t   Tt   TrueR$   t   array_equalt   lent   flatR   t   solveR%   R   t   zipt   Falset   ravel(   R.   R/   R0   R3   R4   t	   n_targetst   At   Xyt	   one_alphaR5   R:   t   targett   current_alpha(    (    s9   lib/python2.7/site-packages/sklearn/linear_model/ridge.pyt   _solve_choleskyp   s     #$
% $c         C   sw  |  j  d } | j  d } | r/ |  j   }  n  t j |  } | | d k j   } t | t j  po | d k } | r t j t j |   }	 | |	 d  d   t j	 f } |  t j
 |	 |	  9}  n  | r|  j d  d  | d  c | d 7<y" t j |  | d t d t }
 Wn: t j j k
 rNt j d  t j |  |  d }
 n X|  j d  d  | d  c | d 8<| r|
 |	 d  d   t j	 f 9}
 n  |
 St j | | g |  j  } x t | | j |  D]t \ }
 } } |  j d  d  | d  c | 7<t j |  | d t d t j   |
 (|  j d  d  | d  c | 8<qW| rl| |	 t j	 d  d   f 9} n  | j Sd  S(   Ni    i   g      ?RG   RH   sN   Singular matrix in solving dual problem. Using least-squares solution instead.(   g      ?N(   R!   t   copyR$   t
   atleast_1dt   allt
   isinstancet   ndarrayR+   RA   t   newaxist   outerRM   R   RN   RJ   RP   t   LinAlgErrorR,   R-   t   lstsqR%   R   RO   RI   RQ   (   t   KR/   R0   t   sample_weightRY   R3   RR   RU   t   has_swt   swt	   dual_coeft
   dual_coefsRV   RW   (    (    s9   lib/python2.7/site-packages/sklearn/linear_model/ridge.pyt   _solve_cholesky_kernel   sB    $$ % $ c         C   s   t  j |  d t \ } } } | d k } | | d  d   t j f } t j | j |  } t j | j | j f d |  j	 }	 | | d | |	 | <|	 | }
 t j | j |
  j S(   Nt   full_matricesgV瞯<R   i   (
   R   t   svdRP   R$   R^   t   dotRI   t   zerost   sizeR   (   R.   R/   R0   t   Ut   st   Vtt   idxt   s_nnzt   UTyt   dt   d_UT_y(    (    s9   lib/python2.7/site-packages/sklearn/linear_model/ridge.pyt
   _solve_svd   s    $
t   autoc         C   s  |
 rF t  j |   rF | d k rF | d k r= t j d  n  d } n  t j t j g } | d! k r t |  d d g d t j d d	 }  t | d t j d
 t d d } n< t |  d d d d g d | }  t | d |  j	 d
 t } t
 |  |  |  j \ } } | j d k r/t d t | j    n  t } | j d k r_| j d d  } t } n  | j \ } } | | k rt d | | f   n  | d  k	 } | d k rt  j |   s| rd } qd } n  | r*t j |  j d k r t d   n  | d" k r*t |  | |  \ }  } q*n  t j | d |  j	 j   } | j d | g k ryt d | j | f   n  | j d k r| d k rt j | |  } n  | d# k rt d |   n  d  } | d k rt |  | | | | |  } ng| d k r%t |  | | | |  \ } } n:| d k r| | k rt |  |  j d t } y1 t | | |  } t |  j | d t j } Wqt j k
 rd } qXq_y t |  | |  } Wq_t j k
 rd } q_Xn| d$ k r_t  |  d t j!   } t j" | j d | f  } t j" | j d d t j# } t j$ | j d f  } x t% t& | | j   D] \ } \ } } i t j$ | t' |
  d f  d 6} t( |  | j   | d | d | | | | t | | d | d k \ } } } |
 r| d  | | <| d | | <n
 | | | <| | | <qmW| j d d k rM| d } n  t j |  } n  | d k rt  j |   rt) d   n  t* |  | |  } n  | r| j   } n  |	 r|
 r| | | f S|
 r| | f S|	 r| | f S| Sd  S(%   s  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},
        shape = [n_samples, n_features]
        Training data

    y : array-like, shape = [n_samples] or [n_samples, n_targets]
        Target values

    alpha : {float, array-like},
        shape = [n_targets] if array-like
        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 ``C^-1`` in other linear models such as
        LogisticRegression or LinearSVC. If an array is passed, penalties are
        assumed to be specific to the targets. Hence they must correspond in
        number.

    sample_weight : float or numpy array of shape [n_samples]
        Individual weights for each sample. 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'}
        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. More stable for singular matrices than
          'cholesky'.

        - '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.


        All last five solvers support both dense and sparse data. However, only
        'sag' and 'saga' supports sparse input when`fit_intercept` is True.

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

    max_iter : int, optional
        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.

    tol : float
        Precision of the solution.

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

    random_state : int, RandomState instance or None, optional, default None
        The seed of the pseudo random number generator to use when shuffling
        the data.  If int, random_state is the seed used by the random number
        generator; If RandomState instance, random_state is the random number
        generator; If None, the random number generator is the RandomState
        instance used by `np.random`. Used when ``solver`` == 'sag'.

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

        .. versionadded:: 0.17

    return_intercept : boolean, 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

    Returns
    -------
    coef : array, 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 array, 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.
    t   sagRw   s   In Ridge, only 'sag' solver can currently fit the intercept when X is sparse. Solver has been automatically changed into 'sag'.t   sagat   accept_sparset   csrR   t   orderR9   t	   ensure_2dt   Ft   csct   cooi   s   Target y has the wrong shape %si   is:   Number of samples in X and y does not correspond: %d != %dt   choleskyt	   sparse_cgs)   Sample weights must be 1D array or scalarsE   Number of targets and number of penalties do not correspond: %d != %dRj   RB   s   Solver %s not understoodRF   t   squaredR:   i    t   is_sagas3   SVD solver does not support sparse inputs currentlyN(   Rx   Ry   (   Rx   Ry   (   R   R   Rj   RB   Rx   Ry   (   Rx   Ry   (+   R   t   issparseR,   R-   R$   t   float64t   float32R   RP   R   R   R!   t   ndimR*   t   strt   reshapeRJ   R+   RZ   R   t   asarrayRQ   Rm   t   repeatR<   RE   R	   RI   Rh   R   R`   RX   R
   t   maxR%   R@   Rl   t	   enumerateRO   t   intR   R)   Rv   (   R.   R/   R0   Rc   t   solverR1   R   R2   t   random_statet   return_n_itert   return_interceptt   _dtypeR3   R4   RQ   t
   n_samples_RR   Rd   RC   R:   Rb   Rf   t   max_squared_sumt	   interceptR6   t   alpha_iRV   t   initt   coef_t   n_iter_t   _(    (    s9   lib/python2.7/site-packages/sklearn/linear_model/ridge.pyt   ridge_regression   s    z!	$				!+&


t
   _BaseRidgec        
   B   s;   e  Z e d  e e e d d d d d   Z d d  Z RS(   g      ?gMbP?Rw   c	   	      C   sL   | |  _  | |  _ | |  _ | |  _ | |  _ | |  _ | |  _ | |  _ d  S(   N(   R0   t   fit_interceptt	   normalizet   copy_XR1   R   R   R   (	   t   selfR0   R   R   R   R1   R   R   R   (    (    s9   lib/python2.7/site-packages/sklearn/linear_model/ridge.pyt   __init__  s    							c         C   s  |  j  d k r t j } n t j t j g } t | | d d d g d | d t d t \ } } | d  k	 r t j |  j d	 k r t	 d
   n  |  j
 | | |  j |  j |  j d | \ } } } } } t j |  rV|  j rVt | | d |  j d | d |  j d |  j d |  j  d |  j d t d t \ |  _ |  _ |  _ |  j | 7_ nm t | | d |  j d | d |  j d |  j d |  j  d |  j d t d t \ |  _ |  _ |  j | | |  |  S(   NRx   Ry   R{   R   R   R   t   multi_outputt	   y_numerici   s)   Sample weights must be 1D array or scalarRc   R0   R1   R   R   R   R   R   (   Rx   Ry   (   R   R$   R   R   R   RJ   R+   RZ   R   R*   t   _preprocess_dataR   R   R   R   R   R   R0   R1   R   R   R   R   t
   intercept_RP   t   _set_intercept(   R   R.   R/   Rc   R   t   X_offsett   y_offsett   X_scale(    (    s9   lib/python2.7/site-packages/sklearn/linear_model/ridge.pyt   fit  s2    N(   t   __name__t
   __module__R   RJ   RP   R+   R   R   (    (    (    s9   lib/python2.7/site-packages/sklearn/linear_model/ridge.pyR     s
   	
t   Ridgec        	   B   s;   e  Z d  Z d e e e d d d d d  Z d d  Z RS(   s  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, array-like}, shape (n_targets)
        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 ``C^-1`` in other linear models such as
        LogisticRegression or LinearSVC. If an array is passed, penalties are
        assumed to be specific to the targets. Hence they must correspond in
        number.

    fit_intercept : boolean
        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).

    normalize : boolean, optional, default False
        This parameter is ignored when ``fit_intercept`` is set to False.
        If True, the regressors X will be normalized before regression by
        subtracting the mean and dividing by the l2-norm.
        If you wish to standardize, please use
        :class:`sklearn.preprocessing.StandardScaler` before calling ``fit``
        on an estimator with ``normalize=False``.

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

    max_iter : int, optional
        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.

    tol : float
        Precision of the solution.

    solver : {'auto', 'svd', 'cholesky', 'lsqr', 'sparse_cg', 'sag', 'saga'}
        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. More stable for singular matrices than
          'cholesky'.

        - '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.

        All last five solvers support both dense and sparse data. However,
        only 'sag' and 'saga' supports sparse input when `fit_intercept` is
        True.

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

    random_state : int, RandomState instance or None, optional, default None
        The seed of the pseudo random number generator to use when shuffling
        the data.  If int, random_state is the seed used by the random number
        generator; If RandomState instance, random_state is the random number
        generator; If None, the random number generator is the RandomState
        instance used by `np.random`. Used when ``solver`` == 'sag'.

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

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

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

    n_iter_ : array or None, 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

    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

    Examples
    --------
    >>> from sklearn.linear_model import Ridge
    >>> import numpy as np
    >>> n_samples, n_features = 10, 5
    >>> np.random.seed(0)
    >>> y = np.random.randn(n_samples)
    >>> X = np.random.randn(n_samples, n_features)
    >>> clf = Ridge(alpha=1.0)
    >>> clf.fit(X, y) # doctest: +NORMALIZE_WHITESPACE
    Ridge(alpha=1.0, copy_X=True, fit_intercept=True, max_iter=None,
          normalize=False, random_state=None, solver='auto', tol=0.001)

    g      ?gMbP?Rw   c	   	      C   sG   t  t |   j d | d | d | d | d | d | d | d |  d  S(	   NR0   R   R   R   R1   R   R   R   (   t   superR   R   (	   R   R0   R   R   R   R1   R   R   R   (    (    s9   lib/python2.7/site-packages/sklearn/linear_model/ridge.pyR     s    c         C   s   t  t |   j | | d | S(   s  Fit Ridge regression model

        Parameters
        ----------
        X : {array-like, sparse matrix}, shape = [n_samples, n_features]
            Training data

        y : array-like, shape = [n_samples] or [n_samples, n_targets]
            Target values

        sample_weight : float or numpy array of shape [n_samples]
            Individual weights for each sample

        Returns
        -------
        self : returns an instance of self.
        Rc   (   R   R   R   (   R   R.   R/   Rc   (    (    s9   lib/python2.7/site-packages/sklearn/linear_model/ridge.pyR     s    N(   R   R   t   __doc__RJ   RP   R+   R   R   (    (    (    s9   lib/python2.7/site-packages/sklearn/linear_model/ridge.pyR     s
   	t   RidgeClassifierc        
   B   sM   e  Z d  Z d e e e d d d d d d 	 Z d d  Z e d    Z	 RS(   se  Classifier using Ridge regression.

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

    Parameters
    ----------
    alpha : float
        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 ``C^-1`` in other linear models such as
        LogisticRegression or LinearSVC.

    fit_intercept : boolean
        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).

    normalize : boolean, optional, default False
        This parameter is ignored when ``fit_intercept`` is set to False.
        If True, the regressors X will be normalized before regression by
        subtracting the mean and dividing by the l2-norm.
        If you wish to standardize, please use
        :class:`sklearn.preprocessing.StandardScaler` before calling ``fit``
        on an estimator with ``normalize=False``.

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

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

    tol : float
        Precision of the solution.

    class_weight : dict or 'balanced', optional
        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'}
        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. More stable for singular matrices than
          'cholesky'.

        - '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.

    random_state : int, RandomState instance or None, optional, default None
        The seed of the pseudo random number generator to use when shuffling
        the data.  If int, random_state is the seed used by the random number
        generator; If RandomState instance, random_state is the random number
        generator; If None, the random number generator is the RandomState
        instance used by `np.random`. Used when ``solver`` == 'sag'.

    Attributes
    ----------
    coef_ : array, shape (n_features,) or (n_classes, n_features)
        Weight vector(s).

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

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

    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) # doctest: +ELLIPSIS
    0.9595...

    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.
    g      ?gMbP?Rw   c
   
      C   sP   t  t |   j d | d | d | d | d | d | d | d |	  | |  _ d  S(	   NR0   R   R   R   R1   R   R   R   (   R   R   R   t   class_weight(
   R   R0   R   R   R   R1   R   R   R   R   (    (    s9   lib/python2.7/site-packages/sklearn/linear_model/ridge.pyR   #  s
    c         C   s   t  | | d d d d g d t t d d d d	  |  _ |  j j |  } |  j j j d
  sv t | d t } n t d |  j	 j
   |  j r | d k r d } n  | t |  j |  } n  t t |   j | | d | |  S(   s  Fit Ridge regression model.

        Parameters
        ----------
        X : {array-like, sparse matrix}, shape = [n_samples,n_features]
            Training data

        y : array-like, shape = [n_samples]
            Target values

        sample_weight : float or numpy array of shape (n_samples,)
            Sample weight.

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

        Returns
        -------
        self : returns an instance of self.
        Rz   R{   R   R   R   t	   pos_labeli   t	   neg_labelit
   multilabelR-   s-   %s doesn't support multi-label classificationg      ?Rc   N(   R   RJ   R   t   _label_binarizert   fit_transformt   y_type_t
   startswithR   R*   t	   __class__R   R   R+   R   R   R   R   (   R   R.   R/   Rc   t   Y(    (    s9   lib/python2.7/site-packages/sklearn/linear_model/ridge.pyR   ,  s     		c         C   s
   |  j  j S(   N(   R   t   classes_(   R   (    (    s9   lib/python2.7/site-packages/sklearn/linear_model/ridge.pyR   X  s    N(
   R   R   R   RJ   RP   R+   R   R   t   propertyR   (    (    (    s9   lib/python2.7/site-packages/sklearn/linear_model/ridge.pyR     s   v	,t	   _RidgeGCVc           B   s   e  Z d  Z d e e d e d e d  Z e d  Z d   Z d   Z	 d   Z
 d	   Z d
   Z e d  Z d   Z d   Z d   Z d d  Z RS(   s,  Ridge regression with built-in Generalized Cross-Validation

    It allows efficient Leave-One-Out cross-validation.

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

    Notes
    -----

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

    Let G = (K + alpha*Id)^-1.

    Dual solution: c = Gy
    Primal solution: w = X^T c

    Compute eigendecomposition K = Q V Q^T.
    Then G = 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 = (KGY - diag(KG)Y) / diag(I-KG)

    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)

    References
    ----------
    http://cbcl.mit.edu/publications/ps/MIT-CSAIL-TR-2007-025.pdf
    https://www.mit.edu/~9.520/spring07/Classes/rlsslides.pdf
    g?g      ?g      $@c         C   sL   t  j |  |  _ | |  _ | |  _ | |  _ | |  _ | |  _ | |  _ d  S(   N(	   R$   R   t   alphasR   R   t   scoringR   t   gcv_modet   store_cv_values(   R   R   R   R   R   R   R   R   (    (    s9   lib/python2.7/site-packages/sklearn/linear_model/ridge.pyR     s    					c         C   sk   t  | | j d t } | r4 | t j |  7} n  t j |  \ } } t j | j |  } | | | f S(   NRF   (   R	   RI   RJ   R$   t	   ones_likeR   t   eighRk   (   R   R.   R/   t   centered_kernelRb   t   vt   Qt   QT_y(    (    s9   lib/python2.7/site-packages/sklearn/linear_model/ridge.pyt   _pre_compute  s    c         C   s   | | d j  d d  S(   Ni   t   axisi(   t   sum(   R   t   v_primeR   (    (    s9   lib/python2.7/site-packages/sklearn/linear_model/ridge.pyt   _decomp_diag  s    c         C   sN   t  | j  d k rF | t d   f t j f t  | j  d } n  | | S(   Ni   (   RL   R!   t   sliceR+   R$   R^   (   R   t   Dt   B(    (    s9   lib/python2.7/site-packages/sklearn/linear_model/ridge.pyt	   _diag_dot  s    1c   
      C   s   d | | } t  j | d  d k  } d | | <t  j | |  j | |   } |  j | |  }	 t | j  d k r |	 d d  t  j f }	 n  |	 | f S(   s   Helper function to avoid code duplication between self._errors and
        self._values.

        Notes
        -----
        We don't construct matrix G, instead compute action on y & diagonal.
        g      ?i    g-q=i   N(   R$   t   varRk   R   R   RL   R!   R^   (
   R   R0   R/   R   R   R   t   wt   constant_columnt   ct   G_diag(    (    s9   lib/python2.7/site-packages/sklearn/linear_model/ridge.pyt   _errors_and_values_helper  s    
c         C   s3   |  j  | | | | |  \ } } | | d | f S(   Ni   (   R   (   R   R0   R/   R   R   R   R   R   (    (    s9   lib/python2.7/site-packages/sklearn/linear_model/ridge.pyt   _errors  s    !c         C   s3   |  j  | | | | |  \ } } | | | | f S(   N(   R   (   R   R0   R/   R   R   R   R   R   (    (    s9   lib/python2.7/site-packages/sklearn/linear_model/ridge.pyt   _values  s    !c   	      C   s   t  j |  r t d   n  | rR t j | t j | j d d f  f  } n  t j | d d \ } } } | d } t j	 | j
 |  } | | | f S(   Ns%   SVD not supported for sparse matricesi    i   Ri   i   (   R   R   R)   R$   t   hstackt   onesR!   R   Rj   Rk   RI   (	   R   R.   R/   R   Rn   Ro   R   R   t   UT_y(    (    s9   lib/python2.7/site-packages/sklearn/linear_model/ridge.pyt   _pre_compute_svd  s    .
c   
      C   s   t  j | d  d k  } | | d | d } | d | | <t  j | |  j | |   | d | } |  j | |  | d }	 t | j  d k r |	 d d  t  j f }	 n  |	 | f S(   si   Helper function to avoid code duplication between self._errors_svd
        and self._values_svd.
        i    g-q=ii   N(   R$   R   Rk   R   R   RL   R!   R^   (
   R   R0   R/   R   Rn   R   R   R   R   R   (    (    s9   lib/python2.7/site-packages/sklearn/linear_model/ridge.pyt   _errors_and_values_svd_helper  s    *c         C   s3   |  j  | | | | |  \ } } | | d | f S(   Ni   (   R   (   R   R0   R/   R   Rn   R   R   R   (    (    s9   lib/python2.7/site-packages/sklearn/linear_model/ridge.pyt   _errors_svd  s    !c         C   s3   |  j  | | | | |  \ } } | | | | f S(   N(   R   (   R   R0   R/   R   Rn   R   R   R   (    (    s9   lib/python2.7/site-packages/sklearn/linear_model/ridge.pyt   _values_svd  s    !c      
   C   s  t  | | d d d g d t j d t d t \ } } | d k	 rg t | t  rg t | d t } n  | j	 \ } } t
 j | | |  j |  j |  j d | \ } } } } } |  j }	 t t j	 |   }
 |	 d k s |	 d	 k rt j |  s| | k s|
 rd
 }	 q@d }	 n( |	 d k r@|
 r@t j d  d
 }	 n  |	 d
 k rj|  j } |  j } |  j } n: |	 d k r|  j } |  j } |  j } n t d |	   | d k	 rt | | |  \ } } n  t j |  o|  j } | | | |  \ } } } t | j	  d k rd n
 | j	 d } t j | | t |  j  f  } g  } t |  d |  j  d t } | d k } t j! |  j d k   rt d j" |  j    n  x t# |  j  D] \ } } | r| t |  | | | |  \ } } n$ | t |  | | | |  \ } } | j$   | d d  | f <| j% |  qW| rd| j& d d  j'   } nz d   } d   | _( d   | _) g  t* t |  j   D]. } | | | j$   | d d  | f  ^ q} t j+ |  } |  j | |  _, | | |  _- t. |  j- j/ |  |  _0 |  j1 | | |  |  j2 rt | j	  d k r\| t |  j  f } n | | t |  j  f } | j3 |  |  _4 n  |  S(   s  Fit Ridge regression model

        Parameters
        ----------
        X : {array-like, sparse matrix}, shape = [n_samples, n_features]
            Training data

        y : array-like, shape = [n_samples] or [n_samples, n_targets]
            Target values. Will be cast to X's dtype if necessary

        sample_weight : float or array-like of shape [n_samples]
            Sample weight

        Returns
        -------
        self : object
        R{   R   R   R   R   R   R}   Rc   Rw   t   eigenRj   sF   non-uniform sample weights unsupported for svd, forcing usage of eigens   bad gcv_mode "%s"i   R   t
   allow_nonei    sI   alphas cannot be negative. Got {} containing some negative value instead.NR   c           S   s   d  S(   N(    (    (    (    s9   lib/python2.7/site-packages/sklearn/linear_model/ridge.pyt   identity_estimator=  s    c         S   s   |  S(   N(    (   t	   y_predict(    (    s9   lib/python2.7/site-packages/sklearn/linear_model/ridge.pyt   <lambda>?  s    c         S   s   |  S(   N(    (   R   (    (    s9   lib/python2.7/site-packages/sklearn/linear_model/ridge.pyR   @  s    (5   R   R$   R   RJ   R+   R\   t   floatR   RP   R!   R   R   R   R   R   R   RL   R   R   R,   R-   R   R   R   R   R   R   R*   R   Rl   R   R   R   t   anyt   formatR   RQ   t   appendt   meant   argmint   decision_functiont   predictR&   t   argmaxt   alpha_t
   dual_coef_R	   RI   R   R   R   R   t
   cv_values_(   R   R.   R/   Rc   R3   R4   R   R   R   R   t   with_swR   R   R   R   R   R   R   t   n_yt	   cv_valuesR9   t   scorert   errorR6   R0   t   outR   t   bestR   t   cv_values_shape(    (    s9   lib/python2.7/site-packages/sklearn/linear_model/ridge.pyR     s|    !	!							("	'$	G	(   g?g      ?g      $@N(   R   R   R   RJ   RP   R+   R   R   R   R   R   R   R   R   R   R   R   R   (    (    (    s9   lib/python2.7/site-packages/sklearn/linear_model/ridge.pyR   ]  s    %									t   _BaseRidgeCVc           B   s2   e  Z d e e d d d e d  Z d d  Z RS(   g?g      ?g      $@c         C   sL   t  j |  |  _ | |  _ | |  _ | |  _ | |  _ | |  _ | |  _ d  S(   N(	   R$   R   R   R   R   R   t   cvR   R   (   R   R   R   R   R   R   R   R   (    (    s9   lib/python2.7/site-packages/sklearn/linear_model/ridge.pyR   W  s    					c         C   s0  |  j  d
 k r t |  j d |  j d |  j d |  j d |  j d |  j } | j	 | | d | | j
 |  _
 |  j r| j |  _ qn |  j r t d   n  i |  j d 6} t t d |  j d |  j  | d	 |  j  d |  j } | j	 | | d | | j } | j j |  _
 | j |  _ | j |  _ |  S(   s  Fit Ridge regression model

        Parameters
        ----------
        X : array-like, shape = [n_samples, n_features]
            Training data

        y : array-like, shape = [n_samples] or [n_samples, n_targets]
            Target values. Will be cast to X's dtype if necessary

        sample_weight : float or array-like of shape [n_samples]
            Sample weight

        Returns
        -------
        self : object
        R   R   R   R   R   Rc   s3   cv!=None and store_cv_values=True  are incompatibleR0   R   N(   R   R+   R   R   R   R   R   R   R   R   R   R   R*   R   R   t   best_estimator_R0   R   R   (   R   R.   R/   Rc   t	   estimatort
   parameterst   gs(    (    s9   lib/python2.7/site-packages/sklearn/linear_model/ridge.pyR   c  s.    								(   g?g      ?g      $@N(   R   R   RJ   RP   R+   R   R   (    (    (    s9   lib/python2.7/site-packages/sklearn/linear_model/ridge.pyR   V  s
   		t   RidgeCVc           B   s   e  Z d  Z RS(   s  Ridge regression with built-in cross-validation.

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

    By default, it performs Generalized Cross-Validation, which is a form of
    efficient Leave-One-Out cross-validation.

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

    Parameters
    ----------
    alphas : numpy array of shape [n_alphas]
        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 ``C^-1`` in other linear models such as
        LogisticRegression or LinearSVC.

    fit_intercept : boolean
        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).

    normalize : boolean, optional, default False
        This parameter is ignored when ``fit_intercept`` is set to False.
        If True, the regressors X will be normalized before regression by
        subtracting the mean and dividing by the l2-norm.
        If you wish to standardize, please use
        :class:`sklearn.preprocessing.StandardScaler` before calling ``fit``
        on an estimator with ``normalize=False``.

    scoring : string, callable or None, optional, default: None
        A string (see model evaluation documentation) or
        a scorer callable object / function with signature
        ``scorer(estimator, X, y)``.

    cv : int, cross-validation generator or an iterable, optional
        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 : {None, 'auto', 'svd', eigen'}, optional
        Flag indicating which strategy to use when performing
        Generalized Cross-Validation. Options are::

            'auto' : use svd if n_samples > n_features or when X is a sparse
                     matrix, otherwise use eigen
            'svd' : force computation via singular value decomposition of X
                    (does not work for sparse matrices)
            'eigen' : force computation via eigendecomposition of X^T X

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

    store_cv_values : boolean, default=False
        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
        Generalized Cross-Validation).

    Attributes
    ----------
    cv_values_ : array, shape = [n_samples, n_alphas] or         shape = [n_samples, n_targets, n_alphas], optional
        Cross-validation values for each alpha (if ``store_cv_values=True``        and ``cv=None``). After ``fit()`` has been called, this attribute         will contain the mean squared errors (by default) or the values         of the ``{loss,score}_func`` function (if provided in the constructor).

    coef_ : array, shape = [n_features] or [n_targets, n_features]
        Weight vector(s).

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

    alpha_ : float
        Estimated regularization parameter.

    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) # doctest: +ELLIPSIS
    0.5166...

    See also
    --------
    Ridge : Ridge regression
    RidgeClassifier : Ridge classifier
    RidgeClassifierCV : Ridge classifier with built-in cross validation
    (   R   R   R   (    (    (    s9   lib/python2.7/site-packages/sklearn/linear_model/ridge.pyR    s   kt   RidgeClassifierCVc           B   sG   e  Z d  Z d e e d d d e d  Z d d  Z e d    Z	 RS(	   s  Ridge classifier with built-in cross-validation.

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

    By default, it performs Generalized Cross-Validation, which is a form of
    efficient 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 : numpy array of shape [n_alphas]
        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 ``C^-1`` in other linear models such as
        LogisticRegression or LinearSVC.

    fit_intercept : boolean
        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).

    normalize : boolean, optional, default False
        This parameter is ignored when ``fit_intercept`` is set to False.
        If True, the regressors X will be normalized before regression by
        subtracting the mean and dividing by the l2-norm.
        If you wish to standardize, please use
        :class:`sklearn.preprocessing.StandardScaler` before calling ``fit``
        on an estimator with ``normalize=False``.

    scoring : string, callable or None, optional, default: None
        A string (see model evaluation documentation) or
        a scorer callable object / function with signature
        ``scorer(estimator, X, y)``.

    cv : int, cross-validation generator or an iterable, optional
        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', optional
        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_values : boolean, default=False
        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
        Generalized Cross-Validation).

    Attributes
    ----------
    cv_values_ : array, shape = [n_samples, n_targets, n_alphas], optional
        Cross-validation values for each alpha (if ``store_cv_values=True`` and
        ``cv=None``). After ``fit()`` has been called, this attribute will
        contain the mean squared errors (by default) or the values of the
        ``{loss,score}_func`` function (if provided in the constructor).

    coef_ : array, shape = [n_features] or [n_targets, n_features]
        Weight vector(s).

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

    alpha_ : float
        Estimated regularization parameter

    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) # doctest: +ELLIPSIS
    0.9630...

    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.
    g?g      ?g      $@c         C   sD   t  t |   j d | d | d | d | d | d |  | |  _ d  S(   NR   R   R   R   R   R   (   R   R  R   R   (   R   R   R   R   R   R   R   R   (    (    s9   lib/python2.7/site-packages/sklearn/linear_model/ridge.pyR   j  s    c         C   s   t  | | d d d d g d t t d d d d	  |  _ |  j j |  } |  j j j d
  sv t | d t } n  |  j r | d k r d } n  | t
 |  j |  } n  t j |  | | d | |  S(   s   Fit the ridge classifier.

        Parameters
        ----------
        X : array-like, shape (n_samples, n_features)
            Training vectors, where n_samples is the number of samples
            and n_features is the number of features.

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

        sample_weight : float or numpy array of shape (n_samples,)
            Sample weight.

        Returns
        -------
        self : object
        Rz   R{   R   R   R   R   i   R   iR   R-   g      ?Rc   N(   R   RJ   R   R   R   R   R   R   R   R+   R   R   R   (   R   R.   R/   Rc   R   (    (    s9   lib/python2.7/site-packages/sklearn/linear_model/ridge.pyR   r  s    		c         C   s
   |  j  j S(   N(   R   R   (   R   (    (    s9   lib/python2.7/site-packages/sklearn/linear_model/ridge.pyR     s    (   g?g      ?g      $@N(
   R   R   R   RJ   RP   R+   R   R   R   R   (    (    (    s9   lib/python2.7/site-packages/sklearn/linear_model/ridge.pyR    s   g%(6   R   t   abcR    R   R,   t   numpyR$   t   scipyR   R   t   scipy.sparseR"   t   baseR   R   R   Rx   R   R   t   utils.extmathR	   R
   t   utilsR   R   R   R   R   t   preprocessingR   t   model_selectionR   t	   externalsR   t   metrics.scorerR   t
   exceptionsR   R+   R<   RE   RX   RP   Rh   Rv   R   t   with_metaclassR   R   R   R   R   R  R  (    (    (    s9   lib/python2.7/site-packages/sklearn/linear_model/ridge.pyt   <module>   sJ   	9	=	 "5<o