Skip to content
Snippets Groups Projects
Commit a9a5003d authored by Alexandre Gramfort's avatar Alexandre Gramfort
Browse files

adding example that illustrates that L1 penalty leads to sparse estimates of coef_

git-svn-id: https://scikit-learn.svn.sourceforge.net/svnroot/scikit-learn/trunk@697 22fbfee3-77ab-4535-9bad-27d1bd3bc7d8
parent a00f908f
No related branches found
No related tags found
No related merge requests found
# Author: Alexandre Gramfort <alexandre.gramfort@inria.fr>
# License: BSD Style.
# $Id$
import numpy as np
from scikits.learn.logistic import LogisticRegression
from scikits.learn import datasets
iris = datasets.load_iris()
X = iris.data
y = iris.target
# Set regularization parameter
C = 0.1
classifier_l1_LR = LogisticRegression(C=C, penalty='l1')
classifier_l2_LR = LogisticRegression(C=C, penalty='l2')
classifier_l1_LR.fit(X, y)
classifier_l2_LR.fit(X, y)
hyperplane_coefficients_l1_LR = classifier_l1_LR.coef_[:]
hyperplane_coefficients_l2_LR = classifier_l2_LR.coef_[:]
# hyperplane_coefficients_l1_LR contains zeros due to the
# L1 sparsity inducing norm
pct_non_zeros_l1_LR = np.mean(hyperplane_coefficients_l1_LR != 0) * 100
pct_non_zeros_l2_LR = np.mean(hyperplane_coefficients_l2_LR != 0) * 100
print "Percentage of non zeros coefficients (L1) : %f" % pct_non_zeros_l1_LR
print "Percentage of non zeros coefficients (L2) : %f" % pct_non_zeros_l2_LR
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment