import warnings

from distutils.version import LooseVersion

import numpy as np
import pytest
from scipy import linalg

from sklearn.model_selection import train_test_split
from sklearn.utils.testing import assert_allclose
from sklearn.utils.testing import assert_array_almost_equal
from sklearn.utils.testing import assert_equal
from sklearn.utils.testing import assert_less
from sklearn.utils.testing import assert_greater
from sklearn.utils.testing import assert_raises
from sklearn.utils.testing import ignore_warnings
from sklearn.utils.testing import assert_warns
from sklearn.utils.testing import TempMemmap
from sklearn.exceptions import ConvergenceWarning
from sklearn import linear_model, datasets
from sklearn.linear_model.least_angle import _lars_path_residues, LassoLarsIC

# TODO: use another dataset that has multiple drops
diabetes = datasets.load_diabetes()
X, y = diabetes.data, diabetes.target
G = np.dot(X.T, X)
Xy = np.dot(X.T, y)
n_samples = y.size


def test_simple():
    # Principle of Lars is to keep covariances tied and decreasing

    # also test verbose output
    from io import StringIO
    import sys
    old_stdout = sys.stdout
    try:
        sys.stdout = StringIO()

        _, _, coef_path_ = linear_model.lars_path(
            X, y, method='lar', verbose=10)

        sys.stdout = old_stdout

        for i, coef_ in enumerate(coef_path_.T):
            res = y - np.dot(X, coef_)
            cov = np.dot(X.T, res)
            C = np.max(abs(cov))
            eps = 1e-3
            ocur = len(cov[C - eps < abs(cov)])
            if i < X.shape[1]:
                assert ocur == i + 1
            else:
                # no more than max_pred variables can go into the active set
                assert ocur == X.shape[1]
    finally:
        sys.stdout = old_stdout


def test_simple_precomputed():
    # The same, with precomputed Gram matrix

    _, _, coef_path_ = linear_model.lars_path(
        X, y, Gram=G, method='lar')

    for i, coef_ in enumerate(coef_path_.T):
        res = y - np.dot(X, coef_)
        cov = np.dot(X.T, res)
        C = np.max(abs(cov))
        eps = 1e-3
        ocur = len(cov[C - eps < abs(cov)])
        if i < X.shape[1]:
            assert ocur == i + 1
        else:
            # no more than max_pred variables can go into the active set
            assert ocur == X.shape[1]


def _assert_same_lars_path_result(output1, output2):
    assert_equal(len(output1), len(output2))
    for o1, o2 in zip(output1, output2):
        assert_allclose(o1, o2)


@pytest.mark.parametrize('method', ['lar', 'lasso'])
@pytest.mark.parametrize('return_path', [True, False])
def test_lars_path_gram_equivalent(method, return_path):
    _assert_same_lars_path_result(
        linear_model.lars_path_gram(
            Xy=Xy, Gram=G, n_samples=n_samples, method=method,
            return_path=return_path),
        linear_model.lars_path(
            X, y, Gram=G, method=method,
            return_path=return_path))


def test_x_none_gram_none_raises_value_error():
    # Test that lars_path with no X and Gram raises exception
    Xy = np.dot(X.T, y)
    assert_raises(ValueError, linear_model.lars_path, None, y, Gram=None,
                  Xy=Xy)


def test_all_precomputed():
    # Test that lars_path with precomputed Gram and Xy gives the right answer
    G = np.dot(X.T, X)
    Xy = np.dot(X.T, y)
    for method in 'lar', 'lasso':
        output = linear_model.lars_path(X, y, method=method)
        output_pre = linear_model.lars_path(X, y, Gram=G, Xy=Xy,
                                            method=method)
        for expected, got in zip(output, output_pre):
            assert_array_almost_equal(expected, got)


@pytest.mark.filterwarnings('ignore: `rcond` parameter will change')
# numpy deprecation
def test_lars_lstsq():
    # Test that Lars gives least square solution at the end
    # of the path
    X1 = 3 * X  # use un-normalized dataset
    clf = linear_model.LassoLars(alpha=0.)
    clf.fit(X1, y)
    # Avoid FutureWarning about default value change when numpy >= 1.14
    rcond = None if LooseVersion(np.__version__) >= '1.14' else -1
    coef_lstsq = np.linalg.lstsq(X1, y, rcond=rcond)[0]
    assert_array_almost_equal(clf.coef_, coef_lstsq)


@pytest.mark.filterwarnings('ignore:`rcond` parameter will change')
# numpy deprecation
def test_lasso_gives_lstsq_solution():
    # Test that Lars Lasso gives least square solution at the end
    # of the path
    _, _, coef_path_ = linear_model.lars_path(X, y, method='lasso')
    coef_lstsq = np.linalg.lstsq(X, y)[0]
    assert_array_almost_equal(coef_lstsq, coef_path_[:, -1])


def test_collinearity():
    # Check that lars_path is robust to collinearity in input
    X = np.array([[3., 3., 1.],
                  [2., 2., 0.],
                  [1., 1., 0]])
    y = np.array([1., 0., 0])
    rng = np.random.RandomState(0)

    f = ignore_warnings
    _, _, coef_path_ = f(linear_model.lars_path)(X, y, alpha_min=0.01)
    assert not np.isnan(coef_path_).any()
    residual = np.dot(X, coef_path_[:, -1]) - y
    assert_less((residual ** 2).sum(), 1.)  # just make sure it's bounded

    n_samples = 10
    X = rng.rand(n_samples, 5)
    y = np.zeros(n_samples)
    _, _, coef_path_ = linear_model.lars_path(X, y, Gram='auto', copy_X=False,
                                              copy_Gram=False, alpha_min=0.,
                                              method='lasso', verbose=0,
                                              max_iter=500)
    assert_array_almost_equal(coef_path_, np.zeros_like(coef_path_))


def test_no_path():
    # Test that the ``return_path=False`` option returns the correct output
    alphas_, _, coef_path_ = linear_model.lars_path(
        X, y, method='lar')
    alpha_, _, coef = linear_model.lars_path(
        X, y, method='lar', return_path=False)

    assert_array_almost_equal(coef, coef_path_[:, -1])
    assert alpha_ == alphas_[-1]


def test_no_path_precomputed():
    # Test that the ``return_path=False`` option with Gram remains correct
    alphas_, _, coef_path_ = linear_model.lars_path(
        X, y, method='lar', Gram=G)
    alpha_, _, coef = linear_model.lars_path(
        X, y, method='lar', Gram=G, return_path=False)

    assert_array_almost_equal(coef, coef_path_[:, -1])
    assert alpha_ == alphas_[-1]


def test_no_path_all_precomputed():
    # Test that the ``return_path=False`` option with Gram and Xy remains
    # correct
    X, y = 3 * diabetes.data, diabetes.target
    G = np.dot(X.T, X)
    Xy = np.dot(X.T, y)
    alphas_, _, coef_path_ = linear_model.lars_path(
        X, y, method='lasso', Xy=Xy, Gram=G, alpha_min=0.9)
    alpha_, _, coef = linear_model.lars_path(
        X, y, method='lasso', Gram=G, Xy=Xy, alpha_min=0.9, return_path=False)

    assert_array_almost_equal(coef, coef_path_[:, -1])
    assert alpha_ == alphas_[-1]


@pytest.mark.filterwarnings('ignore: The default value of cv')  # 0.22
@pytest.mark.parametrize(
        'classifier',
        [linear_model.Lars, linear_model.LarsCV, linear_model.LassoLarsIC])
def test_lars_precompute(classifier):
    # Check for different values of precompute
    G = np.dot(X.T, X)

    clf = classifier(precompute=G)
    output_1 = ignore_warnings(clf.fit)(X, y).coef_
    for precompute in [True, False, 'auto', None]:
        clf = classifier(precompute=precompute)
        output_2 = clf.fit(X, y).coef_
        assert_array_almost_equal(output_1, output_2, decimal=8)


def test_singular_matrix():
    # Test when input is a singular matrix
    X1 = np.array([[1, 1.], [1., 1.]])
    y1 = np.array([1, 1])
    _, _, coef_path = linear_model.lars_path(X1, y1)
    assert_array_almost_equal(coef_path.T, [[0, 0], [1, 0]])


def test_rank_deficient_design():
    # consistency test that checks that LARS Lasso is handling rank
    # deficient input data (with n_features < rank) in the same way
    # as coordinate descent Lasso
    y = [5, 0, 5]
    for X in (
              [[5, 0],
               [0, 5],
               [10, 10]],
              [[10, 10, 0],
               [1e-32, 0, 0],
               [0, 0, 1]]
             ):
        # To be able to use the coefs to compute the objective function,
        # we need to turn off normalization
        lars = linear_model.LassoLars(.1, normalize=False)
        coef_lars_ = lars.fit(X, y).coef_
        obj_lars = (1. / (2. * 3.)
                    * linalg.norm(y - np.dot(X, coef_lars_)) ** 2
                    + .1 * linalg.norm(coef_lars_, 1))
        coord_descent = linear_model.Lasso(.1, tol=1e-6, normalize=False)
        coef_cd_ = coord_descent.fit(X, y).coef_
        obj_cd = ((1. / (2. * 3.)) * linalg.norm(y - np.dot(X, coef_cd_)) ** 2
                  + .1 * linalg.norm(coef_cd_, 1))
        assert_less(obj_lars, obj_cd * (1. + 1e-8))


def test_lasso_lars_vs_lasso_cd():
    # Test that LassoLars and Lasso using coordinate descent give the
    # same results.
    X = 3 * diabetes.data

    alphas, _, lasso_path = linear_model.lars_path(X, y, method='lasso')
    lasso_cd = linear_model.Lasso(fit_intercept=False, tol=1e-8)
    for c, a in zip(lasso_path.T, alphas):
        if a == 0:
            continue
        lasso_cd.alpha = a
        lasso_cd.fit(X, y)
        error = linalg.norm(c - lasso_cd.coef_)
        assert_less(error, 0.01)

    # similar test, with the classifiers
    for alpha in np.linspace(1e-2, 1 - 1e-2, 20):
        clf1 = linear_model.LassoLars(alpha=alpha, normalize=False).fit(X, y)
        clf2 = linear_model.Lasso(alpha=alpha, tol=1e-8,
                                  normalize=False).fit(X, y)
        err = linalg.norm(clf1.coef_ - clf2.coef_)
        assert_less(err, 1e-3)

    # same test, with normalized data
    X = diabetes.data
    alphas, _, lasso_path = linear_model.lars_path(X, y, method='lasso')
    lasso_cd = linear_model.Lasso(fit_intercept=False, normalize=True,
                                  tol=1e-8)
    for c, a in zip(lasso_path.T, alphas):
        if a == 0:
            continue
        lasso_cd.alpha = a
        lasso_cd.fit(X, y)
        error = linalg.norm(c - lasso_cd.coef_)
        assert_less(error, 0.01)


def test_lasso_lars_vs_lasso_cd_early_stopping():
    # Test that LassoLars and Lasso using coordinate descent give the
    # same results when early stopping is used.
    # (test : before, in the middle, and in the last part of the path)
    alphas_min = [10, 0.9, 1e-4]

    for alpha_min in alphas_min:
        alphas, _, lasso_path = linear_model.lars_path(X, y, method='lasso',
                                                       alpha_min=alpha_min)
        lasso_cd = linear_model.Lasso(fit_intercept=False, tol=1e-8)
        lasso_cd.alpha = alphas[-1]
        lasso_cd.fit(X, y)
        error = linalg.norm(lasso_path[:, -1] - lasso_cd.coef_)
        assert_less(error, 0.01)

    # same test, with normalization
    for alpha_min in alphas_min:
        alphas, _, lasso_path = linear_model.lars_path(X, y, method='lasso',
                                                       alpha_min=alpha_min)
        lasso_cd = linear_model.Lasso(fit_intercept=True, normalize=True,
                                      tol=1e-8)
        lasso_cd.alpha = alphas[-1]
        lasso_cd.fit(X, y)
        error = linalg.norm(lasso_path[:, -1] - lasso_cd.coef_)
        assert_less(error, 0.01)


def test_lasso_lars_path_length():
    # Test that the path length of the LassoLars is right
    lasso = linear_model.LassoLars()
    lasso.fit(X, y)
    lasso2 = linear_model.LassoLars(alpha=lasso.alphas_[2])
    lasso2.fit(X, y)
    assert_array_almost_equal(lasso.alphas_[:3], lasso2.alphas_)
    # Also check that the sequence of alphas is always decreasing
    assert np.all(np.diff(lasso.alphas_) < 0)


def test_lasso_lars_vs_lasso_cd_ill_conditioned():
    # Test lasso lars on a very ill-conditioned design, and check that
    # it does not blow up, and stays somewhat close to a solution given
    # by the coordinate descent solver
    # Also test that lasso_path (using lars_path output style) gives
    # the same result as lars_path and previous lasso output style
    # under these conditions.
    rng = np.random.RandomState(42)

    # Generate data
    n, m = 70, 100
    k = 5
    X = rng.randn(n, m)
    w = np.zeros((m, 1))
    i = np.arange(0, m)
    rng.shuffle(i)
    supp = i[:k]
    w[supp] = np.sign(rng.randn(k, 1)) * (rng.rand(k, 1) + 1)
    y = np.dot(X, w)
    sigma = 0.2
    y += sigma * rng.rand(*y.shape)
    y = y.squeeze()
    lars_alphas, _, lars_coef = linear_model.lars_path(X, y, method='lasso')

    _, lasso_coef2, _ = linear_model.lasso_path(X, y,
                                                alphas=lars_alphas,
                                                tol=1e-6,
                                                fit_intercept=False)

    assert_array_almost_equal(lars_coef, lasso_coef2, decimal=1)


def test_lasso_lars_vs_lasso_cd_ill_conditioned2():
    # Create an ill-conditioned situation in which the LARS has to go
    # far in the path to converge, and check that LARS and coordinate
    # descent give the same answers
    # Note it used to be the case that Lars had to use the drop for good
    # strategy for this but this is no longer the case with the
    # equality_tolerance checks
    X = [[1e20, 1e20, 0],
         [-1e-32, 0, 0],
         [1, 1, 1]]
    y = [10, 10, 1]
    alpha = .0001

    def objective_function(coef):
        return (1. / (2. * len(X)) * linalg.norm(y - np.dot(X, coef)) ** 2
                + alpha * linalg.norm(coef, 1))

    lars = linear_model.LassoLars(alpha=alpha, normalize=False)
    assert_warns(ConvergenceWarning, lars.fit, X, y)
    lars_coef_ = lars.coef_
    lars_obj = objective_function(lars_coef_)

    coord_descent = linear_model.Lasso(alpha=alpha, tol=1e-4, normalize=False)
    cd_coef_ = coord_descent.fit(X, y).coef_
    cd_obj = objective_function(cd_coef_)

    assert_less(lars_obj, cd_obj * (1. + 1e-8))


def test_lars_add_features():
    # assure that at least some features get added if necessary
    # test for 6d2b4c
    # Hilbert matrix
    n = 5
    H = 1. / (np.arange(1, n + 1) + np.arange(n)[:, np.newaxis])
    clf = linear_model.Lars(fit_intercept=False).fit(
        H, np.arange(n))
    assert np.all(np.isfinite(clf.coef_))


def test_lars_n_nonzero_coefs(verbose=False):
    lars = linear_model.Lars(n_nonzero_coefs=6, verbose=verbose)
    lars.fit(X, y)
    assert_equal(len(lars.coef_.nonzero()[0]), 6)
    # The path should be of length 6 + 1 in a Lars going down to 6
    # non-zero coefs
    assert_equal(len(lars.alphas_), 7)


@ignore_warnings
def test_multitarget():
    # Assure that estimators receiving multidimensional y do the right thing
    Y = np.vstack([y, y ** 2]).T
    n_targets = Y.shape[1]
    estimators = [
        linear_model.LassoLars(),
        linear_model.Lars(),
        # regression test for gh-1615
        linear_model.LassoLars(fit_intercept=False),
        linear_model.Lars(fit_intercept=False),
    ]

    for estimator in estimators:
        estimator.fit(X, Y)
        Y_pred = estimator.predict(X)
        alphas, active, coef, path = (estimator.alphas_, estimator.active_,
                                      estimator.coef_, estimator.coef_path_)
        for k in range(n_targets):
            estimator.fit(X, Y[:, k])
            y_pred = estimator.predict(X)
            assert_array_almost_equal(alphas[k], estimator.alphas_)
            assert_array_almost_equal(active[k], estimator.active_)
            assert_array_almost_equal(coef[k], estimator.coef_)
            assert_array_almost_equal(path[k], estimator.coef_path_)
            assert_array_almost_equal(Y_pred[:, k], y_pred)


@pytest.mark.filterwarnings('ignore: The default value of cv')  # 0.22
def test_lars_cv():
    # Test the LassoLarsCV object by checking that the optimal alpha
    # increases as the number of samples increases.
    # This property is not actually guaranteed in general and is just a
    # property of the given dataset, with the given steps chosen.
    old_alpha = 0
    lars_cv = linear_model.LassoLarsCV()
    for length in (400, 200, 100):
        X = diabetes.data[:length]
        y = diabetes.target[:length]
        lars_cv.fit(X, y)
        np.testing.assert_array_less(old_alpha, lars_cv.alpha_)
        old_alpha = lars_cv.alpha_
    assert not hasattr(lars_cv, 'n_nonzero_coefs')


@pytest.mark.filterwarnings('ignore::FutureWarning')
def test_lars_cv_max_iter():
    with warnings.catch_warnings(record=True) as w:
        rng = np.random.RandomState(42)
        x = rng.randn(len(y))
        X = diabetes.data
        X = np.c_[X, x, x]  # add correlated features
        lars_cv = linear_model.LassoLarsCV(max_iter=5)
        lars_cv.fit(X, y)
    assert len(w) == 0


def test_lasso_lars_ic():
    # Test the LassoLarsIC object by checking that
    # - some good features are selected.
    # - alpha_bic > alpha_aic
    # - n_nonzero_bic < n_nonzero_aic
    lars_bic = linear_model.LassoLarsIC('bic')
    lars_aic = linear_model.LassoLarsIC('aic')
    rng = np.random.RandomState(42)
    X = diabetes.data
    X = np.c_[X, rng.randn(X.shape[0], 5)]  # add 5 bad features
    lars_bic.fit(X, y)
    lars_aic.fit(X, y)
    nonzero_bic = np.where(lars_bic.coef_)[0]
    nonzero_aic = np.where(lars_aic.coef_)[0]
    assert_greater(lars_bic.alpha_, lars_aic.alpha_)
    assert_less(len(nonzero_bic), len(nonzero_aic))
    assert_less(np.max(nonzero_bic), diabetes.data.shape[1])

    # test error on unknown IC
    lars_broken = linear_model.LassoLarsIC('<unknown>')
    assert_raises(ValueError, lars_broken.fit, X, y)


def test_lars_path_readonly_data():
    # When using automated memory mapping on large input, the
    # fold data is in read-only mode
    # This is a non-regression test for:
    # https://github.com/scikit-learn/scikit-learn/issues/4597
    splitted_data = train_test_split(X, y, random_state=42)
    with TempMemmap(splitted_data) as (X_train, X_test, y_train, y_test):
        # The following should not fail despite copy=False
        _lars_path_residues(X_train, y_train, X_test, y_test, copy=False)


@pytest.mark.filterwarnings('ignore: The default of the `iid`')  # 0.22
def test_lars_path_positive_constraint():
    # this is the main test for the positive parameter on the lars_path method
    # the estimator classes just make use of this function

    # we do the test on the diabetes dataset

    # ensure that we get negative coefficients when positive=False
    # and all positive when positive=True
    # for method 'lar' (default) and lasso

    # Once deprecation of LAR + positive option is done use these:
    # assert_raises(ValueError, linear_model.lars_path, diabetes['data'],
    #               diabetes['target'], method='lar', positive=True)
    with pytest.warns(DeprecationWarning, match='broken'):
        linear_model.lars_path(diabetes['data'], diabetes['target'],
                               return_path=True, method='lar',
                               positive=True)
    method = 'lasso'
    _, _, coefs = \
        linear_model.lars_path(X, y, return_path=True, method=method,
                               positive=False)
    assert coefs.min() < 0

    _, _, coefs = \
        linear_model.lars_path(X, y, return_path=True, method=method,
                               positive=True)
    assert coefs.min() >= 0


# now we gonna test the positive option for all estimator classes

default_parameter = {'fit_intercept': False}

estimator_parameter_map = {'LassoLars': {'alpha': 0.1},
                           'LassoLarsCV': {},
                           'LassoLarsIC': {}}


@pytest.mark.filterwarnings('ignore: The default value of cv')  # 0.22
def test_estimatorclasses_positive_constraint():
    # testing the transmissibility for the positive option of all estimator
    # classes in this same function here
    default_parameter = {'fit_intercept': False}

    estimator_parameter_map = {'LassoLars': {'alpha': 0.1},
                               'LassoLarsCV': {},
                               'LassoLarsIC': {}}
    for estname in estimator_parameter_map:
        params = default_parameter.copy()
        params.update(estimator_parameter_map[estname])
        estimator = getattr(linear_model, estname)(positive=False, **params)
        estimator.fit(X, y)
        assert estimator.coef_.min() < 0
        estimator = getattr(linear_model, estname)(positive=True, **params)
        estimator.fit(X, y)
        assert min(estimator.coef_) >= 0


def test_lasso_lars_vs_lasso_cd_positive():
    # Test that LassoLars and Lasso using coordinate descent give the
    # same results when using the positive option

    # This test is basically a copy of the above with additional positive
    # option. However for the middle part, the comparison of coefficient values
    # for a range of alphas, we had to make an adaptations. See below.

    # not normalized data
    X = 3 * diabetes.data

    alphas, _, lasso_path = linear_model.lars_path(X, y, method='lasso',
                                                   positive=True)
    lasso_cd = linear_model.Lasso(fit_intercept=False, tol=1e-8, positive=True)
    for c, a in zip(lasso_path.T, alphas):
        if a == 0:
            continue
        lasso_cd.alpha = a
        lasso_cd.fit(X, y)
        error = linalg.norm(c - lasso_cd.coef_)
        assert_less(error, 0.01)

    # The range of alphas chosen for coefficient comparison here is restricted
    # as compared with the above test without the positive option. This is due
    # to the circumstance that the Lars-Lasso algorithm does not converge to
    # the least-squares-solution for small alphas, see 'Least Angle Regression'
    # by Efron et al 2004. The coefficients are typically in congruence up to
    # the smallest alpha reached by the Lars-Lasso algorithm and start to
    # diverge thereafter.  See
    # https://gist.github.com/michigraber/7e7d7c75eca694c7a6ff

    for alpha in np.linspace(6e-1, 1 - 1e-2, 20):
        clf1 = linear_model.LassoLars(fit_intercept=False, alpha=alpha,
                                      normalize=False, positive=True).fit(X, y)
        clf2 = linear_model.Lasso(fit_intercept=False, alpha=alpha, tol=1e-8,
                                  normalize=False, positive=True).fit(X, y)
        err = linalg.norm(clf1.coef_ - clf2.coef_)
        assert_less(err, 1e-3)

    # normalized data
    X = diabetes.data
    alphas, _, lasso_path = linear_model.lars_path(X, y, method='lasso',
                                                   positive=True)
    lasso_cd = linear_model.Lasso(fit_intercept=False, normalize=True,
                                  tol=1e-8, positive=True)
    for c, a in zip(lasso_path.T[:-1], alphas[:-1]):  # don't include alpha=0
        lasso_cd.alpha = a
        lasso_cd.fit(X, y)
        error = linalg.norm(c - lasso_cd.coef_)
        assert_less(error, 0.01)


def test_lasso_lars_vs_R_implementation():
    # Test that sklearn LassoLars implementation agrees with the LassoLars
    # implementation available in R (lars library) under the following
    # scenarios:
    # 1) fit_intercept=False and normalize=False
    # 2) fit_intercept=True and normalize=True

    # Let's generate the data used in the bug report 7778
    y = np.array([-6.45006793, -3.51251449, -8.52445396, 6.12277822,
                  -19.42109366])
    x = np.array([[0.47299829, 0, 0, 0, 0],
                  [0.08239882, 0.85784863, 0, 0, 0],
                  [0.30114139, -0.07501577, 0.80895216, 0, 0],
                  [-0.01460346, -0.1015233, 0.0407278, 0.80338378, 0],
                  [-0.69363927, 0.06754067, 0.18064514, -0.0803561,
                   0.40427291]])

    X = x.T

    ###########################################################################
    # Scenario 1: Let's compare R vs sklearn when fit_intercept=False and
    # normalize=False
    ###########################################################################
    #
    # The R result was obtained using the following code:
    #
    # library(lars)
    # model_lasso_lars = lars(X, t(y), type="lasso", intercept=FALSE,
    #                         trace=TRUE, normalize=FALSE)
    # r = t(model_lasso_lars$beta)
    #

    r = np.array([[0, 0, 0, 0, 0, -79.810362809499026, -83.528788732782829,
                   -83.777653739190711, -83.784156932888934,
                   -84.033390591756657],
                  [0, 0, 0, 0, -0.476624256777266, 0, 0, 0, 0,
                   0.025219751009936],
                  [0, -3.577397088285891, -4.702795355871871,
                   -7.016748621359461, -7.614898471899412, -0.336938391359179,
                   0, 0, 0.001213370600853, 0.048162321585148],
                  [0, 0, 0, 2.231558436628169, 2.723267514525966,
                   2.811549786389614, 2.813766976061531, 2.817462468949557,
                   2.817368178703816, 2.816221090636795],
                  [0, 0, -1.218422599914637, -3.457726183014808,
                   -4.021304522060710, -45.827461592423745,
                   -47.776608869312305,
                   -47.911561610746404, -47.914845922736234,
                   -48.039562334265717]])

    model_lasso_lars = linear_model.LassoLars(alpha=0, fit_intercept=False,
                                              normalize=False)
    model_lasso_lars.fit(X, y)
    skl_betas = model_lasso_lars.coef_path_

    assert_array_almost_equal(r, skl_betas, decimal=12)
    ###########################################################################

    ###########################################################################
    # Scenario 2: Let's compare R vs sklearn when fit_intercept=True and
    # normalize=True
    #
    # Note: When normalize is equal to True, R returns the coefficients in
    # their original units, that is, they are rescaled back, whereas sklearn
    # does not do that, therefore, we need to do this step before comparing
    # their results.
    ###########################################################################
    #
    # The R result was obtained using the following code:
    #
    # library(lars)
    # model_lasso_lars2 = lars(X, t(y), type="lasso", intercept=TRUE,
    #                           trace=TRUE, normalize=TRUE)
    # r2 = t(model_lasso_lars2$beta)

    r2 = np.array([[0, 0, 0, 0, 0],
                   [0, 0, 0, 8.371887668009453, 19.463768371044026],
                   [0, 0, 0, 0, 9.901611055290553],
                   [0, 7.495923132833733, 9.245133544334507,
                    17.389369207545062, 26.971656815643499],
                   [0, 0, -1.569380717440311, -5.924804108067312,
                    -7.996385265061972]])

    model_lasso_lars2 = linear_model.LassoLars(alpha=0, fit_intercept=True,
                                               normalize=True)
    model_lasso_lars2.fit(X, y)
    skl_betas2 = model_lasso_lars2.coef_path_

    # Let's rescale back the coefficients returned by sklearn before comparing
    # against the R result (read the note above)
    temp = X - np.mean(X, axis=0)
    normx = np.sqrt(np.sum(temp ** 2, axis=0))
    skl_betas2 /= normx[:, np.newaxis]

    assert_array_almost_equal(r2, skl_betas2, decimal=12)
    ###########################################################################


@pytest.mark.parametrize('copy_X', [True, False])
def test_lasso_lars_copyX_behaviour(copy_X):
    """
    Test that user input regarding copy_X is not being overridden (it was until
    at least version 0.21)

    """
    lasso_lars = LassoLarsIC(copy_X=copy_X, precompute=False)
    rng = np.random.RandomState(0)
    X = rng.normal(0, 1, (100, 5))
    X_copy = X.copy()
    y = X[:, 2]
    lasso_lars.fit(X, y)
    assert copy_X == np.array_equal(X, X_copy)


@pytest.mark.parametrize('copy_X', [True, False])
def test_lasso_lars_fit_copyX_behaviour(copy_X):
    """
    Test that user input to .fit for copy_X overrides default __init__ value

    """
    lasso_lars = LassoLarsIC(precompute=False)
    rng = np.random.RandomState(0)
    X = rng.normal(0, 1, (100, 5))
    X_copy = X.copy()
    y = X[:, 2]
    lasso_lars.fit(X, y, copy_X=copy_X)
    assert copy_X == np.array_equal(X, X_copy)

[((24, 11, 24, 35), 'sklearn.datasets.load_diabetes', 'datasets.load_diabetes', ({}, {}), '()', False, 'from sklearn import linear_model, datasets\n'), ((26, 4, 26, 18), 'numpy.dot', 'np.dot', ({(26, 11, 26, 14): 'X.T', (26, 16, 26, 17): 'X'}, {}), '(X.T, X)', True, 'import numpy as np\n'), ((27, 5, 27, 19), 'numpy.dot', 'np.dot', ({(27, 12, 27, 15): 'X.T', (27, 17, 27, 18): 'y'}, {}), '(X.T, y)', True, 'import numpy as np\n'), ((86, 1, 86, 52), 'pytest.mark.parametrize', 'pytest.mark.parametrize', ({(86, 25, 86, 33): '"""method"""', (86, 35, 86, 51): "['lar', 'lasso']"}, {}), "('method', ['lar', 'lasso'])", False, 'import pytest\n'), ((87, 1, 87, 54), 'pytest.mark.parametrize', 'pytest.mark.parametrize', ({(87, 25, 87, 38): '"""return_path"""', (87, 40, 87, 53): '[True, False]'}, {}), "('return_path', [True, False])", False, 'import pytest\n'), ((117, 1, 117, 68), 'pytest.mark.filterwarnings', 'pytest.mark.filterwarnings', ({(117, 28, 117, 67): '"""ignore: `rcond` parameter will change"""'}, {}), "('ignore: `rcond` parameter will change')", False, 'import pytest\n'), ((131, 1, 131, 67), 'pytest.mark.filterwarnings', 'pytest.mark.filterwarnings', ({(131, 28, 131, 66): '"""ignore:`rcond` parameter will change"""'}, {}), "('ignore:`rcond` parameter will change')", False, 'import pytest\n'), ((202, 1, 202, 62), 'pytest.mark.filterwarnings', 'pytest.mark.filterwarnings', ({(202, 28, 202, 61): '"""ignore: The default value of cv"""'}, {}), "('ignore: The default value of cv')", False, 'import pytest\n'), ((203, 1, 205, 75), 'pytest.mark.parametrize', 'pytest.mark.parametrize', ({(204, 8, 204, 20): '"""classifier"""', (205, 8, 205, 74): '[linear_model.Lars, linear_model.LarsCV, linear_model.LassoLarsIC]'}, {}), "('classifier', [linear_model.Lars, linear_model.\n    LarsCV, linear_model.LassoLarsIC])", False, 'import pytest\n'), ((437, 1, 437, 62), 'pytest.mark.filterwarnings', 'pytest.mark.filterwarnings', ({(437, 28, 437, 61): '"""ignore: The default value of cv"""'}, {}), "('ignore: The default value of cv')", False, 'import pytest\n'), ((454, 1, 454, 52), 'pytest.mark.filterwarnings', 'pytest.mark.filterwarnings', ({(454, 28, 454, 51): '"""ignore::FutureWarning"""'}, {}), "('ignore::FutureWarning')", False, 'import pytest\n'), ((500, 1, 500, 63), 'pytest.mark.filterwarnings', 'pytest.mark.filterwarnings', ({(500, 28, 500, 62): '"""ignore: The default of the `iid`"""'}, {}), "('ignore: The default of the `iid`')", False, 'import pytest\n'), ((539, 1, 539, 62), 'pytest.mark.filterwarnings', 'pytest.mark.filterwarnings', ({(539, 28, 539, 61): '"""ignore: The default value of cv"""'}, {}), "('ignore: The default value of cv')", False, 'import pytest\n'), ((708, 1, 708, 49), 'pytest.mark.parametrize', 'pytest.mark.parametrize', ({(708, 25, 708, 33): '"""copy_X"""', (708, 35, 708, 48): '[True, False]'}, {}), "('copy_X', [True, False])", False, 'import pytest\n'), ((724, 1, 724, 49), 'pytest.mark.parametrize', 'pytest.mark.parametrize', ({(724, 25, 724, 33): '"""copy_X"""', (724, 35, 724, 48): '[True, False]'}, {}), "('copy_X', [True, False])", False, 'import pytest\n'), ((64, 23, 65, 35), 'sklearn.linear_model.lars_path', 'linear_model.lars_path', (), '', False, 'from sklearn import linear_model, datasets\n'), ((100, 9, 100, 23), 'numpy.dot', 'np.dot', ({(100, 16, 100, 19): 'X.T', (100, 21, 100, 22): 'y'}, {}), '(X.T, y)', True, 'import numpy as np\n'), ((101, 4, 102, 24), 'sklearn.utils.testing.assert_raises', 'assert_raises', (), '', False, 'from sklearn.utils.testing import assert_raises\n'), ((107, 8, 107, 22), 'numpy.dot', 'np.dot', ({(107, 15, 107, 18): 'X.T', (107, 20, 107, 21): 'X'}, {}), '(X.T, X)', True, 'import numpy as np\n'), ((108, 9, 108, 23), 'numpy.dot', 'np.dot', ({(108, 16, 108, 19): 'X.T', (108, 21, 108, 22): 'y'}, {}), '(X.T, y)', True, 'import numpy as np\n'), ((123, 10, 123, 42), 'sklearn.linear_model.LassoLars', 'linear_model.LassoLars', (), '', False, 'from sklearn import linear_model, datasets\n'), ((128, 4, 128, 52), 'sklearn.utils.testing.assert_array_almost_equal', 'assert_array_almost_equal', ({(128, 30, 128, 39): 'clf.coef_', (128, 41, 128, 51): 'coef_lstsq'}, {}), '(clf.coef_, coef_lstsq)', False, 'from sklearn.utils.testing import assert_array_almost_equal\n'), ((136, 23, 136, 67), 'sklearn.linear_model.lars_path', 'linear_model.lars_path', (), '', False, 'from sklearn import linear_model, datasets\n'), ((138, 4, 138, 60), 'sklearn.utils.testing.assert_array_almost_equal', 'assert_array_almost_equal', ({(138, 30, 138, 40): 'coef_lstsq', (138, 42, 138, 59): 'coef_path_[:, (-1)]'}, {}), '(coef_lstsq, coef_path_[:, (-1)])', False, 'from sklearn.utils.testing import assert_array_almost_equal\n'), ((143, 8, 145, 31), 'numpy.array', 'np.array', ({(143, 17, 145, 30): '[[3.0, 3.0, 1.0], [2.0, 2.0, 0.0], [1.0, 1.0, 0]]'}, {}), '([[3.0, 3.0, 1.0], [2.0, 2.0, 0.0], [1.0, 1.0, 0]])', True, 'import numpy as np\n'), ((146, 8, 146, 29), 'numpy.array', 'np.array', ({(146, 17, 146, 28): '[1.0, 0.0, 0]'}, {}), '([1.0, 0.0, 0])', True, 'import numpy as np\n'), ((147, 10, 147, 34), 'numpy.random.RandomState', 'np.random.RandomState', ({(147, 32, 147, 33): '0'}, {}), '(0)', True, 'import numpy as np\n'), ((157, 8, 157, 27), 'numpy.zeros', 'np.zeros', ({(157, 17, 157, 26): 'n_samples'}, {}), '(n_samples)', True, 'import numpy as np\n'), ((158, 23, 161, 59), 'sklearn.linear_model.lars_path', 'linear_model.lars_path', (), '', False, 'from sklearn import linear_model, datasets\n'), ((167, 29, 168, 27), 'sklearn.linear_model.lars_path', 'linear_model.lars_path', (), '', False, 'from sklearn import linear_model, datasets\n'), ((169, 22, 170, 46), 'sklearn.linear_model.lars_path', 'linear_model.lars_path', (), '', False, 'from sklearn import linear_model, datasets\n'), ((172, 4, 172, 54), 'sklearn.utils.testing.assert_array_almost_equal', 'assert_array_almost_equal', ({(172, 30, 172, 34): 'coef', (172, 36, 172, 53): 'coef_path_[:, (-1)]'}, {}), '(coef, coef_path_[:, (-1)])', False, 'from sklearn.utils.testing import assert_array_almost_equal\n'), ((178, 29, 179, 35), 'sklearn.linear_model.lars_path', 'linear_model.lars_path', (), '', False, 'from sklearn import linear_model, datasets\n'), ((180, 22, 181, 54), 'sklearn.linear_model.lars_path', 'linear_model.lars_path', (), '', False, 'from sklearn import linear_model, datasets\n'), ((183, 4, 183, 54), 'sklearn.utils.testing.assert_array_almost_equal', 'assert_array_almost_equal', ({(183, 30, 183, 34): 'coef', (183, 36, 183, 53): 'coef_path_[:, (-1)]'}, {}), '(coef, coef_path_[:, (-1)])', False, 'from sklearn.utils.testing import assert_array_almost_equal\n'), ((191, 8, 191, 22), 'numpy.dot', 'np.dot', ({(191, 15, 191, 18): 'X.T', (191, 20, 191, 21): 'X'}, {}), '(X.T, X)', True, 'import numpy as np\n'), ((192, 9, 192, 23), 'numpy.dot', 'np.dot', ({(192, 16, 192, 19): 'X.T', (192, 21, 192, 22): 'y'}, {}), '(X.T, y)', True, 'import numpy as np\n'), ((193, 29, 194, 59), 'sklearn.linear_model.lars_path', 'linear_model.lars_path', (), '', False, 'from sklearn import linear_model, datasets\n'), ((195, 22, 196, 78), 'sklearn.linear_model.lars_path', 'linear_model.lars_path', (), '', False, 'from sklearn import linear_model, datasets\n'), ((198, 4, 198, 54), 'sklearn.utils.testing.assert_array_almost_equal', 'assert_array_almost_equal', ({(198, 30, 198, 34): 'coef', (198, 36, 198, 53): 'coef_path_[:, (-1)]'}, {}), '(coef, coef_path_[:, (-1)])', False, 'from sklearn.utils.testing import assert_array_almost_equal\n'), ((208, 8, 208, 22), 'numpy.dot', 'np.dot', ({(208, 15, 208, 18): 'X.T', (208, 20, 208, 21): 'X'}, {}), '(X.T, X)', True, 'import numpy as np\n'), ((220, 9, 220, 38), 'numpy.array', 'np.array', ({(220, 18, 220, 37): '[[1, 1.0], [1.0, 1.0]]'}, {}), '([[1, 1.0], [1.0, 1.0]])', True, 'import numpy as np\n'), ((221, 9, 221, 25), 'numpy.array', 'np.array', ({(221, 18, 221, 24): '[1, 1]'}, {}), '([1, 1])', True, 'import numpy as np\n'), ((222, 22, 222, 52), 'sklearn.linear_model.lars_path', 'linear_model.lars_path', ({(222, 45, 222, 47): 'X1', (222, 49, 222, 51): 'y1'}, {}), '(X1, y1)', False, 'from sklearn import linear_model, datasets\n'), ((223, 4, 223, 60), 'sklearn.utils.testing.assert_array_almost_equal', 'assert_array_almost_equal', ({(223, 30, 223, 41): 'coef_path.T', (223, 43, 223, 59): '[[0, 0], [1, 0]]'}, {}), '(coef_path.T, [[0, 0], [1, 0]])', False, 'from sklearn.utils.testing import assert_array_almost_equal\n'), ((258, 28, 258, 72), 'sklearn.linear_model.lars_path', 'linear_model.lars_path', (), '', False, 'from sklearn import linear_model, datasets\n'), ((259, 15, 259, 64), 'sklearn.linear_model.Lasso', 'linear_model.Lasso', (), '', False, 'from sklearn import linear_model, datasets\n'), ((269, 17, 269, 48), 'numpy.linspace', 'np.linspace', ({(269, 29, 269, 33): '(0.01)', (269, 35, 269, 43): '(1 - 0.01)', (269, 45, 269, 47): '(20)'}, {}), '(0.01, 1 - 0.01, 20)', True, 'import numpy as np\n'), ((278, 28, 278, 72), 'sklearn.linear_model.lars_path', 'linear_model.lars_path', (), '', False, 'from sklearn import linear_model, datasets\n'), ((279, 15, 280, 43), 'sklearn.linear_model.Lasso', 'linear_model.Lasso', (), '', False, 'from sklearn import linear_model, datasets\n'), ((319, 12, 319, 36), 'sklearn.linear_model.LassoLars', 'linear_model.LassoLars', ({}, {}), '()', False, 'from sklearn import linear_model, datasets\n'), ((321, 13, 321, 59), 'sklearn.linear_model.LassoLars', 'linear_model.LassoLars', (), '', False, 'from sklearn import linear_model, datasets\n'), ((323, 4, 323, 64), 'sklearn.utils.testing.assert_array_almost_equal', 'assert_array_almost_equal', ({(323, 30, 323, 47): 'lasso.alphas_[:3]', (323, 49, 323, 63): 'lasso2.alphas_'}, {}), '(lasso.alphas_[:3], lasso2.alphas_)', False, 'from sklearn.utils.testing import assert_array_almost_equal\n'), ((335, 10, 335, 35), 'numpy.random.RandomState', 'np.random.RandomState', ({(335, 32, 335, 34): '42'}, {}), '(42)', True, 'import numpy as np\n'), ((341, 8, 341, 24), 'numpy.zeros', 'np.zeros', ({(341, 17, 341, 23): '(m, 1)'}, {}), '((m, 1))', True, 'import numpy as np\n'), ((342, 8, 342, 23), 'numpy.arange', 'np.arange', ({(342, 18, 342, 19): '0', (342, 21, 342, 22): 'm'}, {}), '(0, m)', True, 'import numpy as np\n'), ((346, 8, 346, 20), 'numpy.dot', 'np.dot', ({(346, 15, 346, 16): 'X', (346, 18, 346, 19): 'w'}, {}), '(X, w)', True, 'import numpy as np\n'), ((350, 32, 350, 76), 'sklearn.linear_model.lars_path', 'linear_model.lars_path', (), '', False, 'from sklearn import linear_model, datasets\n'), ((352, 24, 355, 68), 'sklearn.linear_model.lasso_path', 'linear_model.lasso_path', (), '', False, 'from sklearn import linear_model, datasets\n'), ((357, 4, 357, 64), 'sklearn.utils.testing.assert_array_almost_equal', 'assert_array_almost_equal', (), '', False, 'from sklearn.utils.testing import assert_array_almost_equal\n'), ((377, 11, 377, 63), 'sklearn.linear_model.LassoLars', 'linear_model.LassoLars', (), '', False, 'from sklearn import linear_model, datasets\n'), ((378, 4, 378, 52), 'sklearn.utils.testing.assert_warns', 'assert_warns', ({(378, 17, 378, 35): 'ConvergenceWarning', (378, 37, 378, 45): 'lars.fit', (378, 47, 378, 48): 'X', (378, 50, 378, 51): 'y'}, {}), '(ConvergenceWarning, lars.fit, X, y)', False, 'from sklearn.utils.testing import assert_warns\n'), ((382, 20, 382, 78), 'sklearn.linear_model.Lasso', 'linear_model.Lasso', (), '', False, 'from sklearn import linear_model, datasets\n'), ((386, 4, 386, 47), 'sklearn.utils.testing.assert_less', 'assert_less', ({(386, 16, 386, 24): 'lars_obj', (386, 26, 386, 46): '(cd_obj * (1.0 + 1e-08))'}, {}), '(lars_obj, cd_obj * (1.0 + 1e-08))', False, 'from sklearn.utils.testing import assert_less\n'), ((401, 11, 401, 64), 'sklearn.linear_model.Lars', 'linear_model.Lars', (), '', False, 'from sklearn import linear_model, datasets\n'), ((444, 14, 444, 40), 'sklearn.linear_model.LassoLarsCV', 'linear_model.LassoLarsCV', ({}, {}), '()', False, 'from sklearn import linear_model, datasets\n'), ((471, 15, 471, 46), 'sklearn.linear_model.LassoLarsIC', 'linear_model.LassoLarsIC', ({(471, 40, 471, 45): '"""bic"""'}, {}), "('bic')", False, 'from sklearn import linear_model, datasets\n'), ((472, 15, 472, 46), 'sklearn.linear_model.LassoLarsIC', 'linear_model.LassoLarsIC', ({(472, 40, 472, 45): '"""aic"""'}, {}), "('aic')", False, 'from sklearn import linear_model, datasets\n'), ((473, 10, 473, 35), 'numpy.random.RandomState', 'np.random.RandomState', ({(473, 32, 473, 34): '42'}, {}), '(42)', True, 'import numpy as np\n'), ((480, 4, 480, 52), 'sklearn.utils.testing.assert_greater', 'assert_greater', ({(480, 19, 480, 34): 'lars_bic.alpha_', (480, 36, 480, 51): 'lars_aic.alpha_'}, {}), '(lars_bic.alpha_, lars_aic.alpha_)', False, 'from sklearn.utils.testing import assert_greater\n'), ((485, 18, 485, 55), 'sklearn.linear_model.LassoLarsIC', 'linear_model.LassoLarsIC', ({(485, 43, 485, 54): '"""<unknown>"""'}, {}), "('<unknown>')", False, 'from sklearn import linear_model, datasets\n'), ((486, 4, 486, 52), 'sklearn.utils.testing.assert_raises', 'assert_raises', ({(486, 18, 486, 28): 'ValueError', (486, 30, 486, 45): 'lars_broken.fit', (486, 47, 486, 48): 'X', (486, 50, 486, 51): 'y'}, {}), '(ValueError, lars_broken.fit, X, y)', False, 'from sklearn.utils.testing import assert_raises\n'), ((494, 20, 494, 59), 'sklearn.model_selection.train_test_split', 'train_test_split', (), '', False, 'from sklearn.model_selection import train_test_split\n'), ((520, 8, 521, 46), 'sklearn.linear_model.lars_path', 'linear_model.lars_path', (), '', False, 'from sklearn import linear_model, datasets\n'), ((525, 8, 526, 45), 'sklearn.linear_model.lars_path', 'linear_model.lars_path', (), '', False, 'from sklearn import linear_model, datasets\n'), ((570, 28, 571, 65), 'sklearn.linear_model.lars_path', 'linear_model.lars_path', (), '', False, 'from sklearn import linear_model, datasets\n'), ((572, 15, 572, 79), 'sklearn.linear_model.Lasso', 'linear_model.Lasso', (), '', False, 'from sklearn import linear_model, datasets\n'), ((590, 17, 590, 48), 'numpy.linspace', 'np.linspace', ({(590, 29, 590, 33): '(0.6)', (590, 35, 590, 43): '(1 - 0.01)', (590, 45, 590, 47): '(20)'}, {}), '(0.6, 1 - 0.01, 20)', True, 'import numpy as np\n'), ((600, 28, 601, 65), 'sklearn.linear_model.lars_path', 'linear_model.lars_path', (), '', False, 'from sklearn import linear_model, datasets\n'), ((602, 15, 603, 58), 'sklearn.linear_model.Lasso', 'linear_model.Lasso', (), '', False, 'from sklearn import linear_model, datasets\n'), ((619, 8, 620, 32), 'numpy.array', 'np.array', ({(619, 17, 620, 31): '[-6.45006793, -3.51251449, -8.52445396, 6.12277822, -19.42109366]'}, {}), '([-6.45006793, -3.51251449, -8.52445396, 6.12277822, -19.42109366])', True, 'import numpy as np\n'), ((621, 8, 626, 32), 'numpy.array', 'np.array', ({(621, 17, 626, 31): '[[0.47299829, 0, 0, 0, 0], [0.08239882, 0.85784863, 0, 0, 0], [0.30114139, \n    -0.07501577, 0.80895216, 0, 0], [-0.01460346, -0.1015233, 0.0407278, \n    0.80338378, 0], [-0.69363927, 0.06754067, 0.18064514, -0.0803561, \n    0.40427291]]'}, {}), '([[0.47299829, 0, 0, 0, 0], [0.08239882, 0.85784863, 0, 0, 0], [\n    0.30114139, -0.07501577, 0.80895216, 0, 0], [-0.01460346, -0.1015233, \n    0.0407278, 0.80338378, 0], [-0.69363927, 0.06754067, 0.18064514, -\n    0.0803561, 0.40427291]])', True, 'import numpy as np\n'), ((643, 8, 658, 41), 'numpy.array', 'np.array', ({(643, 17, 658, 40): '[[0, 0, 0, 0, 0, -79.81036280949903, -83.52878873278283, -83.77765373919071,\n    -83.78415693288893, -84.03339059175666], [0, 0, 0, 0, -\n    0.476624256777266, 0, 0, 0, 0, 0.025219751009936], [0, -\n    3.577397088285891, -4.702795355871871, -7.016748621359461, -\n    7.614898471899412, -0.336938391359179, 0, 0, 0.001213370600853, \n    0.048162321585148], [0, 0, 0, 2.231558436628169, 2.723267514525966, \n    2.811549786389614, 2.813766976061531, 2.817462468949557, \n    2.817368178703816, 2.816221090636795], [0, 0, -1.218422599914637, -\n    3.457726183014808, -4.02130452206071, -45.827461592423745, -\n    47.776608869312305, -47.9115616107464, -47.914845922736234, -\n    48.03956233426572]]'}, {}), '([[0, 0, 0, 0, 0, -79.81036280949903, -83.52878873278283, -\n    83.77765373919071, -83.78415693288893, -84.03339059175666], [0, 0, 0, 0,\n    -0.476624256777266, 0, 0, 0, 0, 0.025219751009936], [0, -\n    3.577397088285891, -4.702795355871871, -7.016748621359461, -\n    7.614898471899412, -0.336938391359179, 0, 0, 0.001213370600853, \n    0.048162321585148], [0, 0, 0, 2.231558436628169, 2.723267514525966, \n    2.811549786389614, 2.813766976061531, 2.817462468949557, \n    2.817368178703816, 2.816221090636795], [0, 0, -1.218422599914637, -\n    3.457726183014808, -4.02130452206071, -45.827461592423745, -\n    47.776608869312305, -47.9115616107464, -47.914845922736234, -\n    48.03956233426572]])', True, 'import numpy as np\n'), ((660, 23, 661, 62), 'sklearn.linear_model.LassoLars', 'linear_model.LassoLars', (), '', False, 'from sklearn import linear_model, datasets\n'), ((665, 4, 665, 55), 'sklearn.utils.testing.assert_array_almost_equal', 'assert_array_almost_equal', (), '', False, 'from sklearn.utils.testing import assert_array_almost_equal\n'), ((685, 9, 691, 41), 'numpy.array', 'np.array', ({(685, 18, 691, 40): '[[0, 0, 0, 0, 0], [0, 0, 0, 8.371887668009453, 19.463768371044026], [0, 0, \n    0, 0, 9.901611055290553], [0, 7.495923132833733, 9.245133544334507, \n    17.389369207545062, 26.9716568156435], [0, 0, -1.569380717440311, -\n    5.924804108067312, -7.996385265061972]]'}, {}), '([[0, 0, 0, 0, 0], [0, 0, 0, 8.371887668009453, 19.463768371044026],\n    [0, 0, 0, 0, 9.901611055290553], [0, 7.495923132833733, \n    9.245133544334507, 17.389369207545062, 26.9716568156435], [0, 0, -\n    1.569380717440311, -5.924804108067312, -7.996385265061972]])', True, 'import numpy as np\n'), ((693, 24, 694, 62), 'sklearn.linear_model.LassoLars', 'linear_model.LassoLars', (), '', False, 'from sklearn import linear_model, datasets\n'), ((704, 4, 704, 57), 'sklearn.utils.testing.assert_array_almost_equal', 'assert_array_almost_equal', (), '', False, 'from sklearn.utils.testing import assert_array_almost_equal\n'), ((715, 17, 715, 61), 'sklearn.linear_model.least_angle.LassoLarsIC', 'LassoLarsIC', (), '', False, 'from sklearn.linear_model.least_angle import _lars_path_residues, LassoLarsIC\n'), ((716, 10, 716, 34), 'numpy.random.RandomState', 'np.random.RandomState', ({(716, 32, 716, 33): '0'}, {}), '(0)', True, 'import numpy as np\n'), ((730, 17, 730, 46), 'sklearn.linear_model.least_angle.LassoLarsIC', 'LassoLarsIC', (), '', False, 'from sklearn.linear_model.least_angle import _lars_path_residues, LassoLarsIC\n'), ((731, 10, 731, 34), 'numpy.random.RandomState', 'np.random.RandomState', ({(731, 32, 731, 33): '0'}, {}), '(0)', True, 'import numpy as np\n'), ((39, 21, 39, 31), 'io.StringIO', 'StringIO', ({}, {}), '()', False, 'from io import StringIO\n'), ((41, 27, 42, 43), 'sklearn.linear_model.lars_path', 'linear_model.lars_path', (), '', False, 'from sklearn import linear_model, datasets\n'), ((69, 14, 69, 30), 'numpy.dot', 'np.dot', ({(69, 21, 69, 24): 'X.T', (69, 26, 69, 29): 'res'}, {}), '(X.T, res)', True, 'import numpy as np\n'), ((83, 8, 83, 31), 'sklearn.utils.testing.assert_allclose', 'assert_allclose', ({(83, 24, 83, 26): 'o1', (83, 28, 83, 30): 'o2'}, {}), '(o1, o2)', False, 'from sklearn.utils.testing import assert_allclose\n'), ((90, 8, 92, 36), 'sklearn.linear_model.lars_path_gram', 'linear_model.lars_path_gram', (), '', False, 'from sklearn import linear_model, datasets\n'), ((93, 8, 95, 36), 'sklearn.linear_model.lars_path', 'linear_model.lars_path', (), '', False, 'from sklearn import linear_model, datasets\n'), ((110, 17, 110, 60), 'sklearn.linear_model.lars_path', 'linear_model.lars_path', (), '', False, 'from sklearn import linear_model, datasets\n'), ((111, 21, 112, 58), 'sklearn.linear_model.lars_path', 'linear_model.lars_path', (), '', False, 'from sklearn import linear_model, datasets\n'), ((127, 17, 127, 52), 'numpy.linalg.lstsq', 'np.linalg.lstsq', (), '', True, 'import numpy as np\n'), ((137, 17, 137, 38), 'numpy.linalg.lstsq', 'np.linalg.lstsq', ({(137, 33, 137, 34): 'X', (137, 36, 137, 37): 'y'}, {}), '(X, y)', True, 'import numpy as np\n'), ((152, 15, 152, 43), 'numpy.dot', 'np.dot', ({(152, 22, 152, 23): 'X', (152, 25, 152, 42): 'coef_path_[:, (-1)]'}, {}), '(X, coef_path_[:, (-1)])', True, 'import numpy as np\n'), ((162, 42, 162, 67), 'numpy.zeros_like', 'np.zeros_like', ({(162, 56, 162, 66): 'coef_path_'}, {}), '(coef_path_)', True, 'import numpy as np\n'), ((215, 8, 215, 64), 'sklearn.utils.testing.assert_array_almost_equal', 'assert_array_almost_equal', (), '', False, 'from sklearn.utils.testing import assert_array_almost_equal\n'), ((241, 15, 241, 58), 'sklearn.linear_model.LassoLars', 'linear_model.LassoLars', (), '', False, 'from sklearn import linear_model, datasets\n'), ((246, 24, 246, 73), 'sklearn.linear_model.Lasso', 'linear_model.Lasso', (), '', False, 'from sklearn import linear_model, datasets\n'), ((250, 8, 250, 51), 'sklearn.utils.testing.assert_less', 'assert_less', ({(250, 20, 250, 28): 'obj_lars', (250, 30, 250, 50): '(obj_cd * (1.0 + 1e-08))'}, {}), '(obj_lars, obj_cd * (1.0 + 1e-08))', False, 'from sklearn.utils.testing import assert_less\n'), ((265, 16, 265, 47), 'scipy.linalg.norm', 'linalg.norm', ({(265, 28, 265, 46): 'c - lasso_cd.coef_'}, {}), '(c - lasso_cd.coef_)', False, 'from scipy import linalg\n'), ((266, 8, 266, 32), 'sklearn.utils.testing.assert_less', 'assert_less', ({(266, 20, 266, 25): 'error', (266, 27, 266, 31): '(0.01)'}, {}), '(error, 0.01)', False, 'from sklearn.utils.testing import assert_less\n'), ((273, 14, 273, 50), 'scipy.linalg.norm', 'linalg.norm', ({(273, 26, 273, 49): 'clf1.coef_ - clf2.coef_'}, {}), '(clf1.coef_ - clf2.coef_)', False, 'from scipy import linalg\n'), ((274, 8, 274, 30), 'sklearn.utils.testing.assert_less', 'assert_less', ({(274, 20, 274, 23): 'err', (274, 25, 274, 29): '(0.001)'}, {}), '(err, 0.001)', False, 'from sklearn.utils.testing import assert_less\n'), ((286, 16, 286, 47), 'scipy.linalg.norm', 'linalg.norm', ({(286, 28, 286, 46): 'c - lasso_cd.coef_'}, {}), '(c - lasso_cd.coef_)', False, 'from scipy import linalg\n'), ((287, 8, 287, 32), 'sklearn.utils.testing.assert_less', 'assert_less', ({(287, 20, 287, 25): 'error', (287, 27, 287, 31): '(0.01)'}, {}), '(error, 0.01)', False, 'from sklearn.utils.testing import assert_less\n'), ((297, 32, 298, 75), 'sklearn.linear_model.lars_path', 'linear_model.lars_path', (), '', False, 'from sklearn import linear_model, datasets\n'), ((299, 19, 299, 68), 'sklearn.linear_model.Lasso', 'linear_model.Lasso', (), '', False, 'from sklearn import linear_model, datasets\n'), ((302, 16, 302, 63), 'scipy.linalg.norm', 'linalg.norm', ({(302, 28, 302, 62): 'lasso_path[:, (-1)] - lasso_cd.coef_'}, {}), '(lasso_path[:, (-1)] - lasso_cd.coef_)', False, 'from scipy import linalg\n'), ((303, 8, 303, 32), 'sklearn.utils.testing.assert_less', 'assert_less', ({(303, 20, 303, 25): 'error', (303, 27, 303, 31): '(0.01)'}, {}), '(error, 0.01)', False, 'from sklearn.utils.testing import assert_less\n'), ((307, 32, 308, 75), 'sklearn.linear_model.lars_path', 'linear_model.lars_path', (), '', False, 'from sklearn import linear_model, datasets\n'), ((309, 19, 310, 47), 'sklearn.linear_model.Lasso', 'linear_model.Lasso', (), '', False, 'from sklearn import linear_model, datasets\n'), ((313, 16, 313, 63), 'scipy.linalg.norm', 'linalg.norm', ({(313, 28, 313, 62): 'lasso_path[:, (-1)] - lasso_cd.coef_'}, {}), '(lasso_path[:, (-1)] - lasso_cd.coef_)', False, 'from scipy import linalg\n'), ((314, 8, 314, 32), 'sklearn.utils.testing.assert_less', 'assert_less', ({(314, 20, 314, 25): 'error', (314, 27, 314, 31): '(0.01)'}, {}), '(error, 0.01)', False, 'from sklearn.utils.testing import assert_less\n'), ((396, 11, 396, 23), 'numpy.arange', 'np.arange', ({(396, 21, 396, 22): 'n'}, {}), '(n)', True, 'import numpy as np\n'), ((397, 18, 397, 40), 'numpy.isfinite', 'np.isfinite', ({(397, 30, 397, 39): 'clf.coef_'}, {}), '(clf.coef_)', True, 'import numpy as np\n'), ((412, 8, 412, 30), 'numpy.vstack', 'np.vstack', ({(412, 18, 412, 29): '[y, y ** 2]'}, {}), '([y, y ** 2])', True, 'import numpy as np\n'), ((415, 8, 415, 32), 'sklearn.linear_model.LassoLars', 'linear_model.LassoLars', ({}, {}), '()', False, 'from sklearn import linear_model, datasets\n'), ((416, 8, 416, 27), 'sklearn.linear_model.Lars', 'linear_model.Lars', ({}, {}), '()', False, 'from sklearn import linear_model, datasets\n'), ((418, 8, 418, 51), 'sklearn.linear_model.LassoLars', 'linear_model.LassoLars', (), '', False, 'from sklearn import linear_model, datasets\n'), ((419, 8, 419, 46), 'sklearn.linear_model.Lars', 'linear_model.Lars', (), '', False, 'from sklearn import linear_model, datasets\n'), ((449, 8, 449, 63), 'numpy.testing.assert_array_less', 'np.testing.assert_array_less', ({(449, 37, 449, 46): 'old_alpha', (449, 48, 449, 62): 'lars_cv.alpha_'}, {}), '(old_alpha, lars_cv.alpha_)', True, 'import numpy as np\n'), ((456, 9, 456, 45), 'warnings.catch_warnings', 'warnings.catch_warnings', (), '', False, 'import warnings\n'), ((457, 14, 457, 39), 'numpy.random.RandomState', 'np.random.RandomState', ({(457, 36, 457, 38): '42'}, {}), '(42)', True, 'import numpy as np\n'), ((461, 18, 461, 54), 'sklearn.linear_model.LassoLarsCV', 'linear_model.LassoLarsCV', (), '', False, 'from sklearn import linear_model, datasets\n'), ((478, 18, 478, 42), 'numpy.where', 'np.where', ({(478, 27, 478, 41): 'lars_bic.coef_'}, {}), '(lars_bic.coef_)', True, 'import numpy as np\n'), ((479, 18, 479, 42), 'numpy.where', 'np.where', ({(479, 27, 479, 41): 'lars_aic.coef_'}, {}), '(lars_aic.coef_)', True, 'import numpy as np\n'), ((482, 16, 482, 35), 'numpy.max', 'np.max', ({(482, 23, 482, 34): 'nonzero_bic'}, {}), '(nonzero_bic)', True, 'import numpy as np\n'), ((495, 9, 495, 34), 'sklearn.utils.testing.TempMemmap', 'TempMemmap', ({(495, 20, 495, 33): 'splitted_data'}, {}), '(splitted_data)', False, 'from sklearn.utils.testing import TempMemmap\n'), ((497, 8, 497, 73), 'sklearn.linear_model.least_angle._lars_path_residues', '_lars_path_residues', (), '', False, 'from sklearn.linear_model.least_angle import _lars_path_residues, LassoLarsIC\n'), ((514, 9, 514, 57), 'pytest.warns', 'pytest.warns', (), '', False, 'import pytest\n'), ((515, 8, 517, 45), 'sklearn.linear_model.lars_path', 'linear_model.lars_path', (), '', False, 'from sklearn import linear_model, datasets\n'), ((578, 16, 578, 47), 'scipy.linalg.norm', 'linalg.norm', ({(578, 28, 578, 46): 'c - lasso_cd.coef_'}, {}), '(c - lasso_cd.coef_)', False, 'from scipy import linalg\n'), ((579, 8, 579, 32), 'sklearn.utils.testing.assert_less', 'assert_less', ({(579, 20, 579, 25): 'error', (579, 27, 579, 31): '(0.01)'}, {}), '(error, 0.01)', False, 'from sklearn.utils.testing import assert_less\n'), ((595, 14, 595, 50), 'scipy.linalg.norm', 'linalg.norm', ({(595, 26, 595, 49): 'clf1.coef_ - clf2.coef_'}, {}), '(clf1.coef_ - clf2.coef_)', False, 'from scipy import linalg\n'), ((596, 8, 596, 30), 'sklearn.utils.testing.assert_less', 'assert_less', ({(596, 20, 596, 23): 'err', (596, 25, 596, 29): '(0.001)'}, {}), '(err, 0.001)', False, 'from sklearn.utils.testing import assert_less\n'), ((607, 16, 607, 47), 'scipy.linalg.norm', 'linalg.norm', ({(607, 28, 607, 46): 'c - lasso_cd.coef_'}, {}), '(c - lasso_cd.coef_)', False, 'from scipy import linalg\n'), ((608, 8, 608, 32), 'sklearn.utils.testing.assert_less', 'assert_less', ({(608, 20, 608, 25): 'error', (608, 27, 608, 31): '(0.01)'}, {}), '(error, 0.01)', False, 'from sklearn.utils.testing import assert_less\n'), ((700, 15, 700, 33), 'numpy.mean', 'np.mean', (), '', True, 'import numpy as np\n'), ((701, 20, 701, 45), 'numpy.sum', 'np.sum', (), '', True, 'import numpy as np\n'), ((721, 21, 721, 46), 'numpy.array_equal', 'np.array_equal', ({(721, 36, 721, 37): 'X', (721, 39, 721, 45): 'X_copy'}, {}), '(X, X_copy)', True, 'import numpy as np\n'), ((736, 21, 736, 46), 'numpy.array_equal', 'np.array_equal', ({(736, 36, 736, 37): 'X', (736, 39, 736, 45): 'X_copy'}, {}), '(X, X_copy)', True, 'import numpy as np\n'), ((48, 18, 48, 34), 'numpy.dot', 'np.dot', ({(48, 25, 48, 28): 'X.T', (48, 30, 48, 33): 'res'}, {}), '(X.T, res)', True, 'import numpy as np\n'), ((68, 18, 68, 34), 'numpy.dot', 'np.dot', ({(68, 25, 68, 26): 'X', (68, 28, 68, 33): 'coef_'}, {}), '(X, coef_)', True, 'import numpy as np\n'), ((114, 12, 114, 52), 'sklearn.utils.testing.assert_array_almost_equal', 'assert_array_almost_equal', ({(114, 38, 114, 46): 'expected', (114, 48, 114, 51): 'got'}, {}), '(expected, got)', False, 'from sklearn.utils.testing import assert_array_almost_equal\n'), ((126, 20, 126, 48), 'distutils.version.LooseVersion', 'LooseVersion', ({(126, 33, 126, 47): 'np.__version__'}, {}), '(np.__version__)', False, 'from distutils.version import LooseVersion\n'), ((211, 15, 211, 39), 'sklearn.utils.testing.ignore_warnings', 'ignore_warnings', ({(211, 31, 211, 38): 'clf.fit'}, {}), '(clf.fit)', False, 'from sklearn.utils.testing import ignore_warnings\n'), ((325, 18, 325, 40), 'numpy.diff', 'np.diff', ({(325, 26, 325, 39): 'lasso.alphas_'}, {}), '(lasso.alphas_)', True, 'import numpy as np\n'), ((394, 14, 394, 33), 'numpy.arange', 'np.arange', ({(394, 24, 394, 25): '(1)', (394, 27, 394, 32): '(n + 1)'}, {}), '(1, n + 1)', True, 'import numpy as np\n'), ((395, 10, 395, 48), 'sklearn.linear_model.Lars', 'linear_model.Lars', (), '', False, 'from sklearn import linear_model, datasets\n'), ((430, 12, 430, 67), 'sklearn.utils.testing.assert_array_almost_equal', 'assert_array_almost_equal', ({(430, 38, 430, 47): 'alphas[k]', (430, 49, 430, 66): 'estimator.alphas_'}, {}), '(alphas[k], estimator.alphas_)', False, 'from sklearn.utils.testing import assert_array_almost_equal\n'), ((431, 12, 431, 67), 'sklearn.utils.testing.assert_array_almost_equal', 'assert_array_almost_equal', ({(431, 38, 431, 47): 'active[k]', (431, 49, 431, 66): 'estimator.active_'}, {}), '(active[k], estimator.active_)', False, 'from sklearn.utils.testing import assert_array_almost_equal\n'), ((432, 12, 432, 63), 'sklearn.utils.testing.assert_array_almost_equal', 'assert_array_almost_equal', ({(432, 38, 432, 45): 'coef[k]', (432, 47, 432, 62): 'estimator.coef_'}, {}), '(coef[k], estimator.coef_)', False, 'from sklearn.utils.testing import assert_array_almost_equal\n'), ((433, 12, 433, 68), 'sklearn.utils.testing.assert_array_almost_equal', 'assert_array_almost_equal', ({(433, 38, 433, 45): 'path[k]', (433, 47, 433, 67): 'estimator.coef_path_'}, {}), '(path[k], estimator.coef_path_)', False, 'from sklearn.utils.testing import assert_array_almost_equal\n'), ((434, 12, 434, 59), 'sklearn.utils.testing.assert_array_almost_equal', 'assert_array_almost_equal', ({(434, 38, 434, 50): 'Y_pred[:, (k)]', (434, 52, 434, 58): 'y_pred'}, {}), '(Y_pred[:, (k)], y_pred)', False, 'from sklearn.utils.testing import assert_array_almost_equal\n'), ((47, 22, 47, 38), 'numpy.dot', 'np.dot', ({(47, 29, 47, 30): 'X', (47, 32, 47, 37): 'coef_'}, {}), '(X, coef_)', True, 'import numpy as np\n'), ((151, 15, 151, 35), 'numpy.isnan', 'np.isnan', ({(151, 24, 151, 34): 'coef_path_'}, {}), '(coef_path_)', True, 'import numpy as np\n'), ((245, 27, 245, 53), 'scipy.linalg.norm', 'linalg.norm', ({(245, 39, 245, 49): 'coef_lars_', (245, 51, 245, 52): '(1)'}, {}), '(coef_lars_, 1)', False, 'from scipy import linalg\n'), ((249, 25, 249, 49), 'scipy.linalg.norm', 'linalg.norm', ({(249, 37, 249, 45): 'coef_cd_', (249, 47, 249, 48): '(1)'}, {}), '(coef_cd_, 1)', False, 'from scipy import linalg\n'), ((270, 15, 270, 67), 'sklearn.linear_model.LassoLars', 'linear_model.LassoLars', (), '', False, 'from sklearn import linear_model, datasets\n'), ((271, 15, 272, 50), 'sklearn.linear_model.Lasso', 'linear_model.Lasso', (), '', False, 'from sklearn import linear_model, datasets\n'), ((375, 26, 375, 46), 'scipy.linalg.norm', 'linalg.norm', ({(375, 38, 375, 42): 'coef', (375, 44, 375, 45): '(1)'}, {}), '(coef, 1)', False, 'from scipy import linalg\n'), ((394, 36, 394, 48), 'numpy.arange', 'np.arange', ({(394, 46, 394, 47): 'n'}, {}), '(n)', True, 'import numpy as np\n'), ((591, 15, 592, 69), 'sklearn.linear_model.LassoLars', 'linear_model.LassoLars', (), '', False, 'from sklearn import linear_model, datasets\n'), ((593, 15, 594, 65), 'sklearn.linear_model.Lasso', 'linear_model.Lasso', (), '', False, 'from sklearn import linear_model, datasets\n'), ((244, 38, 244, 59), 'numpy.dot', 'np.dot', ({(244, 45, 244, 46): 'X', (244, 48, 244, 58): 'coef_lars_'}, {}), '(X, coef_lars_)', True, 'import numpy as np\n'), ((248, 53, 248, 72), 'numpy.dot', 'np.dot', ({(248, 60, 248, 61): 'X', (248, 63, 248, 71): 'coef_cd_'}, {}), '(X, coef_cd_)', True, 'import numpy as np\n'), ((374, 53, 374, 68), 'numpy.dot', 'np.dot', ({(374, 60, 374, 61): 'X', (374, 63, 374, 67): 'coef'}, {}), '(X, coef)', True, 'import numpy as np\n')]