Skip to content

Commit ce867cd

Browse files
committed
Pushing the docs to dev/ for branch: master, commit c1f58745be7c4923cf0a666f1c6bf052042f131e
1 parent 96fce91 commit ce867cd

File tree

1,062 files changed

+3734
-3588
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,062 files changed

+3734
-3588
lines changed
755 Bytes
Binary file not shown.
727 Bytes
Binary file not shown.

dev/_downloads/plot_logistic_l1_l2_sparsity.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"cell_type": "markdown",
1616
"metadata": {},
1717
"source": [
18-
"\n# L1 Penalty and Sparsity in Logistic Regression\n\n\nComparison of the sparsity (percentage of zero coefficients) of solutions when\nL1 and L2 penalty are used for different values of C. We can see that large\nvalues of C give more freedom to the model. Conversely, smaller values of C\nconstrain the model more. In the L1 penalty case, this leads to sparser\nsolutions.\n\nWe classify 8x8 images of digits into two classes: 0-4 against 5-9.\nThe visualization shows coefficients of the models for varying C.\n\n"
18+
"\n# L1 Penalty and Sparsity in Logistic Regression\n\n\nComparison of the sparsity (percentage of zero coefficients) of solutions when\nL1, L2 and Elastic-Net penalty are used for different values of C. We can see\nthat large values of C give more freedom to the model. Conversely, smaller\nvalues of C constrain the model more. In the L1 penalty case, this leads to\nsparser solutions. As expected, the Elastic-Net penalty sparsity is between\nthat of L1 and L2.\n\nWe classify 8x8 images of digits into two classes: 0-4 against 5-9.\nThe visualization shows coefficients of the models for varying C.\n\n"
1919
]
2020
},
2121
{
@@ -26,7 +26,7 @@
2626
},
2727
"outputs": [],
2828
"source": [
29-
"print(__doc__)\n\n# Authors: Alexandre Gramfort <[email protected]>\n# Mathieu Blondel <[email protected]>\n# Andreas Mueller <[email protected]>\n# License: BSD 3 clause\n\nimport numpy as np\nimport matplotlib.pyplot as plt\n\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn import datasets\nfrom sklearn.preprocessing import StandardScaler\n\ndigits = datasets.load_digits()\n\nX, y = digits.data, digits.target\nX = StandardScaler().fit_transform(X)\n\n# classify small against large digits\ny = (y > 4).astype(np.int)\n\n\n# Set regularization parameter\nfor i, C in enumerate((1, 0.1, 0.01)):\n # turn down tolerance for short training time\n clf_l1_LR = LogisticRegression(C=C, penalty='l1', tol=0.01, solver='saga')\n clf_l2_LR = LogisticRegression(C=C, penalty='l2', tol=0.01, solver='saga')\n clf_l1_LR.fit(X, y)\n clf_l2_LR.fit(X, y)\n\n coef_l1_LR = clf_l1_LR.coef_.ravel()\n coef_l2_LR = clf_l2_LR.coef_.ravel()\n\n # coef_l1_LR contains zeros due to the\n # L1 sparsity inducing norm\n\n sparsity_l1_LR = np.mean(coef_l1_LR == 0) * 100\n sparsity_l2_LR = np.mean(coef_l2_LR == 0) * 100\n\n print(\"C=%.2f\" % C)\n print(\"Sparsity with L1 penalty: %.2f%%\" % sparsity_l1_LR)\n print(\"score with L1 penalty: %.4f\" % clf_l1_LR.score(X, y))\n print(\"Sparsity with L2 penalty: %.2f%%\" % sparsity_l2_LR)\n print(\"score with L2 penalty: %.4f\" % clf_l2_LR.score(X, y))\n\n l1_plot = plt.subplot(3, 2, 2 * i + 1)\n l2_plot = plt.subplot(3, 2, 2 * (i + 1))\n if i == 0:\n l1_plot.set_title(\"L1 penalty\")\n l2_plot.set_title(\"L2 penalty\")\n\n l1_plot.imshow(np.abs(coef_l1_LR.reshape(8, 8)), interpolation='nearest',\n cmap='binary', vmax=1, vmin=0)\n l2_plot.imshow(np.abs(coef_l2_LR.reshape(8, 8)), interpolation='nearest',\n cmap='binary', vmax=1, vmin=0)\n plt.text(-8, 3, \"C = %.2f\" % C)\n\n l1_plot.set_xticks(())\n l1_plot.set_yticks(())\n l2_plot.set_xticks(())\n l2_plot.set_yticks(())\n\nplt.show()"
29+
"print(__doc__)\n\n# Authors: Alexandre Gramfort <[email protected]>\n# Mathieu Blondel <[email protected]>\n# Andreas Mueller <[email protected]>\n# License: BSD 3 clause\n\nimport numpy as np\nimport matplotlib.pyplot as plt\n\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn import datasets\nfrom sklearn.preprocessing import StandardScaler\n\ndigits = datasets.load_digits()\n\nX, y = digits.data, digits.target\nX = StandardScaler().fit_transform(X)\n\n# classify small against large digits\ny = (y > 4).astype(np.int)\n\nl1_ratio = 0.5 # L1 weight in the Elastic-Net regularization\n\nfig, axes = plt.subplots(3, 3)\n\n# Set regularization parameter\nfor i, (C, axes_row) in enumerate(zip((1, 0.1, 0.01), axes)):\n # turn down tolerance for short training time\n clf_l1_LR = LogisticRegression(C=C, penalty='l1', tol=0.01, solver='saga')\n clf_l2_LR = LogisticRegression(C=C, penalty='l2', tol=0.01, solver='saga')\n clf_en_LR = LogisticRegression(C=C, penalty='elasticnet', solver='saga',\n l1_ratio=l1_ratio, tol=0.01)\n clf_l1_LR.fit(X, y)\n clf_l2_LR.fit(X, y)\n clf_en_LR.fit(X, y)\n\n coef_l1_LR = clf_l1_LR.coef_.ravel()\n coef_l2_LR = clf_l2_LR.coef_.ravel()\n coef_en_LR = clf_en_LR.coef_.ravel()\n\n # coef_l1_LR contains zeros due to the\n # L1 sparsity inducing norm\n\n sparsity_l1_LR = np.mean(coef_l1_LR == 0) * 100\n sparsity_l2_LR = np.mean(coef_l2_LR == 0) * 100\n sparsity_en_LR = np.mean(coef_en_LR == 0) * 100\n\n print(\"C=%.2f\" % C)\n print(\"{:<40} {:.2f}%\".format(\"Sparsity with L1 penalty:\", sparsity_l1_LR))\n print(\"{:<40} {:.2f}%\".format(\"Sparsity with Elastic-Net penalty:\",\n sparsity_en_LR))\n print(\"{:<40} {:.2f}%\".format(\"Sparsity with L2 penalty:\", sparsity_l2_LR))\n print(\"{:<40} {:.2f}\".format(\"Score with L1 penalty:\",\n clf_l1_LR.score(X, y)))\n print(\"{:<40} {:.2f}\".format(\"Score with Elastic-Net penalty:\",\n clf_en_LR.score(X, y)))\n print(\"{:<40} {:.2f}\".format(\"Score with L2 penalty:\",\n clf_l2_LR.score(X, y)))\n\n if i == 0:\n axes_row[0].set_title(\"L1 penalty\")\n axes_row[1].set_title(\"Elastic-Net\\nl1_ratio = %s\" % l1_ratio)\n axes_row[2].set_title(\"L2 penalty\")\n\n for ax, coefs in zip(axes_row, [coef_l1_LR, coef_en_LR, coef_l2_LR]):\n ax.imshow(np.abs(coefs.reshape(8, 8)), interpolation='nearest',\n cmap='binary', vmax=1, vmin=0)\n ax.set_xticks(())\n ax.set_yticks(())\n\n axes_row[0].set_ylabel('C = %s' % C)\n\nplt.show()"
3030
]
3131
}
3232
],

dev/_downloads/plot_logistic_l1_l2_sparsity.py

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
==============================================
55
66
Comparison of the sparsity (percentage of zero coefficients) of solutions when
7-
L1 and L2 penalty are used for different values of C. We can see that large
8-
values of C give more freedom to the model. Conversely, smaller values of C
9-
constrain the model more. In the L1 penalty case, this leads to sparser
10-
solutions.
7+
L1, L2 and Elastic-Net penalty are used for different values of C. We can see
8+
that large values of C give more freedom to the model. Conversely, smaller
9+
values of C constrain the model more. In the L1 penalty case, this leads to
10+
sparser solutions. As expected, the Elastic-Net penalty sparsity is between
11+
that of L1 and L2.
1112
1213
We classify 8x8 images of digits into two classes: 0-4 against 5-9.
1314
The visualization shows coefficients of the models for varying C.
@@ -35,45 +36,55 @@
3536
# classify small against large digits
3637
y = (y > 4).astype(np.int)
3738

39+
l1_ratio = 0.5 # L1 weight in the Elastic-Net regularization
40+
41+
fig, axes = plt.subplots(3, 3)
3842

3943
# Set regularization parameter
40-
for i, C in enumerate((1, 0.1, 0.01)):
44+
for i, (C, axes_row) in enumerate(zip((1, 0.1, 0.01), axes)):
4145
# turn down tolerance for short training time
4246
clf_l1_LR = LogisticRegression(C=C, penalty='l1', tol=0.01, solver='saga')
4347
clf_l2_LR = LogisticRegression(C=C, penalty='l2', tol=0.01, solver='saga')
48+
clf_en_LR = LogisticRegression(C=C, penalty='elasticnet', solver='saga',
49+
l1_ratio=l1_ratio, tol=0.01)
4450
clf_l1_LR.fit(X, y)
4551
clf_l2_LR.fit(X, y)
52+
clf_en_LR.fit(X, y)
4653

4754
coef_l1_LR = clf_l1_LR.coef_.ravel()
4855
coef_l2_LR = clf_l2_LR.coef_.ravel()
56+
coef_en_LR = clf_en_LR.coef_.ravel()
4957

5058
# coef_l1_LR contains zeros due to the
5159
# L1 sparsity inducing norm
5260

5361
sparsity_l1_LR = np.mean(coef_l1_LR == 0) * 100
5462
sparsity_l2_LR = np.mean(coef_l2_LR == 0) * 100
63+
sparsity_en_LR = np.mean(coef_en_LR == 0) * 100
5564

5665
print("C=%.2f" % C)
57-
print("Sparsity with L1 penalty: %.2f%%" % sparsity_l1_LR)
58-
print("score with L1 penalty: %.4f" % clf_l1_LR.score(X, y))
59-
print("Sparsity with L2 penalty: %.2f%%" % sparsity_l2_LR)
60-
print("score with L2 penalty: %.4f" % clf_l2_LR.score(X, y))
66+
print("{:<40} {:.2f}%".format("Sparsity with L1 penalty:", sparsity_l1_LR))
67+
print("{:<40} {:.2f}%".format("Sparsity with Elastic-Net penalty:",
68+
sparsity_en_LR))
69+
print("{:<40} {:.2f}%".format("Sparsity with L2 penalty:", sparsity_l2_LR))
70+
print("{:<40} {:.2f}".format("Score with L1 penalty:",
71+
clf_l1_LR.score(X, y)))
72+
print("{:<40} {:.2f}".format("Score with Elastic-Net penalty:",
73+
clf_en_LR.score(X, y)))
74+
print("{:<40} {:.2f}".format("Score with L2 penalty:",
75+
clf_l2_LR.score(X, y)))
6176

62-
l1_plot = plt.subplot(3, 2, 2 * i + 1)
63-
l2_plot = plt.subplot(3, 2, 2 * (i + 1))
6477
if i == 0:
65-
l1_plot.set_title("L1 penalty")
66-
l2_plot.set_title("L2 penalty")
67-
68-
l1_plot.imshow(np.abs(coef_l1_LR.reshape(8, 8)), interpolation='nearest',
69-
cmap='binary', vmax=1, vmin=0)
70-
l2_plot.imshow(np.abs(coef_l2_LR.reshape(8, 8)), interpolation='nearest',
71-
cmap='binary', vmax=1, vmin=0)
72-
plt.text(-8, 3, "C = %.2f" % C)
73-
74-
l1_plot.set_xticks(())
75-
l1_plot.set_yticks(())
76-
l2_plot.set_xticks(())
77-
l2_plot.set_yticks(())
78+
axes_row[0].set_title("L1 penalty")
79+
axes_row[1].set_title("Elastic-Net\nl1_ratio = %s" % l1_ratio)
80+
axes_row[2].set_title("L2 penalty")
81+
82+
for ax, coefs in zip(axes_row, [coef_l1_LR, coef_en_LR, coef_l2_LR]):
83+
ax.imshow(np.abs(coefs.reshape(8, 8)), interpolation='nearest',
84+
cmap='binary', vmax=1, vmin=0)
85+
ax.set_xticks(())
86+
ax.set_yticks(())
87+
88+
axes_row[0].set_ylabel('C = %s' % C)
7889

7990
plt.show()

dev/_downloads/scikit-learn-docs.pdf

254 KB
Binary file not shown.

dev/_images/iris.png

0 Bytes

0 commit comments

Comments
 (0)