2010-12-01 23:05:52 +08:00
.. _linear_model:
2010-05-26 18:03:52 +08:00
=========================
Generalized Linear Models
=========================
2010-03-04 00:23:43 +08:00
2011-09-02 17:00:02 +08:00
.. currentmodule :: sklearn.linear_model
2010-09-01 20:46:46 +08:00
2010-06-10 19:36:04 +08:00
The following are a set of methods intended for regression in which
the target value is expected to be a linear combination of the input
2010-06-10 23:37:46 +08:00
variables. In mathematical notion, if :math: `\hat{y}` is the predicted
2010-06-10 19:36:04 +08:00
value.
2010-03-04 00:23:43 +08:00
2011-11-21 22:36:24 +08:00
.. math :: \hat{y}(w, x) = w_0 + w_1 x_1 + ... + w_p x_p
2010-03-12 21:08:39 +08:00
2011-07-07 21:39:07 +08:00
Across the module, we designate the vector :math:`w = (w_1,
2011-11-21 22:36:24 +08:00
..., w_p)` as ``coef_`` and :math:`w_0` as ``intercept_``.
2010-03-12 21:08:39 +08:00
2011-01-25 01:35:05 +08:00
To perform classification with generalized linear models, see
2011-03-10 05:58:53 +08:00
:ref: `Logistic_regression` .
2010-12-01 23:05:52 +08:00
2011-08-24 22:01:38 +08:00
2010-09-03 20:38:12 +08:00
.. _ordinary_least_squares:
2011-07-07 21:39:07 +08:00
Ordinary Least Squares
=======================
2010-03-12 21:08:39 +08:00
2010-09-03 20:38:12 +08:00
:class: `LinearRegression` fits a linear model with coefficients
2011-11-21 22:36:24 +08:00
:math: `w = (w_1, ..., w_p)` to minimize the residual sum
2010-09-03 22:33:52 +08:00
of squares between the observed responses in the dataset, and the
2011-11-21 19:31:59 +08:00
responses predicted by the linear approximation. Mathematically it
solves a problem of the form:
2011-12-20 00:57:19 +08:00
.. math :: \underset{w}{min\,} {|| X w - y||_2}^2
2010-09-02 22:30:36 +08:00
2011-04-02 00:40:38 +08:00
.. figure :: ../auto_examples/linear_model/images/plot_ols_1.png
2010-11-25 21:53:55 +08:00
:target: ../auto_examples/linear_model/plot_ols.html
2011-07-07 21:43:06 +08:00
:align: center
2010-09-03 22:33:52 +08:00
:scale: 50%
2010-09-02 22:30:36 +08:00
2010-09-03 20:38:12 +08:00
:class: `LinearRegression` will take in its `fit` method arrays X, y
and will store the coefficients :math: `w` of the linear model in its
2011-08-24 22:01:38 +08:00
`coef\_` member::
2010-09-02 22:30:36 +08:00
2011-09-02 17:00:02 +08:00
>>> from sklearn import linear_model
2010-11-25 21:53:55 +08:00
>>> clf = linear_model.LinearRegression()
2010-09-03 22:33:52 +08:00
>>> clf.fit ([[0, 0], [1, 1], [2, 2]], [0, 1, 2])
2011-09-21 18:40:22 +08:00
LinearRegression(copy_X=True, fit_intercept=True, normalize=False)
2010-09-03 22:33:52 +08:00
>>> clf.coef_
array([ 0.5, 0.5])
2010-09-03 20:38:12 +08:00
However, coefficient estimates for Ordinary Least Squares rely on the
independence of the model terms. When terms are correlated and the
columns of the design matrix :math: `X` have an approximate linear
2011-11-21 19:31:59 +08:00
dependence, the design matrix becomes close to singular
2010-09-03 20:38:12 +08:00
and as a result, the least-squares estimate becomes highly sensitive
to random errors in the observed response, producing a large
variance. This situation of *multicollinearity* can arise, for
example, when data are collected without an experimental design.
2010-09-11 19:40:01 +08:00
.. topic :: Examples:
2010-09-03 22:33:52 +08:00
2010-11-25 21:53:55 +08:00
* :ref: `example_linear_model_plot_ols.py`
2010-09-11 19:40:01 +08:00
2011-07-07 21:39:07 +08:00
Ordinary Least Squares Complexity
---------------------------------
2010-09-03 20:38:12 +08:00
This method computes the least squares solution using a singular value
2011-11-21 22:36:24 +08:00
decomposition of X. If X is a matrix of size (n, p) this method has a
2010-09-03 20:38:12 +08:00
cost of :math: `O(n p^2)` , assuming that :math: `n \geq p` .
2010-09-02 22:30:36 +08:00
2011-07-07 21:39:07 +08:00
2010-03-12 21:08:39 +08:00
Ridge Regression
2010-06-23 20:53:14 +08:00
================
2010-03-12 21:08:39 +08:00
2010-10-01 17:51:06 +08:00
:class: `Ridge` regression addresses some of the problems of
2010-09-03 20:38:12 +08:00
:ref: `ordinary_least_squares` by imposing a penalty on the size of
coefficients. The ridge coefficients minimize a penalized residual sum
of squares,
2010-03-12 21:08:39 +08:00
2010-09-03 20:38:12 +08:00
.. math ::
2011-12-20 00:57:19 +08:00
\underset{w}{min\,} {{|| X w - y||_2}^2 + \alpha {||w||_2}^2}
2011-07-07 21:39:07 +08:00
2010-09-03 20:38:12 +08:00
2011-07-08 17:16:56 +08:00
Here, :math: `\alpha \geq 0` is a complexity parameter that controls the amount
of shrinkage: the larger the value of :math: `\alpha` , the greater the amount
of shrinkage and thus the coefficients become more robust to collinearity.
.. figure :: ../auto_examples/linear_model/images/plot_ridge_path_1.png
:target: ../auto_examples/linear_model/plot_ridge_path.html
:align: center
:scale: 50%
As with other linear models, :class: `Ridge` will take in its `fit` method
arrays X, y and will store the coefficients :math: `w` of the linear model in
2011-08-24 22:01:38 +08:00
its `coef\_` member::
2010-03-04 00:23:43 +08:00
2011-09-02 17:00:02 +08:00
>>> from sklearn import linear_model
2010-11-25 21:53:55 +08:00
>>> clf = linear_model.Ridge (alpha = .5)
2012-09-22 11:48:24 +08:00
>>> clf.fit ([[0, 0], [0, 0], [1, 1]], [0, .1, 1]) # doctest: +NORMALIZE_WHITESPACE
Ridge(alpha=0.5, copy_X=True, fit_intercept=True, max_iter=None,
normalize=False, solver='auto', tol=0.001)
2010-09-06 17:36:04 +08:00
>>> clf.coef_
array([ 0.34545455, 0.34545455])
2010-10-22 15:19:11 +08:00
>>> clf.intercept_ #doctest: +ELLIPSIS
0.13636...
2010-09-06 17:36:04 +08:00
2011-04-11 00:16:20 +08:00
2011-07-08 17:16:56 +08:00
.. topic :: Examples:
* :ref: `example_linear_model_plot_ridge_path.py`
2011-11-21 19:51:13 +08:00
* :ref: `example_document_classification_20newsgroups.py`
2011-07-08 17:16:56 +08:00
2010-09-11 19:40:01 +08:00
Ridge Complexity
2011-04-11 00:16:20 +08:00
----------------
2010-09-06 17:36:04 +08:00
This method has the same order of complexity than an
:ref: `ordinary_least_squares` .
2011-07-08 17:16:56 +08:00
.. FIXME:
.. Not completely true: OLS is solved by an SVD, while Ridge is solved by
.. the method of normal equations (Cholesky), there is a big flop difference
.. between these
2011-04-11 00:16:20 +08:00
2011-11-21 19:51:13 +08:00
Setting the regularization parameter: generalized Cross-Validation
------------------------------------------------------------------
2011-02-14 16:50:25 +08:00
2011-05-02 17:29:51 +08:00
:class: `RidgeCV` implements ridge regression with built-in
cross-validation of the alpha parameter. The object works in the same way
as GridSearchCV except that it defaults to Generalized Cross-Validation
2011-08-24 22:01:38 +08:00
(GCV), an efficient form of leave-one-out cross-validation::
2011-02-14 16:50:25 +08:00
2011-09-02 17:00:02 +08:00
>>> from sklearn import linear_model
2011-02-14 16:50:25 +08:00
>>> clf = linear_model.RidgeCV(alphas=[0.1, 1.0, 10.0])
2011-08-25 01:23:44 +08:00
>>> clf.fit([[0, 0], [0, 0], [1, 1]], [0, .1, 1]) # doctest: +SKIP
2011-08-24 22:01:38 +08:00
RidgeCV(alphas=[0.1, 1.0, 10.0], cv=None, fit_intercept=True, loss_func=None,
normalize=False, score_func=None)
2012-09-02 23:56:46 +08:00
>>> clf.alpha_ # doctest: +SKIP
2011-08-25 01:26:37 +08:00
0.1
2011-02-14 16:50:25 +08:00
2011-02-14 17:08:37 +08:00
.. topic :: References
* "Notes on Regularized Least Squares", Rifkin & Lippert (`technical report
<http://cbcl.mit.edu/projects/cbcl/publications/ps/MIT-CSAIL-TR-2007-025.pdf>`_,
`course slides
<http://www.mit.edu/~9.520/spring07/Classes/rlsslides.pdf>`_).
2011-08-05 22:50:34 +08:00
2011-08-03 19:56:49 +08:00
.. _lasso:
2011-07-25 18:57:42 +08:00
2010-05-12 21:23:26 +08:00
Lasso
2010-06-23 20:53:14 +08:00
=====
2010-05-12 21:23:26 +08:00
2011-08-11 01:39:17 +08:00
The :class: `Lasso` is a linear model that estimates sparse coefficients.
It is useful in some contexts due to its tendency to prefer solutions
with fewer parameter values, effectively reducing the number of variables
upon which the given solution is dependent. For this reason, the Lasso
and its variants are fundamental to the field of compressed sensing.
2012-01-15 06:19:32 +08:00
Under certain conditions, it can recover the exact set of non-zero
weights (see
:ref: `example_applications_plot_tomography_l1_reconstruction.py` ).
2011-08-11 01:39:17 +08:00
2011-12-19 18:04:36 +08:00
Mathematically, it consists of a linear model trained with :math: `\ell_1` prior
as regularizer. The objective function to minimize is:
2010-06-10 19:36:04 +08:00
2011-12-20 00:57:19 +08:00
.. math :: \underset{w}{min\,} { \frac{1}{2n_{samples}} ||X w - y||_2 ^ 2 + \alpha ||w||_1}
2010-06-10 19:36:04 +08:00
2010-10-01 17:51:06 +08:00
The lasso estimate thus solves the minimization of the
2011-12-19 18:04:36 +08:00
least-squares penalty with :math: `\alpha ||w||_1` added, where
:math: `\alpha` is a constant and :math: `||w||_1` is the :math: `\ell_1` -norm of
the parameter vector.
2010-06-23 20:53:14 +08:00
2011-08-11 01:39:17 +08:00
The implementation in the class :class: `Lasso` uses coordinate descent as
the algorithm to fit the coefficients. See :ref: `least_angle_regression`
2011-08-24 22:01:38 +08:00
for another implementation::
2010-06-23 20:53:14 +08:00
2010-11-25 21:53:55 +08:00
>>> clf = linear_model.Lasso(alpha = 0.1)
2011-08-24 22:01:38 +08:00
>>> clf.fit([[0, 0], [1, 1]], [0, 1])
2011-09-21 18:40:22 +08:00
Lasso(alpha=0.1, copy_X=True, fit_intercept=True, max_iter=1000,
2012-04-02 01:22:50 +08:00
normalize=False, positive=False, precompute='auto', tol=0.0001,
warm_start=False)
2011-08-24 22:01:38 +08:00
>>> clf.predict([[1, 1]])
2010-09-06 17:36:04 +08:00
array([ 0.8])
2010-06-10 19:36:04 +08:00
2011-08-11 01:39:17 +08:00
Also useful for lower-level tasks is the function :func: `lasso_path` that
computes the coefficients along the full path of possible values.
2010-06-10 19:36:04 +08:00
2010-09-11 19:40:01 +08:00
.. topic :: Examples:
2012-03-05 03:13:22 +08:00
* :ref: `example_linear_model_plot_lasso_and_elasticnet.py`
2012-01-15 06:19:32 +08:00
* :ref: `example_applications_plot_tomography_l1_reconstruction.py`
2010-09-06 17:36:04 +08:00
2012-01-29 02:47:50 +08:00
.. note :: **Feature selection with Lasso**
As the Lasso regression yields sparse models, it can
thus be used to perform feature selection, as detailed in
:ref: `l1_feature_selection` .
2012-08-29 20:14:13 +08:00
.. note :: **Randomized sparsity**
For feature selection or sparse recovery, it may be interesting to
use :ref: `randomized_l1` .
2012-01-29 02:47:50 +08:00
2011-11-21 19:51:13 +08:00
Setting regularization parameter
--------------------------------
2011-07-23 20:47:47 +08:00
2011-08-11 01:39:17 +08:00
The `alpha` parameter control the degree of sparsity of the coefficients
estimated.
2011-08-11 01:33:43 +08:00
Using cross-validation
^^^^^^^^^^^^^^^^^^^^^^^
2011-11-21 19:51:13 +08:00
scikit-learn exposes objects that set the Lasso `alpha` parameter by
2011-07-23 20:47:47 +08:00
cross-validation: :class: `LassoCV` and :class: `LassoLarsCV` .
2011-08-03 19:56:49 +08:00
:class: `LassoLarsCV` is based on the :ref: `least_angle_regression` algorithm
2011-07-23 20:47:47 +08:00
explained below.
For high-dimensional datasets with many collinear regressors,
:class: `LassoCV` is most often preferrable. How, :class: `LassoLarsCV` has
2011-08-10 21:48:24 +08:00
the advantage of exploring more relevant values of `alpha` parameter, and
2011-07-23 20:47:47 +08:00
if the number of samples is very small compared to the number of
observations, it is often faster than :class: `LassoCV` .
2011-08-11 01:33:43 +08:00
.. |lasso_cv_1| image :: ../auto_examples/linear_model/images/plot_lasso_model_selection_2.png
:target: ../auto_examples/linear_model/plot_lasso_model_selection.html
2012-08-07 21:46:46 +08:00
:scale: 48%
2011-08-11 01:33:43 +08:00
.. |lasso_cv_2| image :: ../auto_examples/linear_model/images/plot_lasso_model_selection_3.png
:target: ../auto_examples/linear_model/plot_lasso_model_selection.html
2012-08-07 21:46:46 +08:00
:scale: 48%
2011-08-11 01:33:43 +08:00
2012-08-07 21:46:46 +08:00
.. centered :: |lasso_cv_1| |lasso_cv_2|
2011-08-11 01:33:43 +08:00
Information-criteria based model selection
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2011-08-08 21:21:15 +08:00
Alternatively, the estimator :class: `LassoLarsIC` proposes to use the
Akaike information criterion (AIC) and the Bayes Information criterion (BIC).
It is a computationally cheaper alternative to find the optimal value of alpha
as the regularization path is computed only once instead of k+1 times
2011-08-11 04:30:31 +08:00
when using k-fold cross-validation. However, such criteria needs a
proper estimation of the degrees of freedom of the solution, are
derived for large samples (asymptotic results) and assume the model
is correct, i.e. that the data are actually generated by this model.
They also tend to break when the problem is badly conditioned
(more features than samples).
2011-08-10 21:48:24 +08:00
.. figure :: ../auto_examples/linear_model/images/plot_lasso_model_selection_1.png
:target: ../auto_examples/linear_model/plot_lasso_model_selection.html
:align: center
:scale: 50%
2011-08-11 01:33:43 +08:00
.. topic :: Examples:
* :ref: `example_linear_model_plot_lasso_model_selection.py`
2010-09-06 17:36:04 +08:00
2010-05-26 18:03:52 +08:00
2010-05-12 21:23:26 +08:00
Elastic Net
2010-06-23 20:53:14 +08:00
===========
2010-09-03 22:33:52 +08:00
:class: `ElasticNet` is a linear model trained with L1 and L2 prior as
2010-05-12 21:23:26 +08:00
regularizer.
2010-10-01 17:51:06 +08:00
The objective function to minimize is in this case
2010-06-10 19:36:04 +08:00
2011-11-21 19:31:59 +08:00
.. math ::
2011-12-20 00:57:19 +08:00
\underset{w}{min\,} { \frac{1}{2n_{samples}} ||X w - y||_2 ^ 2 + \alpha \rho ||w||_1 +
\frac{\alpha(1-\rho)}{2} ||w||_2 ^ 2}
2010-06-10 19:36:04 +08:00
2010-03-04 00:23:43 +08:00
2011-07-07 21:39:07 +08:00
.. figure :: ../auto_examples/linear_model/images/plot_lasso_coordinate_descent_path_1.png
:target: ../auto_examples/linear_model/plot_lasso_coordinate_descent_path.html
:align: center
:scale: 50%
2012-02-26 20:09:14 +08:00
The class :class: `ElasticNetCV` can be used to set the parameters `alpha`
and `rho` by cross-validation.
2010-09-11 19:40:01 +08:00
.. topic :: Examples:
2010-03-04 00:23:43 +08:00
2012-03-05 03:13:22 +08:00
* :ref: `example_linear_model_plot_lasso_and_elasticnet.py`
2010-11-25 21:53:55 +08:00
* :ref: `example_linear_model_plot_lasso_coordinate_descent_path.py`
2010-06-10 19:36:04 +08:00
2012-08-03 14:42:38 +08:00
.. _multi_task_lasso:
Multi-task Lasso
================
2012-08-07 21:46:46 +08:00
The :class: `MultiTaskLasso` is a linear model that estimates sparse
coefficients for multiple regression problems jointly: `y` is a 2D array,
of shape (n_samples, n_tasks). The constraint is that the selected
2012-08-03 14:42:38 +08:00
features are the same for all the regression problems, also called tasks.
2012-08-07 21:46:46 +08:00
The following figure compares the location of the non-zeros in W obtained
with a simple Lasso or a MultiTaskLasso. The Lasso estimates yields
scattered non-zeros while the non-zeros of the MultiTaskLasso are full
columns.
.. |multi_task_lasso_1| image :: ../auto_examples/linear_model/images/plot_multi_task_lasso_support_1.png
:target: ../auto_examples/linear_model/plot_multi_task_lasso_support.html
:scale: 48%
.. |multi_task_lasso_2| image :: ../auto_examples/linear_model/images/plot_multi_task_lasso_support_2.png
:target: ../auto_examples/linear_model/plot_multi_task_lasso_support.html
:scale: 48%
.. centered :: |multi_task_lasso_1| |multi_task_lasso_2|
.. centered :: Fitting a time-series model, imposing that any active feature be active at all times.
.. topic :: Examples:
* :ref: `example_linear_model_plot_multi_task_lasso_support.py`
2012-08-03 14:42:38 +08:00
Mathematically, it consists of a linear model trained with a mixed
:math: `\ell_1` :math: `\ell_2` prior as regularizer.
The objective function to minimize is:
.. math :: \underset{w}{min\,} { \frac{1}{2n_{samples}} ||X W - Y||_2 ^ 2 + \alpha ||W||_{21}}
where;
.. math :: ||W||_21 = \sum_i \sqrt{\sum_j w_{ij}^2}
The implementation in the class :class: `MultiTaskLasso` uses coordinate descent as
the algorithm to fit the coefficients.
2010-06-23 20:53:14 +08:00
2010-12-12 02:20:44 +08:00
.. _least_angle_regression:
2010-06-23 20:53:14 +08:00
2010-12-12 02:20:44 +08:00
Least Angle Regression
======================
2010-09-01 22:05:51 +08:00
Least-angle regression (LARS) is a regression algorithm for
high-dimensional data, developed by Bradley Efron, Trevor Hastie, Iain
Johnstone and Robert Tibshirani.
The advantages of LARS are:
2011-08-03 22:57:29 +08:00
- It is numerically efficient in contexts where p >> n (i.e., when the
2011-08-03 19:56:49 +08:00
number of dimensions is significantly greater than the number of
points)
2010-09-01 22:05:51 +08:00
- It is computationally just as fast as forward selection and has
the same order of complexity as an ordinary least squares.
- It produces a full piecewise linear solution path, which is
useful in cross-validation or similar attempts to tune the model.
- If two variables are almost equally correlated with the response,
then their coefficients should increase at approximately the same
rate. The algorithm thus behaves as intuition would expect, and
also is more stable.
- It is easily modified to produce solutions for other estimators,
2011-07-07 17:08:22 +08:00
like the Lasso.
2010-09-01 22:05:51 +08:00
The disadvantages of the LARS method include:
- Because LARS is based upon an iterative refitting of the
residuals, it would appear to be especially sensitive to the
effects of noise. This problem is discussed in detail by Weisberg
in the discussion section of the Efron et al. (2004) Annals of
Statistics article.
2010-06-23 20:53:14 +08:00
2011-07-26 09:41:48 +08:00
The LARS model can be used using estimator :class: `Lars` , or its
2010-12-01 22:53:12 +08:00
low-level implementation :func: `lars_path` .
2010-09-01 22:05:51 +08:00
2010-09-06 17:36:04 +08:00
LARS Lasso
==========
2011-07-26 09:41:48 +08:00
:class: `LassoLars` is a lasso model implemented using the LARS
2010-12-01 22:53:12 +08:00
algorithm, and unlike the implementation based on coordinate_descent,
this yields the exact solution, which is piecewise linear as a
function of the norm of its coefficients.
2011-04-02 00:40:38 +08:00
.. figure :: ../auto_examples/linear_model/images/plot_lasso_lars_1.png
2010-12-01 22:53:12 +08:00
:target: ../auto_examples/linear_model/plot_lasso_lars.html
:align: center
:scale: 50%
2010-09-06 17:36:04 +08:00
2011-03-10 05:58:53 +08:00
::
2010-09-06 17:36:04 +08:00
2011-09-02 17:00:02 +08:00
>>> from sklearn import linear_model
2011-07-26 09:41:48 +08:00
>>> clf = linear_model.LassoLars(alpha=.1)
2012-08-30 04:21:39 +08:00
>>> clf.fit([[0, 0], [1, 1]], [0, 1]) # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
2011-09-21 18:40:22 +08:00
LassoLars(alpha=0.1, copy_X=True, eps=..., fit_intercept=True,
2012-08-30 04:21:39 +08:00
fit_path=True, max_iter=500, normalize=True, precompute='auto',
verbose=False)
2011-09-02 03:34:45 +08:00
>>> clf.coef_ # doctest: +ELLIPSIS
array([ 0.717157..., 0. ])
2010-09-06 17:36:04 +08:00
2010-09-11 19:40:01 +08:00
.. topic :: Examples:
2010-11-25 21:53:55 +08:00
* :ref: `example_linear_model_plot_lasso_lars.py`
2010-09-11 19:40:01 +08:00
2011-07-26 09:41:48 +08:00
The Lars algorithm provides the full path of the coefficients along
2010-12-01 22:53:12 +08:00
the regularization parameter almost for free, thus a common operation
consist of retrieving the path with function :func: `lars_path`
2010-09-01 22:05:51 +08:00
Mathematical formulation
------------------------
2010-06-23 20:53:14 +08:00
2010-07-01 00:12:45 +08:00
The algorithm is similar to forward stepwise regression, but instead
of including variables at each step, the estimated parameters are
increased in a direction equiangular to each one's correlations with
the residual.
Instead of giving a vector result, the LARS solution consists of a
curve denoting the solution for each value of the L1 norm of the
parameter vector. The full coeffients path is stored in the array
`` coef_path_ `` , which has size (n_features, max_features+1). The first
column is always zero.
2010-09-11 19:40:01 +08:00
.. topic :: References:
2010-07-01 00:12:45 +08:00
2010-09-30 22:14:21 +08:00
* Original Algorithm is detailed in the paper `Least Angle Regression
2010-09-11 19:40:01 +08:00
<http://www-stat.stanford.edu/~hastie/Papers/LARS/LeastAngle_2002.pdf>`_
by Hastie et al.
2010-06-23 20:53:14 +08:00
2011-09-05 06:45:31 +08:00
2011-08-23 17:12:20 +08:00
.. _omp:
2011-07-25 18:57:42 +08:00
Orthogonal Matching Pursuit (OMP)
=================================
2011-08-04 17:49:56 +08:00
:class: `OrthogonalMatchingPursuit` and :func: `orthogonal_mp` implements the OMP
algorithm for approximating the fit of a linear model with constraints imposed
2011-08-10 21:48:24 +08:00
on the number of non-zero coefficients (ie. the L :sub: `0` pseudo-norm).
2011-08-04 17:49:56 +08:00
2011-08-10 21:48:24 +08:00
Being a forward feature selection method like :ref: `least_angle_regression` ,
2011-08-04 17:49:56 +08:00
orthogonal matching pursuit can approximate the optimum solution vector with a
fixed number of non-zero elements:
2011-07-25 18:57:42 +08:00
2012-01-06 18:18:10 +08:00
.. math :: \text{arg\,min\,} ||y - X\gamma||_2^2 \text{ subject to } \
2012-05-21 16:33:17 +08:00
||\gamma||_0 \leq n_{nonzero\_coefs}
2011-07-25 18:57:42 +08:00
Alternatively, orthogonal matching pursuit can target a specific error instead
of a specific number of non-zero coefficients. This can be expressed as:
2012-01-20 11:21:23 +08:00
.. math :: \text{arg\,min\,} ||\gamma||_0 \text{ subject to } ||y-X\gamma||_2^2 \
2012-01-06 18:18:10 +08:00
\leq \text{tol}
2011-07-25 18:57:42 +08:00
OMP is based on a greedy algorithm that includes at each step the atom most
highly correlated with the current residual. It is similar to the simpler
matching pursuit (MP) method, but better in that at each iteration, the
residual is recomputed using an orthogonal projection on the space of the
2011-08-10 21:48:24 +08:00
previously chosen dictionary elements.
2011-07-25 18:57:42 +08:00
.. topic :: Examples:
* :ref: `example_linear_model_plot_omp.py`
.. topic :: References:
2012-01-05 18:22:26 +08:00
* http://www.cs.technion.ac.il/~ronrubin/Publications/KSVD-OMP-v2.pdf
2010-09-08 19:46:36 +08:00
2011-07-25 18:57:42 +08:00
* `Matching pursuits with time-frequency dictionaries
<http://blanche.polytechnique.fr/~mallat/papiers/MallatPursuit93.pdf>`_,
2011-08-10 21:48:24 +08:00
S. G. Mallat, Z. Zhang,
2010-09-08 19:46:36 +08:00
Bayesian Regression
===================
2010-10-01 17:51:06 +08:00
Bayesian regression techniques can be used to include regularization
2011-07-23 19:21:48 +08:00
parameters in the estimation procedure: the regularization parameter is
2011-11-04 02:54:47 +08:00
not set in a hard sense but tuned to the data at hand.
2011-07-23 19:21:48 +08:00
2012-01-06 18:18:10 +08:00
This can be done by introducing `uninformative priors
<http://en.wikipedia.org/wiki/Non-informative_prior#Uninformative_priors> `__
2012-01-06 20:33:03 +08:00
over the hyper parameters of the model.
2012-01-06 06:17:35 +08:00
The :math: `\ell_{2}` regularization used in `Ridge Regression`_ is equivalent
to finding a maximum a-postiori solution under a Gaussian prior over the
2012-01-06 18:18:10 +08:00
parameters :math: `w` with precision :math: `\lambda^-1` . Instead of setting
`\lambda` manually, it is possible to treat it as a random variable to be
estimated from the data.
2012-01-06 06:17:35 +08:00
To obtain a fully probabilistic model, the output :math: `y` is assumed
to be Gaussian distributed around :math: `X w` :
.. math :: p(y|X,w,\alpha) = \mathcal{N}(y|X w,\alpha)
Alpha is again treated as a random variable that is to be estimated from the
data.
2010-09-08 19:46:36 +08:00
2011-11-04 02:54:47 +08:00
The advantages of Bayesian Regression are:
2010-09-08 19:46:36 +08:00
2011-07-25 18:57:42 +08:00
- It adapts to the data at hand.
2010-09-08 19:46:36 +08:00
2011-07-25 18:57:42 +08:00
- It can be used to include regularization parameters in the
estimation procedure.
2010-09-08 19:46:36 +08:00
2011-11-04 02:54:47 +08:00
The disadvantages of Bayesian regression include:
2011-07-23 20:14:24 +08:00
2011-07-25 18:57:42 +08:00
- Inference of the model can be time consuming.
2011-07-23 20:14:24 +08:00
2010-09-08 19:46:36 +08:00
2012-01-06 06:17:35 +08:00
.. topic :: References
* A good introduction to Bayesian methods is given in C. Bishop: Pattern
2012-01-20 11:21:23 +08:00
Recognition and Machine learning
2012-01-06 06:17:35 +08:00
* Original Algorithm is detailed in the book `Bayesian learning for neural
networks` by Radford M. Neal
2011-12-19 17:47:15 +08:00
.. _bayesian_ridge_regression:
2010-09-08 19:46:36 +08:00
Bayesian Ridge Regression
2010-09-08 21:39:58 +08:00
-------------------------
2010-09-08 19:46:36 +08:00
2012-01-06 06:17:35 +08:00
:class: `BayesianRidge` estimates a probabilistic model of the
regression problem as described above.
The prior for the parameter :math: `w` is given by a spherical Gaussian:
.. math :: p(w|\lambda) =
\mathcal{N}(w|0,\lambda^{-1}\bold{I_{p}})
2010-09-08 21:05:58 +08:00
2012-01-06 18:18:10 +08:00
The priors over :math: `\alpha` and :math: `\lambda` are choosen to be `gamma
distributions <http://en.wikipedia.org/wiki/Gamma_distribution>`__, the
conjugate prior for the precision of the Gaussian.
2010-09-08 19:46:36 +08:00
2012-01-06 18:18:10 +08:00
The resulting model is called *Bayesian Ridge Regression* , and is similar to the
classical :class: `Ridge` . The parameters :math: `w` , :math: `\alpha` and
:math: `\lambda` are estimated jointly during the fit of the model. The
remaining hyperparameters are the parameters of the gamma priors over
:math: `\alpha` and :math: `\lambda` . These are usually choosen to be
*non-informative* . The parameters are estimated by maximizing the *marginal
log likelihood*.
2010-09-08 19:46:36 +08:00
2012-01-06 06:17:35 +08:00
By default :math: `\alpha_1 = \alpha_2 = \lambda_1 = \lambda_2 = 1.e^{-6}` .
2010-09-08 19:46:36 +08:00
2011-07-25 18:57:42 +08:00
.. figure :: ../auto_examples/linear_model/images/plot_bayesian_ridge_1.png
:target: ../auto_examples/linear_model/plot_bayesian_ridge.html
:align: center
2011-11-21 19:31:59 +08:00
:scale: 50%
2011-07-25 18:57:42 +08:00
2011-11-04 02:54:47 +08:00
Bayesian Ridge Regression is used for regression::
2010-09-08 19:46:36 +08:00
2011-09-02 17:00:02 +08:00
>>> from sklearn import linear_model
2010-10-11 15:18:05 +08:00
>>> X = [[0., 0.], [1., 1.], [2., 2.], [3., 3.]]
2010-09-08 19:46:36 +08:00
>>> Y = [0., 1., 2., 3.]
2010-11-25 21:53:55 +08:00
>>> clf = linear_model.BayesianRidge()
2011-09-26 21:41:16 +08:00
>>> clf.fit(X, Y)
BayesianRidge(alpha_1=1e-06, alpha_2=1e-06, compute_score=False, copy_X=True,
fit_intercept=True, lambda_1=1e-06, lambda_2=1e-06, n_iter=300,
normalize=False, tol=0.001, verbose=False)
2010-09-08 19:46:36 +08:00
After being fitted, the model can then be used to predict new values::
2010-10-11 15:18:05 +08:00
>>> clf.predict ([[1, 0.]])
array([ 0.50000013])
2010-09-08 19:46:36 +08:00
2012-01-06 06:17:35 +08:00
The weights :math: `w` of the model can be access::
2010-09-08 19:46:36 +08:00
>>> clf.coef_
2010-10-11 15:18:05 +08:00
array([ 0.49999993, 0.49999993])
2010-09-08 19:46:36 +08:00
Due to the Bayesian framework, the weights found are slightly different to the
2012-01-06 18:18:10 +08:00
ones found by :ref: `ordinary_least_squares` . However, Bayesian Ridge Regression
is more robust to ill-posed problem.
2010-09-08 19:46:36 +08:00
2010-09-11 19:40:01 +08:00
.. topic :: Examples:
2010-09-08 19:46:36 +08:00
2010-11-25 21:53:55 +08:00
* :ref: `example_linear_model_plot_bayesian_ridge.py`
2010-09-08 19:46:36 +08:00
2010-09-11 19:40:01 +08:00
.. topic :: References
2010-09-08 19:46:36 +08:00
2012-01-06 18:18:10 +08:00
* More details can be found in the article `Bayesian Interpolation
<http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.27.9072&rep=rep1&type=pdf>`_
2010-09-11 19:40:01 +08:00
by MacKay, David J. C.
2010-09-08 19:46:36 +08:00
2011-07-25 18:57:42 +08:00
2010-09-08 19:46:36 +08:00
Automatic Relevance Determination - ARD
2012-01-06 06:17:35 +08:00
---------------------------------------
2010-09-08 19:46:36 +08:00
2012-01-06 06:17:35 +08:00
:class: `ARDRegression` is very similar to `Bayesian Ridge Regression`_ ,
2012-01-06 21:08:43 +08:00
but can lead to sparser weights :math: `w` [1]_ .
2012-01-06 18:18:10 +08:00
:class: `ARDRegression` poses a different prior over :math: `w` , by dropping the
2012-08-27 21:06:06 +08:00
assumption of the Gaussian being spherical.
2012-01-06 06:17:35 +08:00
Instead, the distribution over :math: `w` is assumed to be an axis-parallel,
elliptical Gaussian distribution.
2012-01-06 18:18:10 +08:00
This means each weight :math: `w_{i}` is drawn from a Gaussian distribution,
centered on zero and with a precision :math: `\lambda_{i}` :
2010-09-08 19:46:36 +08:00
2012-01-06 06:17:35 +08:00
.. math :: p(w|\lambda) = \mathcal{N}(w|0,A^{-1})
2010-09-08 19:46:36 +08:00
2010-09-08 23:32:21 +08:00
with :math: `diag \; (A) = \lambda = \{\lambda_{1},...,\lambda_{p}\}` .
2011-07-25 18:57:42 +08:00
2012-01-06 06:17:35 +08:00
In constrast to `Bayesian Ridge Regression`_ , each coordinate of :math: `w_{i}`
has its own standard deviation :math: `\lambda_i` . The prior over all
:math: `\lambda_i` is choosen to be the same gamma distribution given by
hyperparameters :math: `\lambda_1` and :math: `\lambda_2` .
2011-07-25 18:57:42 +08:00
.. figure :: ../auto_examples/linear_model/images/plot_ard_1.png
:target: ../auto_examples/linear_model/plot_ard.html
:align: center
2011-11-21 19:31:59 +08:00
:scale: 50%
2010-09-08 19:46:36 +08:00
2010-09-11 19:40:01 +08:00
.. topic :: Examples:
2010-11-25 21:53:55 +08:00
* :ref: `example_linear_model_plot_ard.py`
2010-09-08 19:46:36 +08:00
2012-01-06 21:08:43 +08:00
.. topic :: References:
2010-09-08 21:05:58 +08:00
2012-01-06 21:08:43 +08:00
.. [1] David Wipf and Srikantan Nagarajan: `A new view of automatic relevance determination. <http://books.nips.cc/papers/files/nips20/NIPS2007_0976.pdf> `_
2010-09-08 19:46:36 +08:00
2011-03-10 05:58:53 +08:00
.. _Logistic_regression:
2012-08-19 03:23:52 +08:00
Logistic regression
2012-08-26 20:19:15 +08:00
===================
2011-01-25 01:35:05 +08:00
2012-08-26 20:19:15 +08:00
Logistic regression, despite its name, is a linear model for classification
rather than regression.
As such, it minimizes a "hit or miss" cost function
rather than the sum of square residuals (as in ordinary regression).
Logistic regression is also known in the literature as
logit regression, maximum-entropy classification (MaxEnt)
or the log-linear classifier.
2011-01-25 01:35:05 +08:00
The :class: `LogisticRegression` class can be used to do L1 or L2 penalized
2011-04-26 15:31:32 +08:00
logistic regression. L1 penalization yields sparse predicting weights.
2011-09-02 17:00:02 +08:00
For L1 penalization :func: `sklearn.svm.l1_min_c` allows to calculate
2011-04-26 15:31:32 +08:00
the lower bound for C in order to get a non "null" (all feature weights to
zero) model.
2011-01-25 01:35:05 +08:00
.. topic :: Examples:
2012-03-11 22:29:25 +08:00
* :ref: `example_linear_model_plot_logistic_l1_l2_sparsity.py`
2011-01-25 01:35:05 +08:00
* :ref: `example_linear_model_plot_logistic_path.py`
2012-01-29 02:47:50 +08:00
.. note :: **Feature selection with sparse logistic regression**
A logistic regression with L1 penalty yields sparse models, and can
thus be used to perform feature selection, as detailed in
:ref: `l1_feature_selection` .
2010-12-02 18:14:02 +08:00
Stochastic Gradient Descent - SGD
=================================
2011-07-07 17:08:22 +08:00
Stochastic gradient descent is a simple yet very efficient approach
to fit linear models. It is particulary useful when the number of samples
2010-12-03 17:28:08 +08:00
(and the number of features) is very large.
2010-12-02 18:14:02 +08:00
2011-07-07 17:08:22 +08:00
The classes :class: `SGDClassifier` and :class: `SGDRegressor` provide
functionality to fit linear models for classification and regression
using different (convex) loss functions and different penalties.
2010-12-02 18:14:02 +08:00
.. topic :: References
* :ref: `sgd`
2012-01-29 01:53:19 +08:00
Perceptron
==========
The :class: `Perceptron` is another simple algorithm suitable for large scale
2012-01-29 20:17:33 +08:00
learning. By default:
2012-01-29 01:53:19 +08:00
- It does not require a learning rate.
- It is not regularized (penalized).
- It updates its model only on mistakes.
2012-01-29 20:17:33 +08:00
The last characteristic implies that the Perceptron is slightly faster to
train than SGD with the hinge loss and that the resulting models are
sparser.
2011-12-21 21:04:17 +08:00
2012-08-25 21:20:58 +08:00
Isotonic regression
====================
2012-09-17 18:33:28 +08:00
The :class: `IsotonicRegression` fits a non-decreasing function to the data.
2012-09-13 23:12:45 +08:00
It solves the following problem:
2012-08-25 21:20:58 +08:00
2012-09-16 00:40:23 +08:00
minimize :math: `\sum_i w_i (y_i - \hat{y}_i)^2`
2012-08-25 21:20:58 +08:00
2012-09-16 23:54:51 +08:00
subject to :math: `\hat{y}_{min} = \hat{y}_1 \le \hat{y}_2 ... \le \hat{y}_n = \hat{y}_{max}`
2012-08-25 21:20:58 +08:00
where each :math: `w_i` is strictly positive and each :math: `y_i` is an
2012-09-15 22:22:14 +08:00
arbitrary real number. It yields the vector which is composed of non-decreasing
elements the closest in terms of mean squared error. In practice this list
of elements forms a function that is piecewise linear.
2012-08-25 21:20:58 +08:00
2012-08-28 02:31:20 +08:00
.. figure :: ../auto_examples/linear_model/images/plot_isotonic_regression_1.png
:target: ../auto_examples/linear_model/images/plot_isotonic_regression.html
2012-08-25 21:20:58 +08:00
:align: center