From a9a5003dc2c4c8979fbc8dc8de66e500631782c8 Mon Sep 17 00:00:00 2001 From: Alexandre Gramfort <alexandre.gramfort@inria.fr> Date: Thu, 22 Apr 2010 17:58:50 +0000 Subject: [PATCH] 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 --- examples/plot_logistic_l1_l2_coef.py | 33 ++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 examples/plot_logistic_l1_l2_coef.py diff --git a/examples/plot_logistic_l1_l2_coef.py b/examples/plot_logistic_l1_l2_coef.py new file mode 100644 index 0000000000..2016debd7e --- /dev/null +++ b/examples/plot_logistic_l1_l2_coef.py @@ -0,0 +1,33 @@ +# 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 -- GitLab