scikit-learn/sklearn/linear_model/least_angle.py

1445 lines
53 KiB
Python
Raw Normal View History

2010-09-12 00:45:30 +08:00
"""
Least Angle Regression algorithm. See the documentation on the
Generalized Linear Model for a complete discussion.
"""
from __future__ import print_function
2010-09-12 00:45:30 +08:00
# Author: Fabian Pedregosa <fabian.pedregosa@inria.fr>
# Alexandre Gramfort <alexandre.gramfort@inria.fr>
# Gael Varoquaux
#
# License: BSD 3 clause
2011-08-08 06:22:49 +08:00
from math import log
2012-03-24 19:27:45 +08:00
import sys
import warnings
from distutils.version import LooseVersion
2012-03-24 19:27:45 +08:00
import numpy as np
2011-06-18 22:20:20 +08:00
from scipy import linalg, interpolate
from scipy.linalg.lapack import get_lapack_funcs
2010-09-30 22:14:21 +08:00
2010-09-12 00:45:30 +08:00
from .base import LinearModel
2012-06-13 12:26:02 +08:00
from ..base import RegressorMixin
from ..utils import arrayfuncs, as_float_array, check_X_y
from ..cross_validation import check_cv
from ..exceptions import ConvergenceWarning
2011-06-18 22:20:20 +08:00
from ..externals.joblib import Parallel, delayed
2013-02-14 09:05:35 +08:00
from ..externals.six.moves import xrange
from ..externals.six import string_types
import scipy
solve_triangular_args = {}
if LooseVersion(scipy.__version__) >= LooseVersion('0.12'):
solve_triangular_args = {'check_finite': False}
2011-05-17 09:52:06 +08:00
def lars_path(X, y, Xy=None, Gram=None, max_iter=500,
alpha_min=0, method='lar', copy_X=True,
2011-07-21 14:15:35 +08:00
eps=np.finfo(np.float).eps,
copy_Gram=True, verbose=0, return_path=True,
2015-08-17 12:16:12 +08:00
return_n_iter=False, positive=False):
2013-03-15 17:41:25 +08:00
"""Compute Least Angle Regression or Lasso path using LARS algorithm [1]
2013-03-15 17:41:25 +08:00
The optimization objective for the case method='lasso' is::
(1 / (2 * n_samples)) * ||y - Xw||^2_2 + alpha * ||w||_1
2013-03-15 17:41:25 +08:00
in the case of method='lars', the objective function is only known in
the form of an implicit equation (see discussion in [1])
2015-06-03 12:24:04 +08:00
Read more in the :ref:`User Guide <least_angle_regression>`.
2011-06-01 08:31:19 +08:00
Parameters
-----------
X : array, shape: (n_samples, n_features)
Input data.
y : array, shape: (n_samples)
Input targets.
positive : boolean (default=False)
Restrict coefficients to be >= 0.
When using this option together with method 'lasso' the model
coefficients will not converge to the ordinary-least-squares solution
for small values of alpha (neither will they when using method 'lar'
2015-09-12 05:32:51 +08:00
..). Only coeffiencts up to the smallest alpha value (``alphas_[alphas_ >
0.].min()`` when fit_path=True) reached by the stepwise Lars-Lasso
algorithm are typically in congruence with the solution of the
coordinate descent lasso_path function.
max_iter : integer, optional (default=500)
Maximum number of iterations to perform, set to infinity for no limit.
2011-05-31 21:14:29 +08:00
Gram : None, 'auto', array, shape: (n_features, n_features), optional
Precomputed Gram matrix (X' * X), if ``'auto'``, the Gram
2011-07-27 06:27:18 +08:00
matrix is precomputed from the given X, if there are more samples
than features.
alpha_min : float, optional (default=0)
2011-06-01 08:31:19 +08:00
Minimum correlation along the path. It corresponds to the
regularization parameter alpha parameter in the Lasso.
method : {'lar', 'lasso'}, optional (default='lar')
Specifies the returned model. Select ``'lar'`` for Least Angle
Regression, ``'lasso'`` for the Lasso.
eps : float, optional (default=``np.finfo(np.float).eps``)
2011-07-21 14:15:35 +08:00
The machine-precision regularization in the computation of the
Cholesky diagonal factors. Increase this for very ill-conditioned
systems.
copy_X : bool, optional (default=True)
If ``False``, ``X`` is overwritten.
copy_Gram : bool, optional (default=True)
If ``False``, ``Gram`` is overwritten.
verbose : int (default=0)
Controls output verbosity.
return_path : bool, optional (default=True)
If ``return_path==True`` returns the entire path, else returns only the
last point of the path.
return_n_iter : bool, optional (default=False)
Whether to return the number of iterations.
2011-06-01 08:31:19 +08:00
Returns
--------
2014-12-01 09:59:42 +08:00
alphas : array, shape: [n_alphas + 1]
2011-08-03 02:42:15 +08:00
Maximum of covariances (in absolute value) at each iteration.
``n_alphas`` is either ``max_iter``, ``n_features`` or the
number of nodes in the path with ``alpha >= alpha_min``, whichever
is smaller.
2014-12-01 09:59:42 +08:00
active : array, shape [n_alphas]
2011-06-01 08:31:19 +08:00
Indices of active variables at the end of the path.
2014-12-01 09:59:42 +08:00
coefs : array, shape (n_features, n_alphas + 1)
2011-06-01 08:31:19 +08:00
Coefficients along the path
2014-07-21 07:03:46 +08:00
n_iter : int
Number of iterations run. Returned only if return_n_iter is set
to True.
2011-06-01 08:31:19 +08:00
See also
--------
lasso_path
LassoLars
Lars
LassoLarsCV
LarsCV
sklearn.decomposition.sparse_encode
2013-03-15 17:41:25 +08:00
References
----------
.. [1] "Least Angle Regression", Effron et al.
http://www-stat.stanford.edu/~tibs/ftp/lars.pdf
.. [2] `Wikipedia entry on the Least-angle regression
<http://en.wikipedia.org/wiki/Least-angle_regression>`_
.. [3] `Wikipedia entry on the Lasso
<http://en.wikipedia.org/wiki/Lasso_(statistics)#Lasso_method>`_
"""
2010-09-30 22:14:21 +08:00
n_features = X.shape[1]
n_samples = y.size
max_features = min(max_iter, n_features)
2012-08-30 02:05:17 +08:00
if return_path:
coefs = np.zeros((max_features + 1, n_features))
alphas = np.zeros(max_features + 1)
else:
coef, prev_coef = np.zeros(n_features), np.zeros(n_features)
alpha, prev_alpha = np.array([0.]), np.array([0.]) # better ideas?
n_iter, n_active = 0, 0
active, indices = list(), np.arange(n_features)
# holds the sign of covariance
sign_active = np.empty(max_features, dtype=np.int8)
drop = False
# will hold the cholesky factorization. Only lower part is
# referenced.
# We are initializing this to "zeros" and not empty, because
# it is passed to scipy linalg functions and thus if it has NaNs,
# even if they are in the upper part that it not used, we
# get errors raised.
# Once we support only scipy > 0.12 we can use check_finite=False and
# go back to "empty"
L = np.zeros((max_features, max_features), dtype=X.dtype)
swap, nrm2 = linalg.get_blas_funcs(('swap', 'nrm2'), (X,))
solve_cholesky, = get_lapack_funcs(('potrs',), (X,))
if Gram is None:
if copy_X:
# force copy. setting the array to be fortran-ordered
# speeds up the calculation of the (partial) Gram matrix
# and allows to easily swap columns
X = X.copy('F')
elif isinstance(Gram, string_types) and Gram == 'auto':
2011-06-18 21:38:43 +08:00
Gram = None
if X.shape[0] > X.shape[1]:
Gram = np.dot(X.T, X)
elif copy_Gram:
Gram = Gram.copy()
if Xy is None:
Cov = np.dot(X.T, y)
2010-09-20 21:36:20 +08:00
else:
Cov = Xy.copy()
if verbose:
2012-03-24 19:27:45 +08:00
if verbose > 1:
print("Step\t\tAdded\t\tDropped\t\tActive set size\t\tC")
2012-03-24 19:27:45 +08:00
else:
sys.stdout.write('.')
sys.stdout.flush()
2012-05-06 03:35:18 +08:00
tiny = np.finfo(np.float).tiny # to avoid division by 0 warning
tiny32 = np.finfo(np.float32).tiny # to avoid division by 0 warning
equality_tolerance = np.finfo(np.float32).eps
2012-05-06 03:35:18 +08:00
2011-10-17 18:09:08 +08:00
while True:
if Cov.size:
2015-08-17 12:16:12 +08:00
if positive:
C_idx = np.argmax(Cov)
else:
C_idx = np.argmax(np.abs(Cov))
C_ = Cov[C_idx]
2015-08-17 12:16:12 +08:00
if positive:
C = C_
else:
C = np.fabs(C_)
else:
C = 0.
2012-08-30 02:05:17 +08:00
if return_path:
alpha = alphas[n_iter, np.newaxis]
coef = coefs[n_iter]
prev_alpha = alphas[n_iter - 1, np.newaxis]
prev_coef = coefs[n_iter - 1]
alpha[0] = C / n_samples
if alpha[0] <= alpha_min + equality_tolerance: # early stopping
if abs(alpha[0] - alpha_min) > equality_tolerance:
# interpolation factor 0 <= ss < 1
if n_iter > 0:
# In the first iteration, all alphas are zero, the formula
# below would make ss a NaN
ss = ((prev_alpha[0] - alpha_min) /
2012-12-22 20:02:50 +08:00
(prev_alpha[0] - alpha[0]))
coef[:] = prev_coef + ss * (coef - prev_coef)
alpha[0] = alpha_min
2012-08-30 02:05:17 +08:00
if return_path:
coefs[n_iter] = coef
break
if n_iter >= max_iter or n_active >= n_features:
break
if not drop:
2011-10-17 18:09:08 +08:00
##########################################################
# Append x_j to the Cholesky factorization of (Xa * Xa') #
# #
# ( L 0 ) #
# L -> ( ) , where L * w = Xa' x_j #
# ( w z ) and z = ||x_j|| #
# #
##########################################################
2015-08-17 12:16:12 +08:00
if positive:
2015-08-25 11:45:19 +08:00
sign_active[n_active] = np.ones_like(C_)
else:
sign_active[n_active] = np.sign(C_)
2011-05-17 09:52:06 +08:00
m, n = n_active, C_idx + n_active
Cov[C_idx], Cov[0] = swap(Cov[C_idx], Cov[0])
indices[n], indices[m] = indices[m], indices[n]
Cov_not_shortened = Cov
2011-05-17 09:52:06 +08:00
Cov = Cov[1:] # remove Cov[0]
2010-09-08 01:47:26 +08:00
if Gram is None:
X.T[n], X.T[m] = swap(X.T[n], X.T[m])
2011-05-17 09:52:06 +08:00
c = nrm2(X.T[n_active]) ** 2
L[n_active, :n_active] = \
np.dot(X.T[n_active], X.T[:n_active].T)
2010-09-08 01:47:26 +08:00
else:
# swap does only work inplace if matrix is fortran
# contiguous ...
Gram[m], Gram[n] = swap(Gram[m], Gram[n])
Gram[:, m], Gram[:, n] = swap(Gram[:, m], Gram[:, n])
c = Gram[n_active, n_active]
L[n_active, :n_active] = Gram[n_active, :n_active]
# Update the cholesky decomposition for the Gram matrix
if n_active:
linalg.solve_triangular(L[:n_active, :n_active],
L[n_active, :n_active],
trans=0, lower=1,
overwrite_b=True,
**solve_triangular_args)
v = np.dot(L[n_active, :n_active], L[n_active, :n_active])
diag = max(np.sqrt(np.abs(c - v)), eps)
2011-10-17 18:09:08 +08:00
L[n_active, n_active] = diag
2012-10-15 01:33:43 +08:00
if diag < 1e-7:
# The system is becoming too ill-conditioned.
# We have degenerate vectors in our active set.
# We'll 'drop for good' the last regressor added.
# Note: this case is very rare. It is no longer triggered by the
# test suite. The `equality_tolerance` margin added in 0.16.0 to
# get early stopping to work consistently on all versions of
# Python including 32 bit Python under Windows seems to make it
# very difficult to trigger the 'drop for good' strategy.
2012-12-22 20:02:50 +08:00
warnings.warn('Regressors in active set degenerate. '
'Dropping a regressor, after %i iterations, '
'i.e. alpha=%.3e, '
'with an active set of %i regressors, and '
'the smallest cholesky pivot element being %.3e'
% (n_iter, alpha, n_active, diag),
ConvergenceWarning)
# XXX: need to figure a 'drop for good' way
Cov = Cov_not_shortened
Cov[0] = 0
Cov[C_idx], Cov[0] = swap(Cov[C_idx], Cov[0])
continue
active.append(indices[n_active])
n_active += 1
if verbose > 1:
print("%s\t\t%s\t\t%s\t\t%s\t\t%s" % (n_iter, active[-1], '',
2013-02-12 02:18:01 +08:00
n_active, C))
2015-08-17 12:16:12 +08:00
if method == 'lasso' and n_iter > 0 and prev_alpha[0] < alpha[0]:
# alpha is increasing. This is because the updates of Cov are
# bringing in too much numerical error that is greater than
# than the remaining correlation with the
# regressors. Time to bail out
warnings.warn('Early stopping the lars path, as the residues '
2012-12-22 20:02:50 +08:00
'are small and the current value of alpha is no '
'longer well controlled. %i iterations, alpha=%.3e, '
2012-12-22 20:02:50 +08:00
'previous alpha=%.3e, with an active set of %i '
'regressors.'
% (n_iter, alpha, prev_alpha, n_active),
ConvergenceWarning)
break
# least squares solution
least_squares, info = solve_cholesky(L[:n_active, :n_active],
2012-12-22 20:02:50 +08:00
sign_active[:n_active],
lower=True)
if least_squares.size == 1 and least_squares == 0:
# This happens because sign_active[:n_active] = 0
least_squares[...] = 1
AA = 1.
else:
# is this really needed ?
AA = 1. / np.sqrt(np.sum(least_squares * sign_active[:n_active]))
if not np.isfinite(AA):
# L is too ill-conditioned
i = 0
L_ = L[:n_active, :n_active].copy()
while not np.isfinite(AA):
L_.flat[::n_active + 1] += (2 ** i) * eps
2012-12-22 20:02:50 +08:00
least_squares, info = solve_cholesky(
L_, sign_active[:n_active], lower=True)
2012-11-07 18:22:58 +08:00
tmp = max(np.sum(least_squares * sign_active[:n_active]),
eps)
AA = 1. / np.sqrt(tmp)
i += 1
least_squares *= AA
2010-09-15 05:55:54 +08:00
if Gram is None:
# equiangular direction of variables in the active set
eq_dir = np.dot(X.T[:n_active].T, least_squares)
# correlation between each unactive variables and
# eqiangular vector
corr_eq_dir = np.dot(X.T[n_active:], eq_dir)
2010-09-08 01:47:26 +08:00
else:
# if huge number of features, this takes 50% of time, I
# think could be avoided if we just update it using an
# orthogonal (QR) decomposition of X
corr_eq_dir = np.dot(Gram[:n_active, n_active:].T,
least_squares)
2012-05-06 03:35:18 +08:00
g1 = arrayfuncs.min_pos((C - Cov) / (AA - corr_eq_dir + tiny))
2015-08-17 12:16:12 +08:00
if positive:
gamma_ = min(g1, C / AA)
else:
2015-08-25 11:45:19 +08:00
g2 = arrayfuncs.min_pos((C + Cov) / (AA + corr_eq_dir + tiny))
gamma_ = min(g1, g2, C / AA)
# TODO: better names for these variables: z
drop = False
2012-08-30 02:05:17 +08:00
z = -coef[active] / (least_squares + tiny32)
z_pos = arrayfuncs.min_pos(z)
if z_pos < gamma_:
# some coefficients have changed sign
idx = np.where(z == z_pos)[0][::-1]
# update the sign, important for LAR
sign_active[idx] = -sign_active[idx]
2011-05-17 09:52:06 +08:00
if method == 'lasso':
gamma_ = z_pos
drop = True
2010-09-20 21:36:20 +08:00
n_iter += 1
2012-08-30 02:05:17 +08:00
if return_path:
if n_iter >= coefs.shape[0]:
del coef, alpha, prev_alpha, prev_coef
# resize the coefs and alphas array
add_features = 2 * max(1, (max_features - n_active))
coefs = np.resize(coefs, (n_iter + add_features, n_features))
alphas = np.resize(alphas, n_iter + add_features)
2012-08-30 02:05:17 +08:00
coef = coefs[n_iter]
prev_coef = coefs[n_iter - 1]
alpha = alphas[n_iter, np.newaxis]
prev_alpha = alphas[n_iter - 1, np.newaxis]
else:
# mimic the effect of incrementing n_iter on the array references
prev_coef = coef
prev_alpha[0] = alpha[0]
coef = np.zeros_like(coef)
2012-08-30 02:05:17 +08:00
coef[active] = prev_coef[active] + gamma_ * least_squares
# update correlations
Cov -= gamma_ * corr_eq_dir
# See if any coefficient has changed sign
if drop and method == 'lasso':
# handle the case when idx is not length of 1
[arrayfuncs.cholesky_delete(L[:n_active, :n_active], ii) for ii in
idx]
n_active -= 1
m, n = idx, n_active
# handle the case when idx is not length of 1
drop_idx = [active.pop(ii) for ii in idx]
if Gram is None:
# propagate dropped variable
for ii in idx:
for i in range(ii, n_active):
X.T[i], X.T[i + 1] = swap(X.T[i], X.T[i + 1])
# yeah this is stupid
indices[i], indices[i + 1] = indices[i + 1], indices[i]
# TODO: this could be updated
2012-08-30 02:05:17 +08:00
residual = y - np.dot(X[:, :n_active], coef[active])
temp = np.dot(X.T[n_active], residual)
2010-12-07 22:26:27 +08:00
Cov = np.r_[temp, Cov]
else:
for ii in idx:
for i in range(ii, n_active):
indices[i], indices[i + 1] = indices[i + 1], indices[i]
Gram[i], Gram[i + 1] = swap(Gram[i], Gram[i + 1])
Gram[:, i], Gram[:, i + 1] = swap(Gram[:, i],
Gram[:, i + 1])
# Cov_n = Cov_j + x_j * X + increment(betas) TODO:
# will this still work with multiple drops ?
# recompute covariance. Probably could be done better
# wrong as Xy is not swapped with the rest of variables
# TODO: this could be updated
2012-08-30 02:05:17 +08:00
residual = y - np.dot(X, coef)
temp = np.dot(X.T[drop_idx], residual)
Cov = np.r_[temp, Cov]
2010-09-20 21:36:20 +08:00
sign_active = np.delete(sign_active, idx)
2011-05-17 09:52:06 +08:00
sign_active = np.append(sign_active, 0.) # just to maintain size
2012-03-24 19:27:45 +08:00
if verbose > 1:
print("%s\t\t%s\t\t%s\t\t%s\t\t%s" % (n_iter, '', drop_idx,
n_active, abs(temp)))
2012-08-30 02:05:17 +08:00
if return_path:
# resize coefs in case of early stop
alphas = alphas[:n_iter + 1]
coefs = coefs[:n_iter + 1]
if return_n_iter:
return alphas, active, coefs.T, n_iter
else:
return alphas, active, coefs.T
2012-08-30 02:05:17 +08:00
else:
if return_n_iter:
return alpha, active, coef, n_iter
else:
return alpha, active, coef
2011-07-27 06:27:18 +08:00
###############################################################################
2011-06-18 16:16:48 +08:00
# Estimator classes
2012-06-13 12:26:02 +08:00
class Lars(LinearModel, RegressorMixin):
2010-09-20 21:36:20 +08:00
"""Least Angle Regression model a.k.a. LAR
2015-06-03 12:24:04 +08:00
Read more in the :ref:`User Guide <least_angle_regression>`.
Parameters
----------
n_nonzero_coefs : int, optional
Target number of non-zero coefficients. Use ``np.inf`` for no limit.
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).
positive : boolean (default=False)
Restrict coefficients to be >= 0. Be aware that you might want to
remove fit_intercept which is set True by default.
2011-06-18 16:16:48 +08:00
verbose : boolean or integer, optional
Sets the verbosity amount
normalize : boolean, optional, default False
If ``True``, the regressors X will be normalized before regression.
2011-06-18 16:16:48 +08:00
precompute : True | False | 'auto' | array-like
Whether to use a precomputed Gram matrix to speed up
calculations. If set to ``'auto'`` let us decide. The Gram
2011-06-18 16:16:48 +08:00
matrix can also be passed as argument.
copy_X : boolean, optional, default True
If ``True``, X will be copied; else, it may be overwritten.
2014-12-01 09:59:42 +08:00
eps : float, optional
2011-07-21 14:15:35 +08:00
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.
2011-07-21 14:15:35 +08:00
2012-09-03 01:30:11 +08:00
fit_path : boolean
If True the full path is stored in the ``coef_path_`` attribute.
2012-09-03 03:46:12 +08:00
If you compute the solution for a large problem or many targets,
setting ``fit_path`` to ``False`` will lead to a speedup, especially
2012-09-03 03:46:12 +08:00
with a small alpha.
2011-07-21 14:15:35 +08:00
Attributes
----------
alphas_ : array, shape (n_alphas + 1,) | list of n_targets such arrays
Maximum of covariances (in absolute value) at each iteration. \
``n_alphas`` is either ``n_nonzero_coefs`` or ``n_features``, \
whichever is smaller.
active_ : list, length = n_alphas | list of n_targets such lists
Indices of active variables at the end of the path.
coef_path_ : array, shape (n_features, n_alphas + 1) \
2013-05-02 21:46:25 +08:00
| list of n_targets such arrays
The varying values of the coefficients along the path. It is not
present if the ``fit_path`` parameter is ``False``.
coef_ : array, shape (n_features,) or (n_targets, n_features)
2013-06-27 21:09:16 +08:00
Parameter vector (w in the formulation formula).
intercept_ : float | array, shape (n_targets,)
Independent term in decision function.
2012-08-31 22:16:40 +08:00
n_iter_ : array-like or int
The number of iterations taken by lars_path to find the
grid of alphas for each target.
Examples
--------
>>> from sklearn import linear_model
>>> clf = linear_model.Lars(n_nonzero_coefs=1)
2011-12-16 00:24:53 +08:00
>>> clf.fit([[-1, 1], [0, 0], [1, 1]], [-1.1111, 0, -1.1111])
... # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
2015-08-18 14:05:21 +08:00
Lars(copy_X=True, eps=..., fit_intercept=True, fit_path=True,
n_nonzero_coefs=1, normalize=True, positive=False, precompute='auto',
verbose=False)
2012-03-15 07:26:30 +08:00
>>> print(clf.coef_) # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
[ 0. -1.11...]
See also
--------
lars_path, LarsCV
sklearn.decomposition.sparse_encode
"""
2011-07-27 06:27:18 +08:00
def __init__(self, fit_intercept=True, verbose=False, normalize=True,
2015-08-25 11:45:19 +08:00
precompute='auto', n_nonzero_coefs=500,
eps=np.finfo(np.float).eps, copy_X=True, fit_path=True,
positive=False):
self.fit_intercept = fit_intercept
self.verbose = verbose
2011-06-18 16:16:48 +08:00
self.normalize = normalize
self.method = 'lar'
2011-07-27 06:27:18 +08:00
self.precompute = precompute
self.n_nonzero_coefs = n_nonzero_coefs
2015-09-12 05:32:51 +08:00
self.positive = positive
2011-07-21 14:15:35 +08:00
self.eps = eps
self.copy_X = copy_X
self.fit_path = fit_path
2011-08-08 06:22:49 +08:00
def _get_gram(self):
# precompute if n_samples > n_features
precompute = self.precompute
if hasattr(precompute, '__array__'):
2012-06-23 19:47:11 +08:00
Gram = precompute
2011-08-08 06:22:49 +08:00
elif precompute == 'auto':
Gram = 'auto'
else:
Gram = None
return Gram
def fit(self, X, y, Xy=None):
"""Fit the model using X, y as training data.
2011-06-18 22:20:20 +08:00
parameters
----------
2013-07-25 21:41:24 +08:00
X : array-like, shape (n_samples, n_features)
2012-12-22 21:29:59 +08:00
Training data.
2013-07-25 21:41:24 +08:00
y : array-like, shape (n_samples,) or (n_samples, n_targets)
2012-12-22 21:29:59 +08:00
Target values.
2013-07-25 21:41:24 +08:00
Xy : array-like, shape (n_samples,) or (n_samples, n_targets), \
optional
Xy = np.dot(X.T, y) that can be precomputed. It is useful
only when the Gram matrix is precomputed.
2011-06-18 22:20:20 +08:00
returns
-------
self : object
returns an instance of self.
"""
X, y = check_X_y(X, y, y_numeric=True, multi_output=True)
2012-06-05 22:42:05 +08:00
n_features = X.shape[1]
X, y, X_mean, y_mean, X_std = self._center_data(X, y,
self.fit_intercept,
self.normalize,
self.copy_X)
2012-06-05 22:42:05 +08:00
if y.ndim == 1:
y = y[:, np.newaxis]
n_targets = y.shape[1]
2011-06-19 21:10:59 +08:00
alpha = getattr(self, 'alpha', 0.)
2011-07-29 17:34:10 +08:00
if hasattr(self, 'n_nonzero_coefs'):
2011-08-08 06:22:49 +08:00
alpha = 0. # n_nonzero_coefs parametrization takes priority
max_iter = self.n_nonzero_coefs
else:
2011-07-29 17:34:10 +08:00
max_iter = self.max_iter
precompute = self.precompute
2012-12-22 20:02:50 +08:00
if not hasattr(precompute, '__array__') and (
2012-12-27 07:28:01 +08:00
precompute is True or
2012-12-22 20:02:50 +08:00
(precompute == 'auto' and X.shape[0] > X.shape[1]) or
(precompute == 'auto' and y.shape[1] > 1)):
Gram = np.dot(X.T, X)
else:
2012-06-05 22:42:05 +08:00
Gram = self._get_gram()
self.alphas_ = []
self.n_iter_ = []
if self.fit_path:
self.coef_ = []
self.active_ = []
self.coef_path_ = []
for k in xrange(n_targets):
this_Xy = None if Xy is None else Xy[:, k]
alphas, active, coef_path, n_iter_ = lars_path(
X, y[:, k], Gram=Gram, Xy=this_Xy, copy_X=self.copy_X,
copy_Gram=True, alpha_min=alpha, method=self.method,
verbose=max(0, self.verbose - 1), max_iter=max_iter,
eps=self.eps, return_path=True,
2015-08-17 12:16:12 +08:00
return_n_iter=True, positive=self.positive)
self.alphas_.append(alphas)
self.active_.append(active)
self.n_iter_.append(n_iter_)
self.coef_path_.append(coef_path)
self.coef_.append(coef_path[:, -1])
if n_targets == 1:
self.alphas_, self.active_, self.coef_path_, self.coef_ = [
2012-12-22 20:02:50 +08:00
a[0] for a in (self.alphas_, self.active_, self.coef_path_,
self.coef_)]
self.n_iter_ = self.n_iter_[0]
else:
self.coef_ = np.empty((n_targets, n_features))
for k in xrange(n_targets):
this_Xy = None if Xy is None else Xy[:, k]
alphas, _, self.coef_[k], n_iter_ = lars_path(
X, y[:, k], Gram=Gram, Xy=this_Xy, copy_X=self.copy_X,
copy_Gram=True, alpha_min=alpha, method=self.method,
verbose=max(0, self.verbose - 1), max_iter=max_iter,
eps=self.eps, return_path=False, return_n_iter=True,
2015-08-17 12:16:12 +08:00
positive=self.positive)
self.alphas_.append(alphas)
self.n_iter_.append(n_iter_)
if n_targets == 1:
self.alphas_ = self.alphas_[0]
self.n_iter_ = self.n_iter_[0]
self._set_intercept(X_mean, y_mean, X_std)
return self
class LassoLars(Lars):
2011-07-26 09:41:48 +08:00
"""Lasso model fit with Least Angle Regression a.k.a. Lars
It is a Linear Model trained with an L1 prior as regularizer.
The optimization objective for Lasso is::
(1 / (2 * n_samples)) * ||y - Xw||^2_2 + alpha * ||w||_1
2015-06-03 12:24:04 +08:00
Read more in the :ref:`User Guide <least_angle_regression>`.
Parameters
----------
2013-01-19 22:19:19 +08:00
alpha : float
Constant that multiplies the penalty term. Defaults to 1.0.
``alpha = 0`` is equivalent to an ordinary least square, solved
2013-05-02 21:46:25 +08:00
by :class:`LinearRegression`. For numerical reasons, using
``alpha = 0`` with the LassoLars object is not advised and you
should prefer the LinearRegression object.
2013-01-19 22:19:19 +08:00
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).
positive : boolean (default=False)
Restrict coefficients to be >= 0. Be aware that you might want to
remove fit_intercept which is set True by default.
Under the positive restriction the model coefficients will not converge
to the ordinary-least-squares solution for small values of alpha.
2015-09-12 05:32:51 +08:00
Only coeffiencts up to the smallest alpha value (``alphas_[alphas_ >
0.].min()`` when fit_path=True) reached by the stepwise Lars-Lasso
algorithm are typically in congruence with the solution of the
coordinate descent Lasso estimator.
2011-06-18 16:16:48 +08:00
verbose : boolean or integer, optional
Sets the verbosity amount
normalize : boolean, optional, default False
If True, the regressors X will be normalized before regression.
2011-06-18 16:16:48 +08:00
copy_X : boolean, optional, default True
If True, X will be copied; else, it may be overwritten.
2011-06-18 16:16:48 +08:00
precompute : True | False | 'auto' | array-like
Whether to use a precomputed Gram matrix to speed up
calculations. If set to ``'auto'`` let us decide. The Gram
2011-06-18 16:16:48 +08:00
matrix can also be passed as argument.
2013-07-25 21:41:24 +08:00
max_iter : integer, optional
Maximum number of iterations to perform.
2013-07-25 21:41:24 +08:00
eps : float, optional
2011-07-21 14:15:35 +08:00
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.
2011-07-21 14:15:35 +08:00
2012-09-03 01:30:11 +08:00
fit_path : boolean
If ``True`` the full path is stored in the ``coef_path_`` attribute.
2012-09-03 03:46:12 +08:00
If you compute the solution for a large problem or many targets,
setting ``fit_path`` to ``False`` will lead to a speedup, especially
2012-09-03 03:46:12 +08:00
with a small alpha.
2011-06-18 16:16:48 +08:00
Attributes
----------
alphas_ : array, shape (n_alphas + 1,) | list of n_targets such arrays
Maximum of covariances (in absolute value) at each iteration. \
``n_alphas`` is either ``max_iter``, ``n_features``, or the number of \
nodes in the path with correlation greater than ``alpha``, whichever \
is smaller.
active_ : list, length = n_alphas | list of n_targets such lists
Indices of active variables at the end of the path.
coef_path_ : array, shape (n_features, n_alphas + 1) or list
2013-05-02 21:46:25 +08:00
If a list is passed it's expected to be one of n_targets such arrays.
The varying values of the coefficients along the path. It is not
present if the ``fit_path`` parameter is ``False``.
coef_ : array, shape (n_features,) or (n_targets, n_features)
2013-06-27 21:09:16 +08:00
Parameter vector (w in the formulation formula).
intercept_ : float | array, shape (n_targets,)
Independent term in decision function.
2012-08-31 22:16:40 +08:00
n_iter_ : array-like or int.
The number of iterations taken by lars_path to find the
grid of alphas for each target.
Examples
--------
>>> from sklearn import linear_model
2011-07-26 09:41:48 +08:00
>>> clf = linear_model.LassoLars(alpha=0.01)
2011-12-16 00:24:53 +08:00
>>> clf.fit([[-1, 1], [0, 0], [1, 1]], [-1, 0, -1])
... # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
2015-08-18 14:05:21 +08:00
LassoLars(alpha=0.01, copy_X=True, eps=..., fit_intercept=True,
fit_path=True, max_iter=500, normalize=True, positive=False,
precompute='auto', verbose=False)
2012-03-15 07:26:30 +08:00
>>> print(clf.coef_) # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
[ 0. -0.963257...]
See also
--------
lars_path
lasso_path
Lasso
LassoCV
LassoLarsCV
sklearn.decomposition.sparse_encode
"""
2011-08-03 02:42:15 +08:00
def __init__(self, alpha=1.0, fit_intercept=True, verbose=False,
normalize=True, precompute='auto', max_iter=500,
2015-08-25 11:45:19 +08:00
eps=np.finfo(np.float).eps, copy_X=True, fit_path=True,
positive=False):
self.alpha = alpha
self.fit_intercept = fit_intercept
2011-06-18 16:16:48 +08:00
self.max_iter = max_iter
self.verbose = verbose
2011-06-18 16:16:48 +08:00
self.normalize = normalize
self.method = 'lasso'
2015-09-12 05:32:51 +08:00
self.positive = positive
self.precompute = precompute
self.copy_X = copy_X
2011-07-21 14:15:35 +08:00
self.eps = eps
self.fit_path = fit_path
2011-08-03 02:42:15 +08:00
###############################################################################
2011-06-18 22:20:20 +08:00
# Cross-validated estimator classes
def _check_copy_and_writeable(array, copy=False):
if copy or not array.flags.writeable:
return array.copy()
return array
2011-06-18 22:20:20 +08:00
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, positive=False):
2011-06-18 22:20:20 +08:00
"""Compute the residues on left-out data for a full LARS path
2011-07-21 00:31:54 +08:00
Parameters
-----------
X_train : array, shape (n_samples, n_features)
2011-07-21 00:31:54 +08:00
The data to fit the LARS on
2015-06-03 12:24:04 +08:00
y_train : array, shape (n_samples)
2011-07-21 00:31:54 +08:00
The target variable to fit LARS on
2015-06-03 12:24:04 +08:00
X_test : array, shape (n_samples, n_features)
2011-07-21 00:31:54 +08:00
The data to compute the residues on
2015-06-03 12:24:04 +08:00
y_test : array, shape (n_samples)
2011-07-21 00:31:54 +08:00
The target variable to compute the residues on
2015-06-03 12:24:04 +08:00
Gram : None, 'auto', array, shape: (n_features, n_features), optional
Precomputed Gram matrix (X' * X), if ``'auto'``, the Gram
2011-08-03 02:42:15 +08:00
matrix is precomputed from the given X, if there are more samples
2011-07-21 00:31:54 +08:00
than features
2015-06-03 12:24:04 +08:00
copy : boolean, optional
Whether X_train, X_test, y_train and y_test should be copied;
if False, they may be overwritten.
2015-06-03 12:24:04 +08:00
method : 'lar' | 'lasso'
Specifies the returned model. Select ``'lar'`` for Least Angle
Regression, ``'lasso'`` for the Lasso.
2015-06-03 12:24:04 +08:00
verbose : integer, optional
2011-07-21 00:31:54 +08:00
Sets the amount of verbosity
2015-06-03 12:24:04 +08:00
2011-07-21 00:31:54 +08:00
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).
2015-06-03 12:24:04 +08:00
positive : boolean (default=False)
Restrict coefficients to be >= 0. Be aware that you might want to
remove fit_intercept which is set True by default.
See reservations for using this option in combination with method
'lasso' for expected small values of alpha in the doc of LassoLarsCV
and LassoLarsIC.
normalize : boolean, optional, default False
If True, the regressors X will be normalized before regression.
2015-06-03 12:24:04 +08:00
max_iter : integer, optional
2011-07-21 00:31:54 +08:00
Maximum number of iterations to perform.
2015-06-03 12:24:04 +08:00
eps : float, optional
The machine-precision regularization in the computation of the
2011-07-21 14:15:35 +08:00
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.
2011-07-21 14:15:35 +08:00
2011-07-21 00:31:54 +08:00
2011-06-18 22:20:20 +08:00
Returns
--------
2013-07-25 21:41:24 +08:00
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.
2011-06-18 22:20:20 +08:00
active : list
2011-06-18 22:20:20 +08:00
Indices of active variables at the end of the path.
2013-07-25 21:41:24 +08:00
coefs : array, shape (n_features, n_alphas)
2011-06-18 22:20:20 +08:00
Coefficients along the path
2013-07-25 21:41:24 +08:00
residues : array, shape (n_alphas, n_samples)
2011-06-18 22:20:20 +08:00
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)
2011-06-20 02:19:22 +08:00
2011-06-18 22:20:20 +08:00
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)
2011-06-18 22:20:20 +08:00
y_train -= y_mean
y_test = as_float_array(y_test, copy=False)
2011-06-18 22:20:20 +08:00
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(
2012-12-22 20:02:50 +08:00
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,
positive=positive)
if normalize:
coefs[nonzeros] /= norms[nonzeros][:, np.newaxis]
residues = np.dot(X_test, coefs) - y_test[:, np.newaxis]
return alphas, active, coefs, residues.T
2011-06-18 22:20:20 +08:00
2012-06-13 12:26:02 +08:00
class LarsCV(Lars):
2011-06-18 22:20:20 +08:00
"""Cross-validated Least Angle Regression model
2015-06-03 12:24:04 +08:00
Read more in the :ref:`User Guide <least_angle_regression>`.
2011-06-18 22:20:20 +08:00
Parameters
----------
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).
positive : boolean (default=False)
Restrict coefficients to be >= 0. Be aware that you might want to
remove fit_intercept which is set True by default.
2011-06-18 22:20:20 +08:00
verbose : boolean or integer, optional
Sets the verbosity amount
normalize : boolean, optional, default False
If True, the regressors X will be normalized before regression.
2011-06-18 22:20:20 +08:00
copy_X : boolean, optional, default True
If ``True``, X will be copied; else, it may be overwritten.
2011-08-10 22:19:59 +08:00
2011-06-18 22:20:20 +08:00
precompute : True | False | 'auto' | array-like
Whether to use a precomputed Gram matrix to speed up
calculations. If set to ``'auto'`` let us decide. The Gram
2011-06-18 22:20:20 +08:00
matrix can also be passed as argument.
max_iter: integer, optional
Maximum number of iterations to perform.
cv : int, cross-validation generator or an iterable, optional
Determines the cross-validation splitting strategy.
Possible inputs for cv are:
- None, to use the default 3-fold cross-validation,
- integer, to specify the number of folds.
- An object to be used as a cross-validation generator.
- An iterable yielding train/test splits.
For integer/None inputs, :class:`KFold` is used.
Refer :ref:`User Guide <cross_validation>` for the various
cross-validation strategies that can be used here.
2011-06-18 22:20:20 +08:00
max_n_alphas : integer, optional
The maximum number of points on the path used to compute the
residuals in the cross-validation
2011-06-18 22:20:20 +08:00
n_jobs : integer, optional
Number of CPUs to use during the cross validation. If ``-1``, use
2011-06-18 22:20:20 +08:00
all the CPUs
2014-12-01 09:59:42 +08:00
eps : float, optional
2011-07-21 14:15:35 +08:00
The machine-precision regularization in the computation of the
Cholesky diagonal factors. Increase this for very ill-conditioned
systems.
2011-06-18 22:20:20 +08:00
Attributes
----------
coef_ : array, shape (n_features,)
2013-06-27 21:09:16 +08:00
parameter vector (w in the formulation formula)
2011-06-18 22:20:20 +08:00
intercept_ : float
independent term in decision function
2011-06-18 22:20:20 +08:00
coef_path_ : array, shape (n_features, n_alphas)
the varying values of the coefficients along the path
alpha_ : float
the estimated regularization parameter alpha
alphas_ : array, shape (n_alphas,)
the different values of alpha along the path
cv_alphas_ : array, shape (n_cv_alphas,)
all the values of alpha along the path for the different folds
cv_mse_path_ : array, shape (n_folds, n_cv_alphas)
the mean square error on left-out for each fold along the path
(alpha values given by ``cv_alphas``)
2011-06-18 22:20:20 +08:00
n_iter_ : array-like or int
the number of iterations run by Lars with the optimal alpha.
2011-06-18 22:20:20 +08:00
See also
--------
lars_path, LassoLars, LassoLarsCV
2011-06-18 22:20:20 +08:00
"""
method = 'lar'
2011-08-03 02:42:15 +08:00
def __init__(self, fit_intercept=True, verbose=False, max_iter=500,
normalize=True, precompute='auto', cv=None,
max_n_alphas=1000, n_jobs=1, eps=np.finfo(np.float).eps,
copy_X=True, positive=False):
2011-06-18 22:20:20 +08:00
self.fit_intercept = fit_intercept
self.positive = positive
2011-06-18 22:20:20 +08:00
self.max_iter = max_iter
self.verbose = verbose
self.normalize = normalize
self.precompute = precompute
self.copy_X = copy_X
2011-06-18 22:20:20 +08:00
self.cv = cv
self.max_n_alphas = max_n_alphas
2011-06-18 22:20:20 +08:00
self.n_jobs = n_jobs
2011-07-21 14:15:35 +08:00
self.eps = eps
2011-06-18 22:20:20 +08:00
2011-08-23 21:21:05 +08:00
def fit(self, X, y):
2011-06-18 22:20:20 +08:00
"""Fit the model using X, y as training data.
Parameters
----------
2013-07-25 21:41:24 +08:00
X : array-like, shape (n_samples, n_features)
2011-06-18 22:20:20 +08:00
Training data.
2013-07-25 21:41:24 +08:00
y : array-like, shape (n_samples,)
2011-06-18 22:20:20 +08:00
Target values.
Returns
-------
self : object
returns an instance of self.
"""
self.fit_path = True
X, y = check_X_y(X, y, y_numeric=True)
X = as_float_array(X, copy=self.copy_X)
y = as_float_array(y, copy=self.copy_X)
2011-06-18 22:20:20 +08:00
# init cross-validation generator
2011-08-04 02:23:18 +08:00
cv = check_cv(self.cv, X, y, classifier=False)
2011-06-18 22:20:20 +08:00
Gram = 'auto' if self.precompute else None
cv_paths = Parallel(n_jobs=self.n_jobs, verbose=self.verbose)(
2012-12-22 20:02:50 +08:00
delayed(_lars_path_residues)(
X[train], y[train], X[test], y[test], Gram=Gram, copy=False,
method=self.method, verbose=max(0, self.verbose - 1),
normalize=self.normalize, fit_intercept=self.fit_intercept,
max_iter=self.max_iter, eps=self.eps, positive=self.positive)
2012-12-22 20:02:50 +08:00
for train, test in cv)
2011-12-29 16:54:08 +08:00
all_alphas = np.concatenate(list(zip(*cv_paths))[0])
# Unique also sorts
all_alphas = np.unique(all_alphas)
# Take at most max_n_alphas values
stride = int(max(1, int(len(all_alphas) / float(self.max_n_alphas))))
all_alphas = all_alphas[::stride]
2011-06-18 22:20:20 +08:00
2011-08-03 05:29:25 +08:00
mse_path = np.empty((len(all_alphas), len(cv_paths)))
for index, (alphas, active, coefs, residues) in enumerate(cv_paths):
alphas = alphas[::-1]
residues = residues[::-1]
if alphas[0] != 0:
alphas = np.r_[0, alphas]
residues = np.r_[residues[0, np.newaxis], residues]
if alphas[-1] != all_alphas[-1]:
alphas = np.r_[alphas, all_alphas[-1]]
residues = np.r_[residues, residues[-1, np.newaxis]]
this_residues = interpolate.interp1d(alphas,
residues,
2011-06-18 22:20:20 +08:00
axis=0)(all_alphas)
this_residues **= 2
2011-11-21 04:16:52 +08:00
mse_path[:, index] = np.mean(this_residues, axis=-1)
2011-06-18 22:20:20 +08:00
2011-11-21 04:16:52 +08:00
mask = np.all(np.isfinite(mse_path), axis=-1)
2011-06-18 22:20:20 +08:00
all_alphas = all_alphas[mask]
mse_path = mse_path[mask]
# Select the alpha that minimizes left-out error
2011-11-21 04:16:52 +08:00
i_best_alpha = np.argmin(mse_path.mean(axis=-1))
2011-06-18 22:20:20 +08:00
best_alpha = all_alphas[i_best_alpha]
# Store our parameters
self.alpha_ = best_alpha
self.cv_alphas_ = all_alphas
2011-06-18 22:20:20 +08:00
self.cv_mse_path_ = mse_path
# Now compute the full model
2012-09-03 00:25:57 +08:00
# it will call a lasso internally when self if LassoLarsCV
# as self.method == 'lasso'
2012-06-13 12:26:02 +08:00
Lars.fit(self, X, y)
2011-06-18 22:20:20 +08:00
return self
@property
def alpha(self):
# impedance matching for the above Lars.fit (should not be documented)
return self.alpha_
2011-06-18 22:20:20 +08:00
class LassoLarsCV(LarsCV):
2011-08-03 02:42:15 +08:00
"""Cross-validated Lasso, using the LARS algorithm
2011-06-18 22:20:20 +08:00
The optimization objective for Lasso is::
(1 / (2 * n_samples)) * ||y - Xw||^2_2 + alpha * ||w||_1
2015-06-03 12:24:04 +08:00
Read more in the :ref:`User Guide <least_angle_regression>`.
2011-06-18 22:20:20 +08:00
Parameters
----------
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).
positive : boolean (default=False)
Restrict coefficients to be >= 0. Be aware that you might want to
remove fit_intercept which is set True by default.
Under the positive restriction the model coefficients do not converge
to the ordinary-least-squares solution for small values of alpha.
2015-09-12 05:32:51 +08:00
Only coeffiencts up to the smallest alpha value (``alphas_[alphas_ >
0.].min()`` when fit_path=True) reached by the stepwise Lars-Lasso
algorithm are typically in congruence with the solution of the
coordinate descent Lasso estimator.
As a consequence using LassoLarsCV only makes sense for problems where
a sparse solution is expected and/or reached.
2011-06-18 22:20:20 +08:00
verbose : boolean or integer, optional
Sets the verbosity amount
normalize : boolean, optional, default False
If True, the regressors X will be normalized before regression.
2011-06-18 22:20:20 +08:00
precompute : True | False | 'auto' | array-like
Whether to use a precomputed Gram matrix to speed up
calculations. If set to ``'auto'`` let us decide. The Gram
2011-06-18 22:20:20 +08:00
matrix can also be passed as argument.
2014-12-01 09:59:42 +08:00
max_iter : integer, optional
2011-06-18 22:20:20 +08:00
Maximum number of iterations to perform.
cv : int, cross-validation generator or an iterable, optional
Determines the cross-validation splitting strategy.
Possible inputs for cv are:
- None, to use the default 3-fold cross-validation,
- integer, to specify the number of folds.
- An object to be used as a cross-validation generator.
- An iterable yielding train/test splits.
For integer/None inputs, :class:`KFold` is used.
Refer :ref:`User Guide <cross_validation>` for the various
cross-validation strategies that can be used here.
2011-06-18 22:20:20 +08:00
2012-01-24 07:18:36 +08:00
max_n_alphas : integer, optional
The maximum number of points on the path used to compute the
residuals in the cross-validation
2011-06-18 22:20:20 +08:00
n_jobs : integer, optional
Number of CPUs to use during the cross validation. If ``-1``, use
2011-06-18 22:20:20 +08:00
all the CPUs
2014-12-01 09:59:42 +08:00
eps : float, optional
2011-07-21 14:15:35 +08:00
The machine-precision regularization in the computation of the
Cholesky diagonal factors. Increase this for very ill-conditioned
systems.
copy_X : boolean, optional, default True
If True, X will be copied; else, it may be overwritten.
2011-07-21 14:15:35 +08:00
2011-06-18 22:20:20 +08:00
Attributes
----------
coef_ : array, shape (n_features,)
2013-06-27 21:09:16 +08:00
parameter vector (w in the formulation formula)
2011-06-18 22:20:20 +08:00
intercept_ : float
2011-06-18 22:20:20 +08:00
independent term in decision function.
coef_path_ : array, shape (n_features, n_alphas)
2011-06-18 22:20:20 +08:00
the varying values of the coefficients along the path
alpha_ : float
the estimated regularization parameter alpha
alphas_ : array, shape (n_alphas,)
2011-06-18 22:20:20 +08:00
the different values of alpha along the path
cv_alphas_ : array, shape (n_cv_alphas,)
2011-06-18 22:20:20 +08:00
all the values of alpha along the path for the different folds
cv_mse_path_ : array, shape (n_folds, n_cv_alphas)
2011-06-18 22:20:20 +08:00
the mean square error on left-out for each fold along the path
(alpha values given by ``cv_alphas``)
2011-06-18 22:20:20 +08:00
n_iter_ : array-like or int
the number of iterations run by Lars with the optimal alpha.
2011-07-23 20:47:47 +08:00
Notes
-----
The object solves the same problem as the LassoCV object. However,
unlike the LassoCV, it find the relevant alphas values by itself.
2011-07-23 20:47:47 +08:00
In general, because of this property, it will be more stable.
However, it is more fragile to heavily multicollinear datasets.
2011-08-03 02:42:15 +08:00
2011-07-23 20:47:47 +08:00
It is more efficient than the LassoCV if only a small number of
features are selected compared to the total number, for instance if
there are very few samples compared to the number of features.
2011-06-18 22:20:20 +08:00
See also
--------
lars_path, LassoLars, LarsCV, LassoCV
2011-06-18 22:20:20 +08:00
"""
method = 'lasso'
2011-08-08 06:22:49 +08:00
class LassoLarsIC(LassoLars):
"""Lasso model fit with Lars using BIC or AIC for model selection
The optimization objective for Lasso is::
(1 / (2 * n_samples)) * ||y - Xw||^2_2 + alpha * ||w||_1
AIC is the Akaike information criterion and BIC is the Bayes
Information criterion. Such criteria are useful to select the value
of the regularization parameter by making a trade-off between the
goodness of fit and the complexity of the model. A good model should
explain well the data while being simple.
2015-06-03 12:24:04 +08:00
Read more in the :ref:`User Guide <least_angle_regression>`.
2011-08-08 06:22:49 +08:00
Parameters
----------
2014-12-01 09:59:42 +08:00
criterion : 'bic' | 'aic'
2011-08-08 06:22:49 +08:00
The type of criterion to use.
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).
positive : boolean (default=False)
Restrict coefficients to be >= 0. Be aware that you might want to
remove fit_intercept which is set True by default.
Under the positive restriction the model coefficients do not converge
to the ordinary-least-squares solution for small values of alpha.
2015-09-12 05:32:51 +08:00
Only coeffiencts up to the smallest alpha value (``alphas_[alphas_ >
0.].min()`` when fit_path=True) reached by the stepwise Lars-Lasso
algorithm are typically in congruence with the solution of the
coordinate descent Lasso estimator.
As a consequence using LassoLarsIC only makes sense for problems where
a sparse solution is expected and/or reached.
2011-08-08 06:22:49 +08:00
verbose : boolean or integer, optional
Sets the verbosity amount
normalize : boolean, optional, default False
If True, the regressors X will be normalized before regression.
2011-08-08 06:22:49 +08:00
copy_X : boolean, optional, default True
If True, X will be copied; else, it may be overwritten.
2011-08-08 06:22:49 +08:00
precompute : True | False | 'auto' | array-like
Whether to use a precomputed Gram matrix to speed up
calculations. If set to ``'auto'`` let us decide. The Gram
2011-08-08 06:22:49 +08:00
matrix can also be passed as argument.
2014-12-01 09:59:42 +08:00
max_iter : integer, optional
2011-08-08 06:22:49 +08:00
Maximum number of iterations to perform. Can be used for
early stopping.
2014-12-01 09:59:42 +08:00
eps : float, optional
2011-08-08 06:22:49 +08:00
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
2011-08-08 06:22:49 +08:00
optimization-based algorithms, this parameter does not control
the tolerance of the optimization.
Attributes
----------
coef_ : array, shape (n_features,)
2013-06-27 21:09:16 +08:00
parameter vector (w in the formulation formula)
2011-08-08 06:22:49 +08:00
intercept_ : float
2011-08-08 06:22:49 +08:00
independent term in decision function.
alpha_ : float
the alpha parameter chosen by the information criterion
n_iter_ : int
number of iterations run by lars_path to find the grid of
alphas.
criterion_ : array, shape (n_alphas,)
The value of the information criteria ('aic', 'bic') across all
alphas. The alpha which has the smallest information criteria
is chosen.
2011-08-08 06:22:49 +08:00
Examples
--------
>>> from sklearn import linear_model
2011-08-08 06:22:49 +08:00
>>> clf = linear_model.LassoLarsIC(criterion='bic')
2011-12-16 00:24:53 +08:00
>>> clf.fit([[-1, 1], [0, 0], [1, 1]], [-1.1111, 0, -1.1111])
... # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
LassoLarsIC(copy_X=True, criterion='bic', eps=..., fit_intercept=True,
2015-08-18 14:05:21 +08:00
max_iter=500, normalize=True, positive=False, precompute='auto',
verbose=False)
2012-03-15 07:26:30 +08:00
>>> print(clf.coef_) # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
[ 0. -1.11...]
2011-08-08 06:22:49 +08:00
Notes
-----
2011-08-08 06:22:49 +08:00
The estimation of the number of degrees of freedom is given by:
"On the degrees of freedom of the lasso"
Hui Zou, Trevor Hastie, and Robert Tibshirani
Ann. Statist. Volume 35, Number 5 (2007), 2173-2192.
http://en.wikipedia.org/wiki/Akaike_information_criterion
http://en.wikipedia.org/wiki/Bayesian_information_criterion
See also
--------
lars_path, LassoLars, LassoLarsCV
"""
def __init__(self, criterion='aic', fit_intercept=True, verbose=False,
2011-08-08 06:22:49 +08:00
normalize=True, precompute='auto', max_iter=500,
eps=np.finfo(np.float).eps, copy_X=True, positive=False):
2011-08-08 06:22:49 +08:00
self.criterion = criterion
self.fit_intercept = fit_intercept
self.positive = positive
2011-08-08 06:22:49 +08:00
self.max_iter = max_iter
self.verbose = verbose
self.normalize = normalize
self.copy_X = copy_X
2011-08-08 06:22:49 +08:00
self.precompute = precompute
self.eps = eps
def fit(self, X, y, copy_X=True):
2011-08-08 06:22:49 +08:00
"""Fit the model using X, y as training data.
Parameters
2011-08-08 06:22:49 +08:00
----------
X : array-like, shape (n_samples, n_features)
2011-08-08 06:22:49 +08:00
training data.
2013-07-25 21:41:24 +08:00
y : array-like, shape (n_samples,)
2011-08-08 06:22:49 +08:00
target values.
2015-06-03 12:24:04 +08:00
copy_X : boolean, optional, default True
If ``True``, X will be copied; else, it may be overwritten.
2011-08-08 06:22:49 +08:00
Returns
2011-08-08 06:22:49 +08:00
-------
self : object
returns an instance of self.
"""
self.fit_path = True
X, y = check_X_y(X, y, y_numeric=True)
2011-08-08 06:22:49 +08:00
2012-12-22 20:02:50 +08:00
X, y, Xmean, ymean, Xstd = LinearModel._center_data(
X, y, self.fit_intercept, self.normalize, self.copy_X)
2011-08-08 06:22:49 +08:00
max_iter = self.max_iter
Gram = self._get_gram()
alphas_, active_, coef_path_, self.n_iter_ = lars_path(
2012-12-22 20:02:50 +08:00
X, y, Gram=Gram, copy_X=copy_X, copy_Gram=True, alpha_min=0.0,
method='lasso', verbose=self.verbose, max_iter=max_iter,
eps=self.eps, return_n_iter=True, positive=self.positive)
2011-08-08 06:22:49 +08:00
n_samples = X.shape[0]
if self.criterion == 'aic':
K = 2 # AIC
elif self.criterion == 'bic':
K = log(n_samples) # BIC
else:
raise ValueError('criterion should be either bic or aic')
R = y[:, np.newaxis] - np.dot(X, coef_path_) # residuals
mean_squared_error = np.mean(R ** 2, axis=0)
2011-08-08 06:22:49 +08:00
df = np.zeros(coef_path_.shape[1], dtype=np.int) # Degrees of freedom
for k, coef in enumerate(coef_path_.T):
mask = np.abs(coef) > np.finfo(coef.dtype).eps
2011-08-08 06:22:49 +08:00
if not np.any(mask):
continue
# get the number of degrees of freedom equal to:
# Xc = X[:, mask]
# Trace(Xc * inv(Xc.T, Xc) * Xc.T) ie the number of non-zero coefs
df[k] = np.sum(mask)
self.alphas_ = alphas_
with np.errstate(divide='ignore'):
self.criterion_ = n_samples * np.log(mean_squared_error) + K * df
n_best = np.argmin(self.criterion_)
2011-08-08 06:22:49 +08:00
self.alpha_ = alphas_[n_best]
self.coef_ = coef_path_[:, n_best]
self._set_intercept(Xmean, ymean, Xstd)
2011-08-08 06:22:49 +08:00
return self