.. _example_linear_model_plot_lasso_model_selection.py:


===================================================
Lasso model selection: Cross-Validation / AIC / BIC
===================================================

Use the Akaike information criterion (AIC), the Bayes Information
criterion (BIC) and cross-validation to select an optimal value
of the regularization parameter alpha of the :ref:`lasso` estimator.

Results obtained with LassoLarsIC are based on AIC/BIC criteria.

Information-criterion based model selection is very fast, but it
relies on a proper estimation of degrees of freedom, 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).

For cross-validation, we use 20-fold with 2 algorithms to compute the
Lasso path: coordinate descent, as implemented by the LassoCV class, and
Lars (least angle regression) as implemented by the LassoLarsCV class.
Both algorithms give roughly the same results. They differ with regards
to their execution speed and sources of numerical errors.

Lars computes a path solution only for each kink in the path. As a
result, it is very efficient when there are only of few kinks, which is
the case if there are few features or samples. Also, it is able to
compute the full path without setting any meta parameter. On the
opposite, coordinate descent compute the path points on a pre-specified
grid (here we use the default). Thus it is more efficient if the number
of grid points is smaller than the number of kinks in the path. Such a
strategy can be interesting if the number of features is really large
and there are enough samples to select a large amount. In terms of
numerical errors, for heavily correlated variables, Lars will accumulate
more errors, while the coordinate descent algorithm will only sample the
path on a grid.

Note how the optimal value of alpha varies for each fold. This
illustrates why nested-cross validation is necessary when trying to
evaluate the performance of a method for which a parameter is chosen by
cross-validation: this choice of parameter may not be optimal for unseen
data.



.. rst-class:: horizontal


    *

      .. image:: images/plot_lasso_model_selection_001.png
            :scale: 47

    *

      .. image:: images/plot_lasso_model_selection_002.png
            :scale: 47

    *

      .. image:: images/plot_lasso_model_selection_003.png
            :scale: 47


**Script output**::

  Computing regularization path using the coordinate descent lasso...
  Computing regularization path using the Lars lasso...



**Python source code:** :download:`plot_lasso_model_selection.py <plot_lasso_model_selection.py>`

.. literalinclude:: plot_lasso_model_selection.py
    :lines: 44-

**Total running time of the example:**  0.69 seconds
( 0 minutes  0.69 seconds)