<commit_before># Least Angle Regression algorithm. See doc/module/glm for a
# complete discussion.
#
# Author: <NAME> <<EMAIL>>
#         <NAME> <<EMAIL>>
#
# License: BSD Style.

import numpy as np
from scipy import linalg
from .base import LinearModel
import scipy.sparse as sp # needed by LeastAngleRegression
from .._minilearn import lars_fit_wrap


# Notes: np.ma.dot copies the masked array before doing the dot
# product. Maybe we should implement in C our own masked_dot that does
# not make unnecessary copies.

# all linalg.solve solve a triangular system, so this could be heavily
# optimized by binding (in scipy ?) trsv or trsm

def lars_path(X, y, max_iter=None, alpha_min=0, method="lar", precompute=True):
    """ Compute Least Angle Regression and LASSO path

        Parameters
        -----------
        X: array, shape: (n, p)
            Input data
        y: array, shape: (n)
            Input targets
        max_iter: integer, optional
            The number of 'kink' in the path
        alpha_min: float, optional
            The minimum correlation along the path. It corresponds
            to the regularization parameter alpha parameter in the Lasso.
        method: 'lar' or 'lasso'
            Specifies the problem solved: the LAR or its variant the LASSO-LARS
            that gives the solution of the LASSO problem for any regularization
            parameter.

        Returns
        --------
        alphas: array, shape: (k)
            The alphas along the path
        
        active: array, shape (?)
            Indices of active variables at the end of the path.
        
        coefs: array, shape (p,k)
            Coefficients along the path

        Notes
        ------
        XXX : add reference papers and wikipedia page
    
    TODOS:
    precompute : empty for now

    TODO: detect stationary points.
    Lasso variant
    store full path
    """

    X = np.atleast_2d(X)
    y = np.atleast_1d(y)

    n_samples, n_features = X.shape

    if max_iter is None:
        max_iter = min(n_samples, n_features)

    max_pred = max_iter # OK for now

    beta     = np.zeros ((max_iter + 1, X.shape[1]))
    alphas   = np.zeros (max_iter + 1)
    n_iter, n_pred = 0, 0
    active   = list()
    # holds the sign of covariance
    sign_active = np.empty (max_pred, dtype=np.int8)
    drop = False

    # will hold the cholesky factorization
    # only lower part is referenced. We do not create it as
    # empty array because chol_solve calls chkfinite on the
    # whole array, which can cause problems.
    L = np.zeros ((max_pred, max_pred), dtype=np.float64)

    Xt  = X.T
    Xna = Xt.view(np.ma.MaskedArray) # variables not in the active set
                                     # should have a better name

    Xna.soften_mask()

    while 1:


        # Calculate covariance matrix and get maximum
        res = y - np.dot (X, beta[n_iter]) # there are better ways
        Cov = np.ma.dot (Xna, res)
        print Cov

        imax    = np.ma.argmax (np.ma.abs(Cov)) #rename
        Cov_max =  Cov.data [imax]

        alpha = np.abs(Cov_max) #sum (np.abs(beta[n_iter]))
        alphas [n_iter] = alpha

        if (n_iter >= max_iter or n_pred >= max_pred ):
            break

        if (alpha < alpha_min): break


        if not drop:

            # Update the Cholesky factorization of (Xa * Xa') #
            #                                                 #
            #          ( L   0 )                              #
            #   L  ->  (       )  , where L * w = b           #
            #          ( w   z )    z = 1 - ||w||             #
            #                                                 #
            #   where u is the last added to the active set   #

            n_pred += 1
            active.append(imax)
            Xna[imax] = np.ma.masked
            Cov[imax] = np.ma.masked<commit_msg>Remove "debugging" traces...<commit_after># Least Angle Regression algorithm. See doc/module/glm for a
# complete discussion.
#
# Author: <NAME> <<EMAIL>>
#         <NAME> <<EMAIL>>
#
# License: BSD Style.

import numpy as np
from scipy import linalg
from .base import LinearModel
import scipy.sparse as sp # needed by LeastAngleRegression
from .._minilearn import lars_fit_wrap


# Notes: np.ma.dot copies the masked array before doing the dot
# product. Maybe we should implement in C our own masked_dot that does
# not make unnecessary copies.

# all linalg.solve solve a triangular system, so this could be heavily
# optimized by binding (in scipy ?) trsv or trsm

def lars_path(X, y, max_iter=None, alpha_min=0, method="lar", precompute=True):
    """ Compute Least Angle Regression and LASSO path

        Parameters
        -----------
        X: array, shape: (n, p)
            Input data
        y: array, shape: (n)
            Input targets
        max_iter: integer, optional
            The number of 'kink' in the path
        alpha_min: float, optional
            The minimum correlation along the path. It corresponds
            to the regularization parameter alpha parameter in the Lasso.
        method: 'lar' or 'lasso'
            Specifies the problem solved: the LAR or its variant the LASSO-LARS
            that gives the solution of the LASSO problem for any regularization
            parameter.

        Returns
        --------
        alphas: array, shape: (k)
            The alphas along the path
        
        active: array, shape (?)
            Indices of active variables at the end of the path.
        
        coefs: array, shape (p,k)
            Coefficients along the path

        Notes
        ------
        XXX : add reference papers and wikipedia page
    
    TODOS:
    precompute : empty for now

    TODO: detect stationary points.
    Lasso variant
    store full path
    """

    X = np.atleast_2d(X)
    y = np.atleast_1d(y)

    n_samples, n_features = X.shape

    if max_iter is None:
        max_iter = min(n_samples, n_features)

    max_pred = max_iter # OK for now

    beta     = np.zeros ((max_iter + 1, X.shape[1]))
    alphas   = np.zeros (max_iter + 1)
    n_iter, n_pred = 0, 0
    active   = list()
    # holds the sign of covariance
    sign_active = np.empty (max_pred, dtype=np.int8)
    drop = False

    # will hold the cholesky factorization
    # only lower part is referenced. We do not create it as
    # empty array because chol_solve calls chkfinite on the
    # whole array, which can cause problems.
    L = np.zeros ((max_pred, max_pred), dtype=np.float64)

    Xt  = X.T
    Xna = Xt.view(np.ma.MaskedArray) # variables not in the active set
                                     # should have a better name

    Xna.soften_mask()

    while 1:


        # Calculate covariance matrix and get maximum
        res = y - np.dot (X, beta[n_iter]) # there are better ways
        Cov = np.ma.dot (Xna, res)

        imax    = np.ma.argmax (np.ma.abs(Cov)) #rename
        Cov_max =  Cov.data [imax]

        alpha = np.abs(Cov_max) #sum (np.abs(beta[n_iter]))
        alphas [n_iter] = alpha

        if (n_iter >= max_iter or n_pred >= max_pred ):
            break

        if (alpha < alpha_min): break


        if not drop:

            # Update the Cholesky factorization of (Xa * Xa') #
            #                                                 #
            #          ( L   0 )                              #
            #   L  ->  (       )  , where L * w = b           #
            #          ( w   z )    z = 1 - ||w||             #
            #                                                 #
            #   where u is the last added to the active set   #

            n_pred += 1
            active.append(imax)
            Xna[imax] = np.ma.masked
            Cov[imax] = np.ma.masked
{'fraction_non_alphanumeric': 0.0851200410835794, 'fraction_numerical': 0.0038515855693927333, 'mean_word_length': 1.6442634080108622, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2941570', 'n_tokens_mistral': 2369, 'n_tokens_neox': 2268, 'n_words': 815}