

.. _example_linear_model_plot_logistic_l1_l2_sparsity.py:


==============================================
L1 Penalty and Sparsity in Logistic Regression
==============================================

Comparison of the sparsity (percentage of zero coefficients) of solutions when
L1 and L2 penalty are used for different values of C. We can see that large
values of C give more freedom to the model.  Conversely, smaller values of C
constrain the model more. In the L1 penalty case, this leads to sparser
solutions.

We classify 8x8 images of digits into two classes: 0-4 against 5-9.
The visualization shows coefficients of the models for varying C.



.. image:: images/plot_logistic_l1_l2_sparsity_1.png
    :align: center


**Script output**::

  C=10.000000
  Sparsity with L1 penalty: 7.812500
  score with L1 penalty: 0.909293
  Sparsity with L2 penalty: 4.687500
  score with L2 penalty: 0.909293
  C=100.000000
  Sparsity with L1 penalty: 6.250000
  score with L1 penalty: 0.909850
  Sparsity with L2 penalty: 4.687500
  score with L2 penalty: 0.909850
  C=1000.000000
  Sparsity with L1 penalty: 4.687500
  score with L1 penalty: 0.909850
  Sparsity with L2 penalty: 4.687500
  score with L2 penalty: 0.909850



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

.. literalinclude:: plot_logistic_l1_l2_sparsity.py
    :lines: 15-
    