
VE                 @   s  d  Z  d d l m Z d d l m Z m Z d d l Z d d l Z d d l Z	 d d l
 j Z d d l m Z d d l m Z d d l m Z d d	 l m Z m Z d d
 l m Z m Z m Z d d l m Z m Z m Z m Z d d l 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' m( Z( d d l) m* Z* m+ Z+ d Z, d d d  Z- d d d  Z. d d d d d  Z/ d d   Z0 Gd d   d e j1 e e   Z2 Gd d    d  e  Z3 Gd! d"   d" e4  Z5 Gd# d$   d$ e2 e  Z6 d% d&   Z7 d S)'z
Generalized Linear models.
    )division)ABCMetaabstractmethodN)linalg)sparse   )six)Paralleldelayed)BaseEstimatorClassifierMixinRegressorMixin)as_float_arraycheck_array	check_X_y
deprecated)check_random_statecolumn_or_1d)safe_sparse_dot)mean_variance_axisinplace_column_scale)sparse_lsqr)NotFittedErrorcheck_is_fitted)ArrayDataset
CSRDatasetg{Gz?c             C   s   t  |  } | j d t j t j  j  } t j |   rl t |  j	 |  j
 |  j | | d | } t } n t |  | | d | } d } | | f S)zCreate ``Dataset`` abstraction for sparse and dense inputs.

    This also returns the ``intercept_decay`` which is different
    for sparse datasets.
       seedg      ?)r   randintnpiinfoint32maxspissparser   dataZindptrindicesSPARSE_INTERCEPT_DECAYr   )Xysample_weightZrandom_staterngr   ZdatasetZintercept_decay r,   T/s/python-3.5.2/amd64_ubu14/lib/python3.5/site-packages/sklearn/linear_model/base.pymake_dataset3   s    !	r.   Fc             C   sv  | rt  j |   rH |  j   d k rH t  j |  d | d t j }  n t  j |  d | d t j }  t |  d d \ } } | r | |  j d 9} t j	 | |  } ~ d | | d k <t
 |  d |  n t j |  j d  } | j d d  } | | } n` t j |  j d  } t j |  j d  } | j d k rDd n t j | j d d |  j } |  | | | | f S)	z
    Compute information needed to center data to have mean zero along
    axis 0. Be aware that X will not be centered since it would break
    the sparsity, but will be normalized if asked so.
    csrcopydtypeaxisr   r   g      ?g        )r#   
isspmatrixZ	getformat
csr_matrixr   float64Z
csc_matrixr   shapesqrtr   onesmeanzerosndimr1   )r(   r)   fit_intercept	normalizeX_meanZX_varX_stdy_meanr,   r,   r-   sparse_center_dataI   s$    !!4rA   Tc       	      C   s  t  |  |  }  | rt | t j  r- d } t j |   rk t j |  j d  } t j	 |  j d  } n t j
 |  d d d | } |  | 8}  | r t j t j |  d d d  } d | | d k <|  | }  n t j	 |  j d  } t j
 | d d d | } | | } n` t j |  j d  } t j	 |  j d  } | j d k rTd n t j | j d d |  j } |  | | | | f S)	z
    Centers data to have mean zero along axis 0. This is here because
    nearly all linear models will want their data to be centered.

    If sample_weight is not None, then the weighted mean of X and y
    is zero, and not the mean itself
    Nr   r2   r   weightsr   g        r1   )r   
isinstancenumbersNumberr#   r$   r   r:   r6   r8   averager7   sumr;   r1   )	r(   r)   r<   r=   r0   r*   r>   r?   r@   r,   r,   r-   center_datan   s(    	
"4rH   c             C   sx   |  j  d } | t j |  } t j |  } t j | d f d | | f } t | |   }  t | |  } |  | f S)z+Rescale data so as to support sample_weightr   r6   )r6   r   r8   r7   r   Z
dia_matrixr   )r(   r)   r*   	n_samplesZ	sw_matrixr,   r,   r-   _rescale_data   s    rJ   c               @   sp   e  Z d  Z d Z e d d    Z e d  d d    Z d d   Z d	 d
   Z	 e
 e  Z d d   Z d S)LinearModelzBase class for Linear Modelsc             C   s   d S)z
Fit model.Nr,   )selfr(   r)   r,   r,   r-   fit   s    zLinearModel.fitz and will be removed in 0.19.c             C   s   |  j  |  S)a#  Decision function of the linear model.

        Parameters
        ----------
        X : {array-like, sparse matrix}, shape = (n_samples, n_features)
            Samples.

        Returns
        -------
        C : array, shape = (n_samples,)
            Returns predicted values.
        )_decision_function)rL   r(   r,   r,   r-   decision_function   s    zLinearModel.decision_functionc             C   sH   t  |  d  t | d d d d g } t | |  j j d d |  j S)Ncoef_accept_sparser/   csccoodense_outputT)r   r   r   rP   T
intercept_)rL   r(   r,   r,   r-   rN      s    zLinearModel._decision_functionc             C   s   |  j  |  S)a  Predict using the linear model

        Parameters
        ----------
        X : {array-like, sparse matrix}, shape = (n_samples, n_features)
            Samples.

        Returns
        -------
        C : array, shape = (n_samples,)
            Returns predicted values.
        )rN   )rL   r(   r,   r,   r-   predict   s    zLinearModel.predictc             C   sH   |  j  r; |  j | |  _ | t j | |  j j  |  _ n	 d |  _ d S)zSet the intercept_
        g        N)r<   rP   r   dotrU   rV   )rL   r>   r@   r?   r,   r,   r-   _set_intercept   s    	"zLinearModel._set_interceptN)__name__
__module____qualname____doc__r   rM   r   rO   rN   rW   staticmethodrH   _center_datarY   r,   r,   r,   r-   rK      s   rK   c               @   s:   e  Z d  Z d Z d d   Z d d   Z d d   Z d S)	LinearClassifierMixinzRMixin for linear classifiers.

    Handles prediction for sparse and dense X.
    c             C   s   t  |  d  s |  j d k r> t d d t |   j i   t | d d } |  j j d } | j d | k r t d | j d | f   t | |  j j	 d	 d
 |  j
 } | j d d k r | j   S| S)aP  Predict confidence scores for samples.

        The confidence score for a sample is the signed distance of that
        sample to the hyperplane.

        Parameters
        ----------
        X : {array-like, sparse matrix}, shape = (n_samples, n_features)
            Samples.

        Returns
        -------
        array, shape=(n_samples,) if n_classes == 2 else (n_samples, n_classes)
            Confidence scores per (sample, class) combination. In the binary
            case, confidence score for self.classes_[1] where >0 means this
            class would be predicted.
        rP   Nz(This %(name)s instance is not fitted yetnamerQ   r/   r   z*X has %d features per sample; expecting %drT   T)hasattrrP   r   typerZ   r   r6   
ValueErrorr   rU   rV   ravel)rL   r(   
n_featuresscoresr,   r,   r-   rO      s    z'LinearClassifierMixin.decision_functionc             C   s\   |  j  |  } t | j  d k r? | d k j t j  } n | j d d  } |  j | S)a*  Predict class labels for samples in X.

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

        Returns
        -------
        C : array, shape = [n_samples]
            Predicted class label per sample.
        r   r   r2   )rO   lenr6   astyper   intargmaxZclasses_)rL   r(   rg   r&   r,   r,   r-   rW      s
    zLinearClassifierMixin.predictc             C   s   |  j  |  } | d 9} t j | |  | d 7} t j | |  | j d k rl t j d | | g  j S| | j d d  j | j	 d d f  } | Sd S)zProbability estimation for OvR logistic regression.

        Positive class probabilities are computed as
        1. / (1. + np.exp(-self.decision_function(X)));
        multiclass is handled by normalizing that over all classes.
        r   r2   r   Nrl   )
rO   r   exp
reciprocalr;   vstackrU   rG   reshaper6   )rL   r(   Zprobr,   r,   r-   _predict_proba_lr  s    

,z'LinearClassifierMixin._predict_proba_lrN)rZ   r[   r\   r]   rO   rW   rq   r,   r,   r,   r-   r`      s   !r`   c               @   s.   e  Z d  Z d Z d d   Z d d   Z d S)SparseCoefMixinzlMixin for converting coef_ to and from CSR format.

    L1-regularizing estimators should inherit this.
    c             C   sA   d } t  |  d d | t j |  j  r= |  j j   |  _ |  S)a  Convert coefficient matrix to dense array format.

        Converts the ``coef_`` member (back) to a numpy.ndarray. This is the
        default format of ``coef_`` and is required for fitting, so calling
        this method is only required on models that have previously been
        sparsified; otherwise, it is a no-op.

        Returns
        -------
        self: estimator
        z6Estimator, %(name)s, must be fitted before densifying.rP   msg)r   r#   r$   rP   Ztoarray)rL   rs   r,   r,   r-   densify-  s
    zSparseCoefMixin.densifyc             C   s2   d } t  |  d d | t j |  j  |  _ |  S)au  Convert coefficient matrix to sparse format.

        Converts the ``coef_`` member to a scipy.sparse matrix, which for
        L1-regularized models can be much more memory- and storage-efficient
        than the usual numpy.ndarray representation.

        The ``intercept_`` member is not converted.

        Notes
        -----
        For non-sparse models, i.e. when there are not many zeros in ``coef_``,
        this may actually *increase* memory usage, so use this method with
        care. A rule of thumb is that the number of zero elements, which can
        be computed with ``(coef_ == 0).sum()``, must be more than 50% for this
        to provide significant benefits.

        After calling this method, further fitting with the partial_fit
        method (if any) will not work until you call densify.

        Returns
        -------
        self: estimator
        z7Estimator, %(name)s, must be fitted before sparsifying.rP   rs   )r   r#   r4   rP   )rL   rs   r,   r,   r-   sparsify?  s    zSparseCoefMixin.sparsifyN)rZ   r[   r\   r]   rt   ru   r,   r,   r,   r-   rr   '  s   rr   c               @   s[   e  Z d  Z d Z d d d d d d  Z e e d  d d	     Z d
 d d  Z d
 S)LinearRegressionaJ  
    Ordinary least squares Linear Regression.

    Parameters
    ----------
    fit_intercept : boolean, optional
        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
        If True, the regressors X will be normalized before regression.

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

    n_jobs : int, optional, default 1
        The number of jobs to use for the computation.
        If -1 all CPUs are used. This will only provide speedup for
        n_targets > 1 and sufficient large problems.

    Attributes
    ----------
    coef_ : array, shape (n_features, ) or (n_targets, n_features)
        Estimated coefficients for the linear regression problem.
        If multiple targets are passed during the fit (y 2D), this
        is a 2D array of shape (n_targets, n_features), while if only
        one target is passed, this is a 1D array of length n_features.

    intercept_ : array
        Independent term in the linear model.

    Notes
    -----
    From the implementation point of view, this is just plain Ordinary
    Least Squares (scipy.linalg.lstsq) wrapped as a predictor object.

    TFr   c             C   s(   | |  _  | |  _ | |  _ | |  _ d  S)N)r<   r=   copy_Xn_jobs)rL   r<   r=   rw   rx   r,   r,   r-   __init__  s    			zLinearRegression.__init__z7``residues_`` is deprecated and will be removed in 0.19c             C   s   |  j  S)z%Get the residues of the fitted model.)	_residues)rL   r,   r,   r-   	residues_  s    zLinearRegression.residues_Nc       
   	      s  |  j  } t    d d d d g d d d d \    | d k	 ro t j |  j d	 k ro t | d
 d } |  j    |  j |  j |  j	 d | \    } } } | d k	 r t
    |  \    t j    r j d k  rt     } | d |  _ | d |  _ qt d |     f d d   t  j d	  D  }	 t j d d   |	 D  |  _ t j d d   |	 D  |  _ n9 t j     \ |  _ |  _ |  _ |  _ |  j j |  _  j d	 k rt j |  j  |  _ |  j | | |  |  S)a   
        Fit linear model.

        Parameters
        ----------
        X : numpy array or sparse matrix of shape [n_samples,n_features]
            Training data

        y : numpy array of shape [n_samples, n_targets]
            Target values

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

            .. versionadded:: 0.17
               parameter *sample_weight* support to LinearRegression.

        Returns
        -------
        self : returns an instance of self.
        rQ   r/   rR   rS   Z	y_numericTZmulti_outputNr   warnr*   r   r      rx   c             3   s:   |  ]0 } t  t     d  d   | f j    Vq d  S)N)r
   r   re   ).0j)r(   r)   r,   r-   	<genexpr>  s   z'LinearRegression.fit.<locals>.<genexpr>c             s   s   |  ] } | d  Vq d S)r   Nr,   )r~   outr,   r,   r-   r     s    c             s   s   |  ] } | d  Vq d S)r}   Nr,   )r~   r   r,   r,   r-   r     s    )rx   r   r   
atleast_1dr;   r   r_   r<   r=   rw   rJ   r#   r$   r   rP   rz   r	   ranger6   ro   r   lstsqZrank_Z	singular_rU   re   rY   )
rL   r(   r)   r*   Zn_jobs_r>   r@   r?   r   Zoutsr,   )r(   r)   r-   rM     s4    	$"*zLinearRegression.fit)	rZ   r[   r\   r]   ry   propertyr   r{   rM   r,   r,   r,   r-   rv   ]  s   &	rv   c             C   sW  |  j  \ } } t j |   rK d } t |  | | |  \ }  } }	 }
 } n* t |  | | | d | \ }  } }	 }
 } t | d  r | r t j |	 t j |   s | r t j | t j	 |   r t
 j d t  d } d } t | t j  r| d k r| | k } | d k rZt j d | | f d	 |  j d
 d  } t j |  j |  d | t | d  sod } t | d  r>| d k r>t j |  j | j g g   } | j d k rt j d | d	 | d
 d  } t j |  j | d | nM | j  d } t j d | | f d	 | d
 d  } t j | j |  d | j |  | |	 |
 | | | f S)z6Aux function used at beginning of fit in linear modelsFr0   	__array__zlGram matrix was provided but X was centered to fit intercept, or X was normalized : recomputing Gram matrix.autoNTr6   r1   orderCr   r   F)r6   r   r3   rA   rH   rb   r   allcloser:   r8   warningsr|   UserWarningrC   r   string_typesemptyr1   rX   rU   find_common_typer;   )r(   r)   ZXyZ
precomputer=   r<   r0   rI   rf   r>   r@   r?   Zcommon_dtype	n_targetsr,   r,   r-   _pre_fit  s@    $'""			r   )8r]   
__future__r   abcr   r   rD   r   numpyr   Zscipy.sparser   r#   scipyr   	externalsr   Zexternals.joblibr	   r
   baser   r   r   utilsr   r   r   r   r   r   Zutils.extmathr   Zutils.sparsefuncsr   r   Zutils.fixesr   Zutils.validationr   r   Zutils.seq_datasetr   r   r'   r.   rA   rH   rJ   with_metaclassrK   r`   objectrr   rv   r   r,   r,   r,   r-   <module>   s:   "%"";O6r