# Buggy code
def _lars_path_residues(
    X_train,
    y_train,
    X_test,
    y_test,
    Gram=None,
    copy=True,
    method="lars",
    verbose=False,
    fit_intercept=True,
    normalize=True,
    max_iter=500,
    eps=np.finfo(np.float).eps,
):
    """Compute the residues on left-out data for a full LARS path

    Parameters
    -----------
    X_train : array, shape (n_samples, n_features)
        The data to fit the LARS on
    y_train : array, shape (n_samples)
        The target variable to fit LARS on
    X_test : array, shape (n_samples, n_features)
        The data to compute the residues on
    y_test : array, shape (n_samples)
        The target variable to compute the residues on
    Gram : None, 'auto', array, shape: (n_features, n_features), optional
        Precomputed Gram matrix (X' * X), if ``'auto'``, the Gram
        matrix is precomputed from the given X, if there are more samples
        than features
    copy : boolean, optional
        Whether X_train, X_test, y_train and y_test should be copied;
        if False, they may be overwritten.
    method : 'lar' | 'lasso'
        Specifies the returned model. Select ``'lar'`` for Least Angle
        Regression, ``'lasso'`` for the Lasso.
    verbose : integer, optional
        Sets the amount of verbosity
    fit_intercept : boolean
        whether to calculate the intercept for this model. If set
        to false, no intercept will be used in calculations
        (e.g. data is expected to be already centered).
    normalize : boolean, optional, default False
        If True, the regressors X will be normalized before regression.
    max_iter : integer, optional
        Maximum number of iterations to perform.
    eps : float, optional
        The machine-precision regularization in the computation of the
        Cholesky diagonal factors. Increase this for very ill-conditioned
        systems. Unlike the ``tol`` parameter in some iterative
        optimization-based algorithms, this parameter does not control
        the tolerance of the optimization.


    Returns
    --------
    alphas : array, shape (n_alphas,)
        Maximum of covariances (in absolute value) at each iteration.
        ``n_alphas`` is either ``max_iter`` or ``n_features``, whichever
        is smaller.

    active : list
        Indices of active variables at the end of the path.

    coefs : array, shape (n_features, n_alphas)
        Coefficients along the path

    residues : array, shape (n_alphas, n_samples)
        Residues of the prediction on the test data
    """
    if copy:
        X_train = X_train.copy()
        y_train = y_train.copy()
        X_test = X_test.copy()
        y_test = y_test.copy()

    if fit_intercept:
        X_mean = X_train.mean(axis=0)
        X_train -= X_mean
        X_test -= X_mean
        y_mean = y_train.mean(axis=0)
        y_train = as_float_array(y_train, copy=False)
        y_train -= y_mean
        y_test = as_float_array(y_test, copy=False)
        y_test -= y_mean

    if normalize:
        norms = np.sqrt(np.sum(X_train**2, axis=0))
        nonzeros = np.flatnonzero(norms)
        X_train[:, nonzeros] /= norms[nonzeros]

    alphas, active, coefs = lars_path(
        X_train,
        y_train,
        Gram=Gram,
        copy_X=False,
        copy_Gram=False,
        method=method,
        verbose=max(0, verbose - 1),
        max_iter=max_iter,
        eps=eps,
    )
    if normalize:
        coefs[nonzeros] /= norms[nonzeros][:, np.newaxis]
    residues = np.dot(X_test, coefs) - y_test[:, np.newaxis]
    return alphas, active, coefs, residues.T


# Traceback (ValueError)
n,p = 2194, 12277
X = np.random.normal(0, 1, (n, p))
y = X[:,1] + X[:,2] + np.random.normal(0,1, n)


l1 = LassoLarsCV(n_jobs=1).fit(X,y)
print(l1.coef_)
l = LassoLarsCV(n_jobs=-1).fit(X,y)
print(l.coef_)

OUTPUT



[ 0.          0.91122441  0.88051206 ...,  0.          0.          0.        ]
multiprocessing.pool.RemoteTraceback:
"""
Traceback (most recent call last):
File "/usr/local/lib/python3.4/dist-packages/sklearn/externals/joblib/parallel.py", line 94, in __call__
return self.func(*args, **kwargs)
File "/usr/local/lib/python3.4/dist-packages/sklearn/linear_model/least_angle.py", line 853, in _lars_path_residues
X_train -= X_mean
ValueError: output array is read-only

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/usr/lib/python3.4/multiprocessing/pool.py", line 119, in worker
result = (True, func(*args, **kwds))
File "/usr/local/lib/python3.4/dist-packages/sklearn/externals/joblib/parallel.py", line 104, in __call__
raise TransportableException(text, e_type)
sklearn.externals.joblib.my_exceptions.TransportableException: TransportableException
___________________________________________________________________________
ValueError                                         Wed Apr 15 14:14:46 2015
PID: 4123                                    Python 3.4.0: /usr/bin/python3
...........................................................................
/usr/local/lib/python3.4/dist-packages/sklearn/linear_model/least_angle.py in _lars_path_residues(X_train=memmap([[ 1.00577915, -1.05126903,  1.30304231, ... -0.35100295,
-0.9267396 ,  0.63928884]]), y_train=array([ 1.3232542 ,  0.84262663, -2.62905025, ...,  1.62472897,
-1.45902494, -5.00504709]), X_test=array([[-0.31256648, -0.8413869 ,  0.46347277, ....  1.21977537,
-1.12781956, -1.12695671]]), y_test=array([ -1.91413301e-02,  -7.90822410e-01,  -2.1...2352138e-01,  -2.06750561e+00,  -3.79778359e-01]), Gram='auto', copy=False, method='lasso', verbose=0, fit_intercept=True, normalize=True, max_iter=500, eps=2.2204460492503131e-16)
848         X_test = X_test.copy()
849         y_test = y_test.copy()
850
851     if fit_intercept:
852         X_mean = X_train.mean(axis=0)
--> 853         X_train -= X_mean
X_train = memmap([[ 1.00577915, -1.05126903,  1.30304231, ... -0.35100295,
-0.9267396 ,  0.63928884]])
X_mean = memmap([-0.01531417,  0.02889734, -0.01592266, ...,  0.00315228,
-0.00060194,  0.01170645])
854         X_test -= X_mean
855         y_mean = y_train.mean(axis=0)
856         y_train = as_float_array(y_train, copy=False)
857         y_train -= y_mean

ValueError: output array is read-only
___________________________________________________________________________
"""

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "/usr/local/lib/python3.4/dist-packages/sklearn/externals/joblib/parallel.py", line 518, in retrieve
self._output.append(job.get())
File "/usr/lib/python3.4/multiprocessing/pool.py", line 599, in get
raise self._value
sklearn.externals.joblib.my_exceptions.TransportableException: TransportableException
___________________________________________________________________________
ValueError                                         Wed Apr 15 14:14:46 2015
PID: 4123                                    Python 3.4.0: /usr/bin/python3
...........................................................................
/usr/local/lib/python3.4/dist-packages/sklearn/linear_model/least_angle.py in _lars_path_residues(X_train=memmap([[ 1.00577915, -1.05126903,  1.30304231, ... -0.35100295,
-0.9267396 ,  0.63928884]]), y_train=array([ 1.3232542 ,  0.84262663, -2.62905025, ...,  1.62472897,
-1.45902494, -5.00504709]), X_test=array([[-0.31256648, -0.8413869 ,  0.46347277, ....  1.21977537,
-1.12781956, -1.12695671]]), y_test=array([ -1.91413301e-02,  -7.90822410e-01,  -2.1...2352138e-01,  -2.06750561e+00,  -3.79778359e-01]), Gram='auto', copy=False, method='lasso', verbose=0, fit_intercept=True, normalize=True, max_iter=500, eps=2.2204460492503131e-16)
848         X_test = X_test.copy()
849         y_test = y_test.copy()
850
851     if fit_intercept:
852         X_mean = X_train.mean(axis=0)
--> 853         X_train -= X_mean
X_train = memmap([[ 1.00577915, -1.05126903,  1.30304231, ... -0.35100295,
-0.9267396 ,  0.63928884]])
X_mean = memmap([-0.01531417,  0.02889734, -0.01592266, ...,  0.00315228,
-0.00060194,  0.01170645])
854         X_test -= X_mean
855         y_mean = y_train.mean(axis=0)
856         y_train = as_float_array(y_train, copy=False)
857         y_train -= y_mean

ValueError: output array is read-only
___________________________________________________________________________

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/donbeo/Desktop/prova.py", line 31, in <module>
l = LassoLarsCV(n_jobs=-1).fit(X,y)
File "/usr/local/lib/python3.4/dist-packages/sklearn/linear_model/least_angle.py", line 999, in fit
for train, test in cv)
File "/usr/local/lib/python3.4/dist-packages/sklearn/externals/joblib/parallel.py", line 666, in __call__
self.retrieve()
File "/usr/local/lib/python3.4/dist-packages/sklearn/externals/joblib/parallel.py", line 549, in retrieve
raise exception_type(report)
sklearn.externals.joblib.my_exceptions.JoblibValueError: JoblibValueError
___________________________________________________________________________
Multiprocessing exception:
...........................................................................
/home/donbeo/Desktop/<stdin> in <module>()
----> 1
2
3
4
5
6
7
8
9
10

...........................................................................
/home/donbeo/Desktop/prova.py in <module>()
26 y = X[:,1] + X[:,2] + np.random.normal(0,1, n)
27
28
29 l1 = LassoLarsCV(n_jobs=1).fit(X,y)
30 print(l1.coef_)
---> 31 l = LassoLarsCV(n_jobs=-1).fit(X,y)
32 print(l.coef_)
33
34
35

...........................................................................
/usr/local/lib/python3.4/dist-packages/sklearn/linear_model/least_angle.py in fit(self=LassoLarsCV(copy_X=True, cv=None, eps=2.22044604...normalize=True, precompute='auto', verbose=False), X=array([[-0.31256648, -0.8413869 ,  0.46347277, .... -0.35100295,
-0.9267396 ,  0.63928884]]), y=array([-0.01914133, -0.79082241, -2.15777265, ...,  1.62472897,
-1.45902494, -5.00504709]))
994             delayed(_lars_path_residues)(
995                 X[train], y[train], X[test], y[test], Gram=Gram, copy=False,
996                 method=self.method, verbose=max(0, self.verbose - 1),
997                 normalize=self.normalize, fit_intercept=self.fit_intercept,
998                 max_iter=self.max_iter, eps=self.eps)
--> 999             for train, test in cv)
cv = sklearn.cross_validation.KFold(n=2194, n_folds=3, shuffle=False, random_state=None)
1000         all_alphas = np.concatenate(list(zip(*cv_paths))[0])
1001         # Unique also sorts
1002         all_alphas = np.unique(all_alphas)
1003         # Take at most max_n_alphas values

...........................................................................
/usr/local/lib/python3.4/dist-packages/sklearn/externals/joblib/parallel.py in __call__(self=Parallel(n_jobs=-1), iterable=<generator object <genexpr>>)
661             if pre_dispatch == "all" or n_jobs == 1:
662                 # The iterable was consumed all at once by the above for loop.
663                 # No need to wait for async callbacks to trigger to
664                 # consumption.
665                 self._iterating = False
--> 666             self.retrieve()
self.retrieve = <bound method Parallel.retrieve of Parallel(n_jobs=-1)>
667             # Make sure that we get a last message telling us we are done
668             elapsed_time = time.time() - self._start_time
669             self._print('Done %3i out of %3i | elapsed: %s finished',
670                         (len(self._output),

---------------------------------------------------------------------------
Sub-process traceback:
---------------------------------------------------------------------------
ValueError                                         Wed Apr 15 14:14:46 2015
PID: 4123                                    Python 3.4.0: /usr/bin/python3
...........................................................................
/usr/local/lib/python3.4/dist-packages/sklearn/linear_model/least_angle.py in _lars_path_residues(X_train=memmap([[ 1.00577915, -1.05126903,  1.30304231, ... -0.35100295,
-0.9267396 ,  0.63928884]]), y_train=array([ 1.3232542 ,  0.84262663, -2.62905025, ...,  1.62472897,
-1.45902494, -5.00504709]), X_test=array([[-0.31256648, -0.8413869 ,  0.46347277, ....  1.21977537,
-1.12781956, -1.12695671]]), y_test=array([ -1.91413301e-02,  -7.90822410e-01,  -2.1...2352138e-01,  -2.06750561e+00,  -3.79778359e-01]), Gram='auto', copy=False, method='lasso', verbose=0, fit_intercept=True, normalize=True, max_iter=500, eps=2.2204460492503131e-16)
848         X_test = X_test.copy()
849         y_test = y_test.copy()
850
851     if fit_intercept:
852         X_mean = X_train.mean(axis=0)
--> 853         X_train -= X_mean
X_train = memmap([[ 1.00577915, -1.05126903,  1.30304231, ... -0.35100295,
-0.9267396 ,  0.63928884]])
X_mean = memmap([-0.01531417,  0.02889734, -0.01592266, ...,  0.00315228,
-0.00060194,  0.01170645])
854         X_test -= X_mean
855         y_mean = y_train.mean(axis=0)
856         y_train = as_float_array(y_train, copy=False)
857         y_train -= y_mean

ValueError: output array is read-only
___________________________________________________________________________
>>>

# Fixed code
def _lars_path_residues(
    X_train,
    y_train,
    X_test,
    y_test,
    Gram=None,
    copy=True,
    method="lars",
    verbose=False,
    fit_intercept=True,
    normalize=True,
    max_iter=500,
    eps=np.finfo(np.float).eps,
):
    """Compute the residues on left-out data for a full LARS path

    Parameters
    -----------
    X_train : array, shape (n_samples, n_features)
        The data to fit the LARS on
    y_train : array, shape (n_samples)
        The target variable to fit LARS on
    X_test : array, shape (n_samples, n_features)
        The data to compute the residues on
    y_test : array, shape (n_samples)
        The target variable to compute the residues on
    Gram : None, 'auto', array, shape: (n_features, n_features), optional
        Precomputed Gram matrix (X' * X), if ``'auto'``, the Gram
        matrix is precomputed from the given X, if there are more samples
        than features
    copy : boolean, optional
        Whether X_train, X_test, y_train and y_test should be copied;
        if False, they may be overwritten.
    method : 'lar' | 'lasso'
        Specifies the returned model. Select ``'lar'`` for Least Angle
        Regression, ``'lasso'`` for the Lasso.
    verbose : integer, optional
        Sets the amount of verbosity
    fit_intercept : boolean
        whether to calculate the intercept for this model. If set
        to false, no intercept will be used in calculations
        (e.g. data is expected to be already centered).
    normalize : boolean, optional, default False
        If True, the regressors X will be normalized before regression.
    max_iter : integer, optional
        Maximum number of iterations to perform.
    eps : float, optional
        The machine-precision regularization in the computation of the
        Cholesky diagonal factors. Increase this for very ill-conditioned
        systems. Unlike the ``tol`` parameter in some iterative
        optimization-based algorithms, this parameter does not control
        the tolerance of the optimization.


    Returns
    --------
    alphas : array, shape (n_alphas,)
        Maximum of covariances (in absolute value) at each iteration.
        ``n_alphas`` is either ``max_iter`` or ``n_features``, whichever
        is smaller.

    active : list
        Indices of active variables at the end of the path.

    coefs : array, shape (n_features, n_alphas)
        Coefficients along the path

    residues : array, shape (n_alphas, n_samples)
        Residues of the prediction on the test data
    """
    X_train = _check_copy_and_writeable(X_train, copy)
    y_train = _check_copy_and_writeable(y_train, copy)
    X_test = _check_copy_and_writeable(X_test, copy)
    y_test = _check_copy_and_writeable(y_test, copy)

    if fit_intercept:
        X_mean = X_train.mean(axis=0)
        X_train -= X_mean
        X_test -= X_mean
        y_mean = y_train.mean(axis=0)
        y_train = as_float_array(y_train, copy=False)
        y_train -= y_mean
        y_test = as_float_array(y_test, copy=False)
        y_test -= y_mean

    if normalize:
        norms = np.sqrt(np.sum(X_train**2, axis=0))
        nonzeros = np.flatnonzero(norms)
        X_train[:, nonzeros] /= norms[nonzeros]

    alphas, active, coefs = lars_path(
        X_train,
        y_train,
        Gram=Gram,
        copy_X=False,
        copy_Gram=False,
        method=method,
        verbose=max(0, verbose - 1),
        max_iter=max_iter,
        eps=eps,
    )
    if normalize:
        coefs[nonzeros] /= norms[nonzeros][:, np.newaxis]
    residues = np.dot(X_test, coefs) - y_test[:, np.newaxis]
    return alphas, active, coefs, residues.T