{"id":304,"date":"2026-10-22T23:16:10","date_gmt":"2026-10-22T23:16:10","guid":{"rendered":"https:\/\/stephendavies.org\/data419\/?p=304"},"modified":"2026-10-23T16:01:26","modified_gmt":"2026-10-23T16:01:26","slug":"304","status":"publish","type":"post","link":"https:\/\/stephendavies.org\/data419\/index.php\/2026\/10\/22\/304\/","title":{"rendered":"Regularization in Logistic Regression"},"content":{"rendered":"<p>I was reminded by <a href=\"http:\/\/stephendavies.org\/data419\/homework3.html\">a Kaggler<\/a> today that although I showed you guys sample code to add regularization for <em>linear<\/em> regression (<code>Lasso<\/code> and <code>Ridge<\/code>) I never explained how to do this for classification (<em>logistic<\/em> regression).<\/p>\n<p>First off, fun fact: it turns out that sklearn\u2019s <code>LogisticRegression<\/code> already uses regularization by default! Specifically, it uses <strong>L2 (ridge) regularization<\/strong>.<\/p>\n<p>To control the regularization in <code>LogisticRegression<\/code>, there are three important parameters:<\/p>\n<ul>\n<li><strong><code>C<\/code><\/strong>: Controls the strength of regularization. Unlike <code>alpha<\/code> in <code>Ridge<\/code> and <code>Lasso<\/code>, <strong>smaller values mean stronger regularization<\/strong>. The default is <code>C=1.0<\/code>.<\/li>\n<li><strong><code>l1_ratio<\/code><\/strong>: Controls the <em>type<\/em> of regularization:\n<ul>\n<li><code>0<\/code>: Pure L2 (ridge)<\/li>\n<li><code>1<\/code>: Pure L1 (lasso)<\/li>\n<li>Between 0 and 1: A mixture of L1 and L2 (elastic net)<\/li>\n<\/ul>\n<\/li>\n<li><strong><code>solver<\/code><\/strong>: Specifies the optimization algorithm. Use <code>solver=\"saga\"<\/code> if you want L1 or elastic net regularization.<\/li>\n<\/ul>\n<p>Here are some examples:<\/p>\n<div class=\"sourceCode\" id=\"cb1\">\n<pre\r\nclass=\"sourceCode python\"><code class=\"sourceCode python\"><span id=\"cb1-1\"><a href=\"#cb1-1\" aria-hidden=\"true\" tabindex=\"-1\"><\/a><span class=\"im\">from<\/span> sklearn.linear_model <span class=\"im\">import<\/span> LogisticRegression<\/span>\r\n<span id=\"cb1-2\"><a href=\"#cb1-2\" aria-hidden=\"true\" tabindex=\"-1\"><\/a><\/span>\r\n<span id=\"cb1-3\"><a href=\"#cb1-3\" aria-hidden=\"true\" tabindex=\"-1\"><\/a><span class=\"co\"># Ridge (L2) regularization \u2014 the default<\/span><\/span>\r\n<span id=\"cb1-4\"><a href=\"#cb1-4\" aria-hidden=\"true\" tabindex=\"-1\"><\/a>model <span class=\"op\">=<\/span> LogisticRegression(C<span class=\"op\">=<\/span><span class=\"fl\">1.0<\/span>)<\/span>\r\n<span id=\"cb1-5\"><a href=\"#cb1-5\" aria-hidden=\"true\" tabindex=\"-1\"><\/a><\/span>\r\n<span id=\"cb1-6\"><a href=\"#cb1-6\" aria-hidden=\"true\" tabindex=\"-1\"><\/a><span class=\"co\"># Stronger ridge regularization<\/span><\/span>\r\n<span id=\"cb1-7\"><a href=\"#cb1-7\" aria-hidden=\"true\" tabindex=\"-1\"><\/a>model <span class=\"op\">=<\/span> LogisticRegression(C<span class=\"op\">=<\/span><span class=\"fl\">0.1<\/span>)<\/span>\r\n<span id=\"cb1-8\"><a href=\"#cb1-8\" aria-hidden=\"true\" tabindex=\"-1\"><\/a><\/span>\r\n<span id=\"cb1-9\"><a href=\"#cb1-9\" aria-hidden=\"true\" tabindex=\"-1\"><\/a><span class=\"co\"># Lasso (L1) regularization<\/span><\/span>\r\n<span id=\"cb1-10\"><a href=\"#cb1-10\" aria-hidden=\"true\" tabindex=\"-1\"><\/a>model <span class=\"op\">=<\/span> LogisticRegression(C<span class=\"op\">=<\/span><span class=\"fl\">1.0<\/span>, l1_ratio<span class=\"op\">=<\/span><span class=\"dv\">1<\/span>, solver<span class=\"op\">=<\/span><span class=\"st\">&quot;saga&quot;<\/span>)<\/span>\r\n<span id=\"cb1-11\"><a href=\"#cb1-11\" aria-hidden=\"true\" tabindex=\"-1\"><\/a><\/span>\r\n<span id=\"cb1-12\"><a href=\"#cb1-12\" aria-hidden=\"true\" tabindex=\"-1\"><\/a><span class=\"co\"># Elastic net (50% L1, 50% L2)<\/span><\/span>\r\n<span id=\"cb1-13\"><a href=\"#cb1-13\" aria-hidden=\"true\" tabindex=\"-1\"><\/a>model <span class=\"op\">=<\/span> LogisticRegression(C<span class=\"op\">=<\/span><span class=\"fl\">1.0<\/span>, l1_ratio<span class=\"op\">=<\/span><span class=\"fl\">0.5<\/span>, solver<span class=\"op\">=<\/span><span class=\"st\">&quot;saga&quot;<\/span>)<\/span>\r\n<span id=\"cb1-14\"><a href=\"#cb1-14\" aria-hidden=\"true\" tabindex=\"-1\"><\/a><\/span>\r\n<span id=\"cb1-15\"><a href=\"#cb1-15\" aria-hidden=\"true\" tabindex=\"-1\"><\/a><span class=\"co\"># No regularization at all<\/span><\/span>\r\n<span id=\"cb1-16\"><a href=\"#cb1-16\" aria-hidden=\"true\" tabindex=\"-1\"><\/a>model <span class=\"op\">=<\/span> LogisticRegression(C<span class=\"op\">=<\/span><span class=\"bu\">float<\/span>(<span class=\"st\">&quot;inf&quot;<\/span>))<\/span><\/code><\/pre>\n<\/div>\n<p><strong>Important:<\/strong> You should generally scale your numerical features before applying regularization, since the penalty depends on the magnitudes of the coefficients.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was reminded by a Kaggler today that although I showed you guys sample code to add regularization for linear regression (Lasso and Ridge) I never explained how to do this for classification (logistic regression). First off, fun fact: it turns out that sklearn\u2019s LogisticRegression already uses regularization by default! Specifically, it uses L2 (ridge) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_links_to":"","_links_to_target":""},"categories":[1],"tags":[],"class_list":["post-304","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/stephendavies.org\/data419\/index.php\/wp-json\/wp\/v2\/posts\/304","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/stephendavies.org\/data419\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/stephendavies.org\/data419\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/stephendavies.org\/data419\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/stephendavies.org\/data419\/index.php\/wp-json\/wp\/v2\/comments?post=304"}],"version-history":[{"count":6,"href":"https:\/\/stephendavies.org\/data419\/index.php\/wp-json\/wp\/v2\/posts\/304\/revisions"}],"predecessor-version":[{"id":310,"href":"https:\/\/stephendavies.org\/data419\/index.php\/wp-json\/wp\/v2\/posts\/304\/revisions\/310"}],"wp:attachment":[{"href":"https:\/\/stephendavies.org\/data419\/index.php\/wp-json\/wp\/v2\/media?parent=304"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/stephendavies.org\/data419\/index.php\/wp-json\/wp\/v2\/categories?post=304"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/stephendavies.org\/data419\/index.php\/wp-json\/wp\/v2\/tags?post=304"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}