a
    �ù»jî™  ã                   @   sú  d Z ddlZddlmZ ddlmZmZ ddl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mZmZ ddlmZmZmZ ddlmZ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Z)d.dd„Z*d/dd„Z+edgej,geedddd�dgeedddd�dgdedhƒgdgdgdgdœdd�dddddddœd d!„ƒZ-edgdgeeddd"d�dgeedddd�dgddgdgdgdgdgd#œ	dd�dddddddd$œd%d&„ƒZ.G d'd(„ d(eee'ƒZ/d0d*d+„Z0G d,d-„ d-ee'ƒZ1dS )1z&Orthogonal matching pursuit algorithmsé    N)Úsqrt)ÚIntegralÚReal)Úlinalg)Úget_lapack_funcsé   )ÚMultiOutputMixinÚRegressorMixinÚ_fit_context)Úcheck_cv)ÚBunchÚas_float_arrayÚcheck_array)ÚIntervalÚ
StrOptionsÚvalidate_params)ÚMetadataRouterÚMethodMappingÚ_raise_for_paramsÚ_routing_enabledÚprocess_routing)ÚParallelÚdelayed)Úvalidate_dataé   )ÚLinearModelÚ_pre_fitzŠOrthogonal matching pursuit ended prematurely due to linear dependence in the dictionary. The requested precision might not have been met.TFc                 C   s   |r|   d¡} n
t | ¡} t | j¡j}t d| f¡\}}td| fƒ\}	t 	| j
|¡}
|}t d¡}d}t | jd ¡}|durŠ| jd n|}tj||f| jd�}|r°t |¡}t t t 	| j
|¡¡¡}||k sâ|
| d |k rötjttdd	� �qà|dk�rÎt 	| dd…d|…f j
| dd…|f ¡||d|…f< tj|d|…d|…f ||d|…f ddd
dd� |||d|…f ƒd }t | dd…|f ¡d | }||k�r¼tjttdd	� �qàt|ƒ|||f< nt | dd…|f ¡|d< || j
| | j
| ƒ\| j
|< | j
|< |
| |
|  |
|< |
|< || ||  ||< ||< |d7 }|	|d|…d|…f |
d|… d
dd�\}}|�r�||d|…|d f< |t 	| dd…d|…f |¡ }|du�rÒ||ƒd |k�rÒ�qàq°||kr°�qàq°|�r
||d|… |dd…d|…f |fS ||d|… |fS dS )a•  Orthogonal Matching Pursuit step using the Cholesky decomposition.

    Parameters
    ----------
    X : ndarray of shape (n_samples, n_features)
        Input dictionary. Columns are assumed to have unit norm.

    y : ndarray of shape (n_samples,)
        Input targets.

    n_nonzero_coefs : int
        Targeted number of non-zero elements.

    tol : float, default=None
        Targeted squared error, if not None overrides n_nonzero_coefs.

    copy_X : bool, default=True
        Whether the design matrix X must be copied by the algorithm. A false
        value is only helpful if X is already Fortran-ordered, otherwise a
        copy is made anyway.

    return_path : bool, default=False
        Whether to return every value of the nonzero coefficients along the
        forward path. Useful for cross-validation.

    Returns
    -------
    gamma : ndarray of shape (n_nonzero_coefs,)
        Non-zero elements of the solution.

    idx : ndarray of shape (n_nonzero_coefs,)
        Indices of the positions of the elements in gamma within the solution
        vector.

    coef : ndarray of shape (n_features, n_nonzero_coefs)
        The first k values of column k correspond to the coefficient value
        for the active features at that step. The lower left triangle contains
        garbage. Only returned if ``return_path=True``.

    n_active : int
        Number of active features at convergence.
    ÚF©Únrm2Úswap©Úpotrsr   r   N©Údtyper   ©Ú
stacklevelTF©ZtransÚlowerÚoverwrite_bZcheck_finite©r   r   ©r(   r)   )ÚcopyÚnpÚasfortranarrayÚfinfor$   Úepsr   Úget_blas_funcsr   ÚdotÚTÚemptyÚarangeÚshapeÚ
empty_likeÚargmaxÚabsÚwarningsÚwarnÚ	prematureÚRuntimeWarningÚsolve_triangularZnormr   )ÚXÚyÚn_nonzero_coefsÚtolÚcopy_XÚreturn_pathÚ	min_floatr   r    r"   ÚalphaZresidualÚgammaÚn_activeÚindicesÚmax_featuresÚLÚcoefsÚlamÚvÚLkkÚ_© rQ   ú?E:\soccer\ml_env\Lib\site-packages\sklearn/linear_model/_omp.pyÚ_cholesky_omp$   sj    +



6ú
& ÿ
 $rS   c                 C   s:  |r|   d¡nt | ¡} |s$|jjs,|  ¡ }t | j¡j}t 	d| f¡\}	}
t
d| fƒ\}t t| ƒ¡}|}|}d}t d¡}d}|dur’t| ƒn|}tj||f| jd�}d|d< |rÀt |¡}t t |¡¡}||k sè|| d	 |k rütjttd
d� �qú|dk�r¬| |d|…f ||d|…f< tj|d|…d|…f ||d|…f ddddd� |	||d|…f ƒd	 }| ||f | }||k�rštjttd
d� �qút|ƒ|||f< nt| ||f ƒ|d< |
| | | | ƒ\| |< | |< |
| j| | j| ƒ\| j|< | j|< || ||  ||< ||< || ||  ||< ||< |d7 }||d|…d|…f |d|… ddd�\}}|�r†||d|…|d f< t | dd…d|…f |¡}|| }|du�rì||7 }t ||d|… ¡}||8 }t|ƒ|k�rø�qúqÀ||krÀ�qúqÀ|�r$||d|… |dd…d|…f |fS ||d|… |fS dS )a®  Orthogonal Matching Pursuit step on a precomputed Gram matrix.

    This function uses the Cholesky decomposition method.

    Parameters
    ----------
    Gram : ndarray of shape (n_features, n_features)
        Gram matrix of the input data matrix.

    Xy : ndarray of shape (n_features,)
        Input targets.

    n_nonzero_coefs : int
        Targeted number of non-zero elements.

    tol_0 : float, default=None
        Squared norm of y, required if tol is not None.

    tol : float, default=None
        Targeted squared error, if not None overrides n_nonzero_coefs.

    copy_Gram : bool, default=True
        Whether the gram matrix must be copied by the algorithm. A false
        value is only helpful if it is already Fortran-ordered, otherwise a
        copy is made anyway.

    copy_Xy : bool, default=True
        Whether the covariance vector Xy must be copied by the algorithm.
        If False, it may be overwritten.

    return_path : bool, default=False
        Whether to return every value of the nonzero coefficients along the
        forward path. Useful for cross-validation.

    Returns
    -------
    gamma : ndarray of shape (n_nonzero_coefs,)
        Non-zero elements of the solution.

    idx : ndarray of shape (n_nonzero_coefs,)
        Indices of the positions of the elements in gamma within the solution
        vector.

    coefs : ndarray of shape (n_features, n_nonzero_coefs)
        The first k values of column k correspond to the coefficient value
        for the active features at that step. The lower left triangle contains
        garbage. Only returned if ``return_path=True``.

    n_active : int
        Number of active features at convergence.
    r   r   r!   r   Nr#   g      ð?r*   r   é   r%   r   TFr'   r+   )r,   r-   r.   ÚflagsÚ	writeabler/   r$   r0   r   r1   r   r5   Úlenr4   r7   r8   r9   r:   r;   r<   r=   r>   r   r3   r2   Úinner)ÚGramÚXyrA   Ztol_0rB   Ú	copy_GramÚcopy_XyrD   rE   r   r    r"   rI   rF   Ztol_currÚdeltarG   rH   rJ   rK   rL   rM   rN   rO   rP   ÚbetarQ   rQ   rR   Ú	_gram_omp˜   sz    =


ú
& ÿ

$r_   z
array-likeÚleft©ÚclosedÚbooleanÚauto)r?   r@   rA   rB   Ú
precomputerC   rD   Úreturn_n_iter©Zprefer_skip_nested_validation)rA   rB   re   rC   rD   rf   c             
   C   sN  t | d|d�} d}|jdkr(| dd¡}t |ƒ}|jd dkrBd}|du rj|du rjttd| jd  ƒdƒ}|du rˆ|| jd krˆtd	ƒ‚|d
kr¤| jd | jd k}|�rt | j	| ¡}t 
|¡}t | j	|¡}	|durìtj|d dd�}
nd}
t||	|||
|d|d�S |�r2t | jd |jd | jd f¡}nt | jd |jd f¡}g }t|jd ƒD ]´}t| |dd…|f ||||d�}|�rð|\}}}}|dd…dd…dt|ƒ…f }t|j	ƒD ]0\}}|d|d … ||d|d … ||f< �q¼n|\}}}||||f< | |¡ �q^|jd dk�r,|d }|�r@t |¡|fS t |¡S dS )a^  Orthogonal Matching Pursuit (OMP).

    Solves n_targets Orthogonal Matching Pursuit problems.
    An instance of the problem has the form:

    When parametrized by the number of non-zero coefficients using
    `n_nonzero_coefs`:
    argmin ||y - X\gamma||^2 subject to ||\gamma||_0 <= n_{nonzero coefs}

    When parametrized by error using the parameter `tol`:
    argmin ||\gamma||_0 subject to ||y - X\gamma||^2 <= tol

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

    Parameters
    ----------
    X : array-like of shape (n_samples, n_features)
        Input data. Columns are assumed to have unit norm.

    y : ndarray of shape (n_samples,) or (n_samples, n_targets)
        Input targets.

    n_nonzero_coefs : int, default=None
        Desired number of non-zero entries in the solution. If None (by
        default) this value is set to 10% of n_features.

    tol : float, default=None
        Maximum squared norm of the residual. If not None, overrides n_nonzero_coefs.

    precompute : 'auto' or bool, default=False
        Whether to perform precomputations. Improves performance when n_targets
        or n_samples is very large.

    copy_X : bool, default=True
        Whether the design matrix X must be copied by the algorithm. A false
        value is only helpful if X is already Fortran-ordered, otherwise a
        copy is made anyway.

    return_path : bool, default=False
        Whether to return every value of the nonzero coefficients along the
        forward path. Useful for cross-validation.

    return_n_iter : bool, default=False
        Whether or not to return the number of iterations.

    Returns
    -------
    coef : ndarray of shape (n_features,) or (n_features, n_targets)
        Coefficients of the OMP solution. If `return_path=True`, this contains
        the whole coefficient path. In this case its shape is
        (n_features, n_features) or (n_features, n_targets, n_features) and
        iterating over the last axis generates coefficients in increasing order
        of active features.

    n_iters : array-like or int
        Number of active features across every target. Returned only if
        `return_n_iter` is set to True.

    See Also
    --------
    OrthogonalMatchingPursuit : Orthogonal Matching Pursuit model.
    orthogonal_mp_gram : Solve OMP problems using Gram matrix and the product X.T * y.
    lars_path : Compute Least Angle Regression or Lasso path using LARS algorithm.
    sklearn.decomposition.sparse_encode : Sparse coding.

    Notes
    -----
    Orthogonal matching pursuit was introduced in S. Mallat, Z. Zhang,
    Matching pursuits with time-frequency dictionaries, IEEE Transactions on
    Signal Processing, Vol. 41, No. 12. (December 1993), pp. 3397-3415.
    (https://www.di.ens.fr/~mallat/papiers/MallatPursuit93.pdf)

    This implementation is based on Rubinstein, R., Zibulevsky, M. and Elad,
    M., Efficient Implementation of the K-SVD Algorithm using Batch Orthogonal
    Matching Pursuit Technical Report - CS Technion, April 2008.
    https://www.cs.technion.ac.il/~ronrubin/Publications/KSVD-OMP-v2.pdf

    Examples
    --------
    >>> from sklearn.datasets import make_regression
    >>> from sklearn.linear_model import orthogonal_mp
    >>> X, y = make_regression(noise=4, random_state=0)
    >>> coef = orthogonal_mp(X, y)
    >>> coef.shape
    (100,)
    >>> X[:1,] @ coef
    array([-78.68...])
    r   ©Úorderr,   Fr   éÿÿÿÿTNçš™™™™™¹?ú>The number of atoms cannot be more than the number of featuresrd   r   r   ©Zaxis)rA   rB   Únorms_squaredr[   r\   rD   )rC   rD   )r   ÚndimZreshaper6   ÚmaxÚintÚ
ValueErrorr-   r2   r3   r.   ÚsumÚorthogonal_mp_gramÚzerosÚrangerS   rW   Ú	enumerateÚappendÚsqueeze)r?   r@   rA   rB   re   rC   rD   rf   ÚGrZ   rn   ÚcoefÚn_itersÚkÚoutrP   ÚidxrL   Ún_iterrH   ÚxrQ   rQ   rR   Úorthogonal_mp"  sj    p
ÿ
ø$ÿ,
r‚   Zneither)	rY   rZ   rA   rB   rn   r[   r\   rD   rf   )rA   rB   rn   r[   r\   rD   rf   c                C   sX  t | d|d�} t |¡}|jdkr4|jd dkr4d}|jdkr^|dd…tjf }|dur^|g}|sj|jjsr| ¡ }|du r’|du r’t	dt
| ƒ ƒ}|durª|du rªtdƒ‚|durÂ|dk rÂtd	ƒ‚|du rÚ|dkrÚtd
ƒ‚|du rö|t
| ƒkrötdƒ‚|�r"tjt
| ƒ|jd t
| ƒf| jd�}	ntjt
| ƒ|jd f| jd�}	g }
t|jd ƒD ]Ê}t| |dd…|f ||du�r||| nd||d|d�}|�rú|\}}}}|	dd…dd…dt
|ƒ…f }	t|jƒD ]0\}}|d|d … |	|d|d … ||f< �qÆn|\}}}||	||f< |
 |¡ �qR|jd dk�r6|
d }
|�rJt |	¡|
fS t |	¡S dS )an  Gram Orthogonal Matching Pursuit (OMP).

    Solves n_targets Orthogonal Matching Pursuit problems using only
    the Gram matrix X.T * X and the product X.T * y.

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

    Parameters
    ----------
    Gram : array-like of shape (n_features, n_features)
        Gram matrix of the input data: `X.T * X`.

    Xy : array-like of shape (n_features,) or (n_features, n_targets)
        Input targets multiplied by `X`: `X.T * y`.

    n_nonzero_coefs : int, default=None
        Desired number of non-zero entries in the solution. If `None` (by
        default) this value is set to 10% of n_features.

    tol : float, default=None
        Maximum squared norm of the residual. If not `None`,
        overrides `n_nonzero_coefs`.

    norms_squared : array-like of shape (n_targets,), default=None
        Squared L2 norms of the lines of `y`. Required if `tol` is not None.

    copy_Gram : bool, default=True
        Whether the gram matrix must be copied by the algorithm. A `False`
        value is only helpful if it is already Fortran-ordered, otherwise a
        copy is made anyway.

    copy_Xy : bool, default=True
        Whether the covariance vector `Xy` must be copied by the algorithm.
        If `False`, it may be overwritten.

    return_path : bool, default=False
        Whether to return every value of the nonzero coefficients along the
        forward path. Useful for cross-validation.

    return_n_iter : bool, default=False
        Whether or not to return the number of iterations.

    Returns
    -------
    coef : ndarray of shape (n_features,) or (n_features, n_targets)
        Coefficients of the OMP solution. If `return_path=True`, this contains
        the whole coefficient path. In this case its shape is
        `(n_features, n_features)` or `(n_features, n_targets, n_features)` and
        iterating over the last axis yields coefficients in increasing order
        of active features.

    n_iters : list or int
        Number of active features across every target. Returned only if
        `return_n_iter` is set to True.

    See Also
    --------
    OrthogonalMatchingPursuit : Orthogonal Matching Pursuit model (OMP).
    orthogonal_mp : Solves n_targets Orthogonal Matching Pursuit problems.
    lars_path : Compute Least Angle Regression or Lasso path using
        LARS algorithm.
    sklearn.decomposition.sparse_encode : Generic sparse coding.
        Each column of the result is the solution to a Lasso problem.

    Notes
    -----
    Orthogonal matching pursuit was introduced in G. Mallat, Z. Zhang,
    Matching pursuits with time-frequency dictionaries, IEEE Transactions on
    Signal Processing, Vol. 41, No. 12. (December 1993), pp. 3397-3415.
    (https://www.di.ens.fr/~mallat/papiers/MallatPursuit93.pdf)

    This implementation is based on Rubinstein, R., Zibulevsky, M. and Elad,
    M., Efficient Implementation of the K-SVD Algorithm using Batch Orthogonal
    Matching Pursuit Technical Report - CS Technion, April 2008.
    https://www.cs.technion.ac.il/~ronrubin/Publications/KSVD-OMP-v2.pdf

    Examples
    --------
    >>> from sklearn.datasets import make_regression
    >>> from sklearn.linear_model import orthogonal_mp_gram
    >>> X, y = make_regression(noise=4, random_state=0)
    >>> coef = orthogonal_mp_gram(X.T @ X, X.T @ y)
    >>> coef.shape
    (100,)
    >>> X[:1,] @ coef
    array([-78.68...])
    r   rh   r   TNrk   zSGram OMP needs the precomputed norms in order to evaluate the error sum of squares.r   zEpsilon cannot be negativez$The number of atoms must be positiverl   r#   F)r[   r\   rD   )r   r-   Zasarrayro   r6   ÚnewaxisrU   rV   r,   rq   rW   rr   ru   r$   rv   r_   rw   r3   rx   ry   )rY   rZ   rA   rB   rn   r[   r\   rD   rf   r{   r|   r}   r~   rP   r   rL   r€   rH   r�   rQ   rQ   rR   rt   Ó  sh    q

ÿÿ&ø
,
rt   c                   @   sz   e Zd ZU dZeedddd�dgeedddd�dgdgedhƒdgd	œZe	e
d
< ddddd	œdd„Zedd�dd„ ƒZdS )ÚOrthogonalMatchingPursuita  Orthogonal Matching Pursuit model (OMP).

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

    Parameters
    ----------
    n_nonzero_coefs : int, default=None
        Desired number of non-zero entries in the solution. Ignored if `tol` is set.
        When `None` and `tol` is also `None`, this value is either set to 10% of
        `n_features` or 1, whichever is greater.

    tol : float, default=None
        Maximum squared norm of the residual. If not None, overrides n_nonzero_coefs.

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

    precompute : 'auto' or bool, default='auto'
        Whether to use a precomputed Gram and Xy matrix to speed up
        calculations. Improves performance when :term:`n_targets` or
        :term:`n_samples` is very large. Note that if you already have such
        matrices, you can pass them directly to the fit method.

    Attributes
    ----------
    coef_ : ndarray of shape (n_features,) or (n_targets, n_features)
        Parameter vector (w in the formula).

    intercept_ : float or ndarray of shape (n_targets,)
        Independent term in decision function.

    n_iter_ : int or array-like
        Number of active features across every target.

    n_nonzero_coefs_ : int or None
        The number of non-zero coefficients in the solution or `None` when `tol` is
        set. If `n_nonzero_coefs` is None and `tol` is None this value is either set
        to 10% of `n_features` or 1, whichever is greater.

    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
    --------
    orthogonal_mp : Solves n_targets Orthogonal Matching Pursuit problems.
    orthogonal_mp_gram :  Solves n_targets Orthogonal Matching Pursuit
        problems using only the Gram matrix X.T * X and the product X.T * y.
    lars_path : Compute Least Angle Regression or Lasso path using LARS algorithm.
    Lars : Least Angle Regression model a.k.a. LAR.
    LassoLars : Lasso model fit with Least Angle Regression a.k.a. Lars.
    sklearn.decomposition.sparse_encode : Generic sparse coding.
        Each column of the result is the solution to a Lasso problem.
    OrthogonalMatchingPursuitCV : Cross-validated
        Orthogonal Matching Pursuit model (OMP).

    Notes
    -----
    Orthogonal matching pursuit was introduced in G. Mallat, Z. Zhang,
    Matching pursuits with time-frequency dictionaries, IEEE Transactions on
    Signal Processing, Vol. 41, No. 12. (December 1993), pp. 3397-3415.
    (https://www.di.ens.fr/~mallat/papiers/MallatPursuit93.pdf)

    This implementation is based on Rubinstein, R., Zibulevsky, M. and Elad,
    M., Efficient Implementation of the K-SVD Algorithm using Batch Orthogonal
    Matching Pursuit Technical Report - CS Technion, April 2008.
    https://www.cs.technion.ac.il/~ronrubin/Publications/KSVD-OMP-v2.pdf

    Examples
    --------
    >>> from sklearn.linear_model import OrthogonalMatchingPursuit
    >>> from sklearn.datasets import make_regression
    >>> X, y = make_regression(noise=4, random_state=0)
    >>> reg = OrthogonalMatchingPursuit().fit(X, y)
    >>> reg.score(X, y)
    0.9991...
    >>> reg.predict(X[:1,])
    array([-78.3854...])
    r   Nr`   ra   r   rc   rd   ©rA   rB   Úfit_interceptre   Ú_parameter_constraintsTc                C   s   || _ || _|| _|| _d S ©Nr…   )ÚselfrA   rB   r†   re   rQ   rQ   rR   Ú__init__æ  s    z"OrthogonalMatchingPursuit.__init__rg   c              
   C   s,  t | ||ddd�\}}|jd }t||d| j| jdd�\}}}}}}}|jdkrb|dd…tjf }| jdu rŒ| j	du rŒt
td| ƒdƒ| _n| j	duržd| _n| j| _|du rÐt||| j| j	dddd�\}	| _nB| j	durìtj|d	 d
d�nd}
t||| j| j	|
dddd�\}	| _|	j| _|  |||¡ | S )a˜  Fit the model using X, y as training data.

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

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

        Returns
        -------
        self : object
            Returns an instance of self.
        T)Zmulti_outputÚ	y_numericr   N©r,   rk   F)rA   rB   re   rC   rf   r   r   rm   )rZ   rA   rB   rn   r[   r\   rf   )r   r6   r   re   r†   ro   r-   rƒ   rA   rB   rp   rq   Ún_nonzero_coefs_r‚   Ún_iter_rs   rt   r3   Úcoef_Z_set_intercept)r‰   r?   r@   Z
n_featuresZX_offsetZy_offsetZX_scalerY   rZ   r�   Znorms_sqrQ   rQ   rR   Úfitó  sH    
ÿ

ù
 ø
zOrthogonalMatchingPursuit.fit)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r‡   ÚdictÚ__annotations__rŠ   r
   r�   rQ   rQ   rQ   rR   r„   …  s   
Zü
úr„   éd   c           
   	   C   s¾   |r$|   ¡ } |  ¡ }|  ¡ }|  ¡ }|rx| jdd�}| |8 } ||8 }|jdd�}t|dd�}||8 }t|dd�}||8 }t| ||ddddd�}	|	jdkrª|	dd…tjf }	t |	j|j¡| S )	a[  Compute the residues on left-out data for a full LARS path.

    Parameters
    ----------
    X_train : ndarray of shape (n_samples, n_features)
        The data to fit the LARS on.

    y_train : ndarray of shape (n_samples)
        The target variable to fit LARS on.

    X_test : ndarray of shape (n_samples, n_features)
        The data to compute the residues on.

    y_test : ndarray of shape (n_samples)
        The target variable to compute the residues on.

    copy : bool, default=True
        Whether X_train, X_test, y_train and y_test should be copied.  If
        False, they may be overwritten.

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

    max_iter : int, default=100
        Maximum numbers of iterations to perform, therefore maximum features
        to include. 100 by default.

    Returns
    -------
    residues : ndarray of shape (n_samples, max_features)
        Residues of the prediction on the test data.
    r   rm   FrŒ   NT)rA   rB   re   rC   rD   r   )	r,   Úmeanr   r‚   ro   r-   rƒ   r2   r3   )
ZX_trainZy_trainZX_testZy_testr,   r†   Úmax_iterZX_meanZy_meanrL   rQ   rQ   rR   Ú_omp_path_residues3  s4    ,ù	
rš   c                   @   sz   e Zd ZU dZdgdgeedddd�dgdgedgdgd	œZeed
< ddddddd	œdd„Z	e
dd�dd„ ƒZdd„ ZdS )ÚOrthogonalMatchingPursuitCVa  Cross-validated Orthogonal Matching Pursuit model (OMP).

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

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

    Parameters
    ----------
    copy : bool, default=True
        Whether the design matrix X must be copied by the algorithm. A false
        value is only helpful if X is already Fortran-ordered, otherwise a
        copy is made anyway.

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

    max_iter : int, default=None
        Maximum numbers of iterations to perform, therefore maximum features
        to include. 10% of ``n_features`` but at least 5 if available.

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

        - None, to use the default 5-fold 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, :class:`~sklearn.model_selection.KFold` is used.

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

        .. versionchanged:: 0.22
            ``cv`` default value if None changed from 3-fold to 5-fold.

    n_jobs : int, default=None
        Number of CPUs to use during the cross validation.
        ``None`` means 1 unless in a :obj:`joblib.parallel_backend` context.
        ``-1`` means using all processors. See :term:`Glossary <n_jobs>`
        for more details.

    verbose : bool or int, default=False
        Sets the verbosity amount.

    Attributes
    ----------
    intercept_ : float or ndarray of shape (n_targets,)
        Independent term in decision function.

    coef_ : ndarray of shape (n_features,) or (n_targets, n_features)
        Parameter vector (w in the problem formulation).

    n_nonzero_coefs_ : int
        Estimated number of non-zero coefficients giving the best mean squared
        error over the cross-validation folds.

    n_iter_ : int or array-like
        Number of active features across every target for the model refit with
        the best hyperparameters got by cross-validating across all folds.

    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
    --------
    orthogonal_mp : Solves n_targets Orthogonal Matching Pursuit problems.
    orthogonal_mp_gram : Solves n_targets Orthogonal Matching Pursuit
        problems using only the Gram matrix X.T * X and the product X.T * y.
    lars_path : Compute Least Angle Regression or Lasso path using LARS algorithm.
    Lars : Least Angle Regression model a.k.a. LAR.
    LassoLars : Lasso model fit with Least Angle Regression a.k.a. Lars.
    OrthogonalMatchingPursuit : Orthogonal Matching Pursuit model (OMP).
    LarsCV : Cross-validated Least Angle Regression model.
    LassoLarsCV : Cross-validated Lasso model fit with Least Angle Regression.
    sklearn.decomposition.sparse_encode : Generic sparse coding.
        Each column of the result is the solution to a Lasso problem.

    Notes
    -----
    In `fit`, once the optimal number of non-zero coefficients is found through
    cross-validation, the model is fit again using the entire training set.

    Examples
    --------
    >>> from sklearn.linear_model import OrthogonalMatchingPursuitCV
    >>> from sklearn.datasets import make_regression
    >>> X, y = make_regression(n_features=100, n_informative=10,
    ...                        noise=4, random_state=0)
    >>> reg = OrthogonalMatchingPursuitCV(cv=5).fit(X, y)
    >>> reg.score(X, y)
    0.9991...
    >>> reg.n_nonzero_coefs_
    np.int64(10)
    >>> reg.predict(X[:1,])
    array([-78.3854...])
    rc   r   Nr`   ra   Z	cv_objectÚverbose©r,   r†   r™   ÚcvÚn_jobsrœ   r‡   TFc                C   s(   || _ || _|| _|| _|| _|| _d S rˆ   r�   )r‰   r,   r†   r™   rž   rŸ   rœ   rQ   rQ   rR   rŠ   õ  s    
z$OrthogonalMatchingPursuitCV.__init__rg   c           
         sL  t |ˆdƒ tˆˆ ˆddd�\‰ ‰tˆ ddd�‰ tˆjdd�}tƒ rXtˆdfi |¤Ž}ntƒ }ti d�|_ˆj	s”t
ttd	ˆ jd
  ƒdƒˆ jd
 ƒnˆj	‰tˆjˆjd�‡ ‡‡‡fdd„|jˆ fi |jj¤ŽD ƒƒ}t
dd„ |D ƒƒ‰t ‡fdd„|D ƒ¡}t |jdd�¡d
 }|ˆ_t|ˆjd� ˆ ˆ¡}	|	jˆ_|	jˆ_|	jˆ_ˆS )a  Fit the model using X, y as training data.

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

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

        **fit_params : dict
            Parameters to pass to the underlying splitter.

            .. versionadded:: 1.4
                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
            Returns an instance of self.
        r�   Tr   )r‹   Zensure_min_featuresF)r,   Zensure_all_finite)Ú
classifier)Úsplitrk   r   é   )rŸ   rœ   c              	   3   s>   | ]6\}}t tƒˆ | ˆ| ˆ | ˆ| ˆjˆjˆƒV  qd S rˆ   )r   rš   r,   r†   )Ú.0ÚtrainÚtest)r?   r™   r‰   r@   rQ   rR   Ú	<genexpr>1  s   
÷ùz2OrthogonalMatchingPursuitCV.fit.<locals>.<genexpr>c                 s   s   | ]}|j d  V  qdS )r   N)r6   ©r£   ÚfoldrQ   rQ   rR   r¦   >  ó    c                    s$   g | ]}|d ˆ … d j dd�‘qS )Nr   r   rm   )r˜   r§   )Úmin_early_stoprQ   rR   Ú
<listcomp>@  r©   z3OrthogonalMatchingPursuitCV.fit.<locals>.<listcomp>r   rm   )rA   r†   )r   r   r   r   rž   r   r   r   Úsplitterr™   Úminrp   rq   r6   r   rŸ   rœ   r¡   r-   ÚarrayZargminr˜   r�   r„   r†   r�   r�   Z
intercept_rŽ   )
r‰   r?   r@   Z
fit_paramsrž   Zrouted_paramsZcv_pathsZ	mse_foldsZbest_n_nonzero_coefsZomprQ   )r?   r™   rª   r‰   r@   rR   r�     s>    ÿ&ý
öÿþýzOrthogonalMatchingPursuitCV.fitc                 C   s*   t | jjd�j| jtƒ jdd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.4

        Returns
        -------
        routing : MetadataRouter
            A :class:`~sklearn.utils.metadata_routing.MetadataRouter` encapsulating
            routing information.
        )Úownerr�   r¡   )ZcallerZcallee)r¬   Zmethod_mapping)r   Ú	__class__r‘   Úaddrž   r   )r‰   ZrouterrQ   rQ   rR   Úget_metadata_routingN  s
    þz0OrthogonalMatchingPursuitCV.get_metadata_routing)r‘   r’   r“   r”   r   r   r‡   r•   r–   rŠ   r
   r�   r²   rQ   rQ   rQ   rR   r›   ~  s$   
núø
Gr›   )NTF)NNTTF)TTr—   )2r”   r:   Úmathr   Únumbersr   r   Únumpyr-   Zscipyr   Zscipy.linalg.lapackr   Úbaser   r	   r
   Zmodel_selectionr   Úutilsr   r   r   Zutils._param_validationr   r   r   Zutils.metadata_routingr   r   r   r   r   Zutils.parallelr   r   Zutils.validationr   Z_baser   r   r<   rS   r_   Zndarrayr‚   rt   r„   rš   r›   rQ   rQ   rQ   rR   Ú<module>   s’   ÿ
x     ø
 ø
õ÷ %÷ôö % 4   ù
K