B
    	\L                 @   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
mZ ddlmZ ddlmZ ddlmZ dd	lmZmZ dd
lmZmZmZ ddlmZmZ ddlmZ ddlmZ ddlmZ ddl m!Z!m"Z" dd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- dZ.d(ddZ/d)ddZ0dd Z1G dd de2eeZ3G d d! d!eZ4G d"d# d#e5Z6G d$d% d%e3eZ7d*d&d'Z8dS )+z
Generalized Linear models.
    )division)ABCMetaabstractmethodN)linalg)sparse   )six)Paralleldelayed)BaseEstimatorClassifierMixinRegressorMixin)check_array	check_X_y)FLOAT_DTYPES)check_random_state)safe_sparse_dot)mean_variance_axisinplace_column_scale)sparse_lsqr)ArrayDataset
CSRDataset)check_is_fitted)NotFittedError)	normalizeg{Gz?c             C   sd   t |}|dttjj}t| rHt| j	| j
| j|||d}t}nt| |||d}d}||fS )a  Create ``Dataset`` abstraction for sparse and dense inputs.

    This also returns the ``intercept_decay`` which is different
    for sparse datasets.

    Parameters
    ----------
    X : array_like, shape (n_samples, n_features)
        Training data

    y : array_like, shape (n_samples, )
        Target values.

    sample_weight : numpy array of shape (n_samples,)
        The weight of each sample

    random_state : int, RandomState instance or None (default)
        Determines random number generation for dataset shuffling and noise.
        Pass an int for reproducible output across multiple function calls.
        See :term:`Glossary <random_state>`.

    Returns
    -------
    dataset
        The ``Dataset`` abstraction
    intercept_decay
        The intercept decay
       )seedg      ?)r   ZrandintnpZiinfoZint32maxspissparser   dataZindptrindicesSPARSE_INTERCEPT_DECAYr   )Xysample_weightZrandom_staterngr   ZdatasetZintercept_decay r(   8lib/python3.7/site-packages/sklearn/linear_model/base.pymake_dataset0   s    
r*   FTc             C   s  t |tjrd}|r*t| |ddgtd} n$|rNt| rB|  } n| jdd} tj	|| j
d}|rNt| rt| dd	\}}	|s| j
d|dd< |r|	| jd 9 }	t|	|	}
~	d
|
|
dk< t| d|
  ntj| jd
 | j
d}
nJtj| d|d}| |8 } |rt| dddd\} }
ntj| jd
 | j
d}
tj|d|d}|| }n\tj| jd
 | j
d}tj| jd
 | j
d}
|jd
kr| j
d}ntj|jd
 | j
d}| ||||
fS )aS  
    Centers data to have mean zero along axis 0. If fit_intercept=False or if
    the X is a sparse matrix, no centering is done, but normalization can still
    be applied. The function returns the statistics necessary to reconstruct
    the input data, which are X_offset, y_offset, X_scale, such that the output

        X = (X - X_offset) / X_scale

    X_scale is the L2 norm of X - X_offset. If sample_weight is not None,
    then the weighted mean of X and y is zero, and not the mean itself. If
    return_mean=True, the mean, eventually weighted, is returned, independently
    of whether X was centered (option used for optimization with sparse data in
    coordinate_descend).

    This is here because nearly all linear models will want their data to be
    centered. This function also systematically makes y consistent with X.dtype
    Ncsrcsc)copyaccept_sparsedtypeK)order)r/   r   )axisr   g      ?)r2   ZweightsFT)r2   r-   Zreturn_norm)
isinstancenumbersNumberr   r   r   r    r-   r   Zasarrayr/   r   typeshapesqrtr   onesZaveragef_normalizezerosndim)r$   r%   fit_interceptr   r-   r&   return_meancheck_inputX_offsetZX_varX_scaley_offsetr(   r(   r)   _preprocess_data]   sH    




rC   c             C   s^   | j d }tj||t|jd}t|}tj|df||fd}t|| } t||}| |fS )z+Rescale data so as to support sample_weightr   )r/   )r7   )	r7   r   ZfullZarrayr/   r8   r   Z
dia_matrixr   )r$   r%   r&   	n_samplesZ	sw_matrixr(   r(   r)   _rescale_data   s    




rE   c               @   s<   e Zd ZdZe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.fitc             C   s4   t | d t|dddgd}t|| jjdd| j S )Ncoef_r+   r,   coo)r.   T)dense_output)r   r   r   rI   T
intercept_)rG   r$   r(   r(   r)   _decision_function   s    

zLinearModel._decision_functionc             C   s
   |  |S )a  Predict using the linear model

        Parameters
        ----------
        X : array_like or sparse matrix, shape (n_samples, n_features)
            Samples.

        Returns
        -------
        C : array, shape (n_samples,)
            Returns predicted values.
        )rN   )rG   r$   r(   r(   r)   predict   s    zLinearModel.predictc             C   s4   | j r*| j| | _|t|| jj | _nd| _dS )zSet the intercept_
        g        N)r=   rI   r   dotrL   rM   )rG   r@   rB   rA   r(   r(   r)   _set_intercept   s    zLinearModel._set_interceptN)__name__
__module____qualname____doc__r   rH   rN   rO   staticmethodrC   rQ   r(   r(   r(   r)   rF      s   rF   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r| jdkr*tddt| ji t|dd}| jjd }|jd |krftd|jd |f t|| jj	d	d
| j
 }|jd dkr| S |S )aN  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 or 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.
        rI   Nz(This %(name)s instance is not fitted yetnamer+   )r.   r   z*X has %d features per sample; expecting %dT)rK   )hasattrrI   r   r6   rR   r   r7   
ValueErrorr   rL   rM   ravel)rG   r$   
n_featuresscoresr(   r(   r)   decision_function   s    
z'LinearClassifierMixin.decision_functionc             C   s@   |  |}t|jdkr*|dktj}n|jdd}| j| S )a&  Predict class labels for samples in X.

        Parameters
        ----------
        X : array_like or sparse matrix, shape (n_samples, n_features)
            Samples.

        Returns
        -------
        C : array, shape [n_samples]
            Predicted class label per sample.
        r   r   )r2   )r^   lenr7   Zastyper   intZargmaxZclasses_)rG   r$   r]   r"   r(   r(   r)   rO     s
    
zLinearClassifierMixin.predictc             C   sx   |  |}|d9 }t|| |d7 }t|| |jdkrPtd| |gjS ||jdd|j	d df }|S d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   N)
r^   r   ZexpZ
reciprocalr<   vstackrL   sumZreshaper7   )rG   r$   Zprobr(   r(   r)   _predict_proba_lr   s    

 z'LinearClassifierMixin._predict_proba_lrN)rR   rS   rT   rU   r^   rO   rd   r(   r(   r(   r)   rW      s   !rW   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   s.   d}t | d|d t| jr*| 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.rI   )msg)r   r   r    rI   Ztoarray)rG   rf   r(   r(   r)   densify:  s
    zSparseCoefMixin.densifyc             C   s$   d}t | d|d t| j| _| S )av  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.rI   )rf   )r   r   Z
csr_matrixrI   )rG   rf   r(   r(   r)   sparsifyL  s    zSparseCoefMixin.sparsifyN)rR   rS   rT   rU   rg   rh   r(   r(   r(   r)   re   4  s   re   c               @   s$   e Zd ZdZd	ddZd
ddZdS )LinearRegressiona	  
    Ordinary least squares Linear Regression.

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

    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.

    n_jobs : int or None, optional (default=None)
        The number of jobs to use for the computation. This will only provide
        speedup for n_targets > 1 and sufficient large problems.
        ``None`` means 1 unless in a :obj:`joblib.parallel_backend` context.
        ``-1`` means using all processors. See :term:`Glossary <n_jobs>`
        for more details.

    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.

    Examples
    --------
    >>> import numpy as np
    >>> from sklearn.linear_model import LinearRegression
    >>> X = np.array([[1, 1], [1, 2], [2, 2], [2, 3]])
    >>> # y = 1 * x_0 + 2 * x_1 + 3
    >>> y = np.dot(X, np.array([1, 2])) + 3
    >>> reg = LinearRegression().fit(X, y)
    >>> reg.score(X, y)
    1.0
    >>> reg.coef_
    array([1., 2.])
    >>> reg.intercept_ # doctest: +ELLIPSIS
    3.0000...
    >>> reg.predict(np.array([[3, 5]]))
    array([16.])

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

    TFNc             C   s   || _ || _|| _|| _d S )N)r=   r   copy_Xn_jobs)rG   r=   r   rj   rk   r(   r(   r)   __init__  s    zLinearRegression.__init__c       
         s^  | j }t dddgddd\ |dk	rBt|jdkrBtd| j | j| j| j	|d	\ }}}|dk	rt
 |\ t rjd
k rt }|d | _|d | _nTt|d fddtjd D }	tdd |	D | _tdd |	D | _n&t \| _| _| _| _| jj| _jdkrLt| j| _| ||| | S )aC  
        Fit linear model.

        Parameters
        ----------
        X : array-like or sparse matrix, shape (n_samples, n_features)
            Training data

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

        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.
        r+   r,   rJ   T)r.   Z	y_numericZmulti_outputNr   z)Sample weights must be 1D array or scalar)r=   r   r-   r&   r   r      )rk   c             3   s,   | ]$}t t d d |f  V  qd S )N)r
   r   r[   ).0j)r$   r%   r(   r)   	<genexpr>  s   z'LinearRegression.fit.<locals>.<genexpr>c             S   s   g | ]}|d  qS )r   r(   )rn   outr(   r(   r)   
<listcomp>  s    z(LinearRegression.fit.<locals>.<listcomp>c             S   s   g | ]}|d  qS )rm   r(   )rn   rq   r(   r(   r)   rr     s    )rk   r   r   Z
atleast_1dr<   rZ   rC   r=   r   rj   rE   r   r    r   rI   Z	_residuesr	   ranger7   rb   r   ZlstsqZrank_Z	singular_rL   r[   rQ   )
rG   r$   r%   r&   Zn_jobs_r@   rB   rA   rq   Zoutsr(   )r$   r%   r)   rH     s4    



zLinearRegression.fit)TFTN)N)rR   rS   rT   rU   rl   rH   r(   r(   r(   r)   ri   j  s   > 
ri   c          	   C   s  | j \}}	t| r:d}t| |||dd|d\} }}
}}nt| |||||d\} }}
}}t|dr|rxt|
t|	r|rt|t|	st	
dt d}d}t|tjr|dkr||	k}|dkrtj|	|	f| jd	d
}tj| j| |d t|dsd}t|dr|dkrt| j|jgg }|jdkrVtj|	|d	d
}tj| j||d n2|j d }tj|	|f|dd
}tj|j| |jd | ||
||||fS )z6Aux function used at beginning of fit in linear modelsFT)r=   r   r-   r>   r?   )r=   r   r-   r?   Z	__array__zlGram matrix was provided but X was centered to fit intercept, or X was normalized : recomputing Gram matrix.autoNC)r7   r/   r1   )rq   r   F)r7   r   Z
isspmatrixrC   rY   r   Zallcloser;   r9   warningswarnUserWarningr3   r   Zstring_typesemptyr/   rP   rL   Zfind_common_typer<   )r$   r%   ZXyZ
precomputer   r=   r-   r?   rD   r\   r@   rB   rA   Zcommon_dtypeZ	n_targetsr(   r(   r)   _pre_fit  sD    





r{   )N)FTNFT)T)9rU   Z
__future__r   abcr   r   r4   rw   Znumpyr   Zscipy.sparser   r   Zscipyr   Z	externalsr   Zutils._joblibr	   r
   baser   r   r   Zutilsr   r   Zutils.validationr   r   Zutils.extmathr   Zutils.sparsefuncsr   r   Zutils.fixesr   Zutils.seq_datasetr   r   r   
exceptionsr   Zpreprocessing.datar   r:   r#   r*   rC   rE   Zwith_metaclassrF   rW   objectre   ri   r{   r(   r(   r(   r)   <module>   s@   
- 
O+O6 