Skip to content

Commit 37edb52

Browse files
committed
Pushing the docs to dev/ for branch: master, commit 2d95acfe95c28bd8e8a5793021a43ff71c5c6864
1 parent b2ec78a commit 37edb52

File tree

1,260 files changed

+5001
-4091
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,260 files changed

+5001
-4091
lines changed
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {
7+
"collapsed": false
8+
},
9+
"outputs": [],
10+
"source": [
11+
"%matplotlib inline"
12+
]
13+
},
14+
{
15+
"cell_type": "markdown",
16+
"metadata": {},
17+
"source": [
18+
"\n# Fitting an Elastic Net with a precomputed Gram Matrix and Weighted Samples\n\nThe following example shows how to precompute the gram matrix\nwhile using weighted samples with an ElasticNet.\n\nIf weighted samples are used, the design matrix must be centered and then\nrescaled by the square root of the weight vector before the gram matrix\nis computed.\n\n<div class=\"alert alert-info\"><h4>Note</h4><p>`sample_weight` vector is also rescaled to sum to `n_samples`, see the\n documentation for the `sample_weight` parameter to\n :func:`linear_model.ElasticNet.fit`.</p></div>\n"
19+
]
20+
},
21+
{
22+
"cell_type": "code",
23+
"execution_count": null,
24+
"metadata": {
25+
"collapsed": false
26+
},
27+
"outputs": [],
28+
"source": [
29+
"print(__doc__)"
30+
]
31+
},
32+
{
33+
"cell_type": "markdown",
34+
"metadata": {},
35+
"source": [
36+
"Let's start by loading the dataset and creating some sample weights.\n\n"
37+
]
38+
},
39+
{
40+
"cell_type": "code",
41+
"execution_count": null,
42+
"metadata": {
43+
"collapsed": false
44+
},
45+
"outputs": [],
46+
"source": [
47+
"import numpy as np\nfrom sklearn.datasets import make_regression\n\nrng = np.random.RandomState(0)\n\nn_samples = int(1e5)\nX, y = make_regression(n_samples=n_samples, noise=0.5, random_state=rng)\n\nsample_weight = rng.lognormal(size=n_samples)\n# normalize the sample weights\nnormalized_weights = sample_weight * (n_samples / (sample_weight.sum()))"
48+
]
49+
},
50+
{
51+
"cell_type": "markdown",
52+
"metadata": {},
53+
"source": [
54+
"To fit the elastic net using the `precompute` option together with the sample\nweights, we must first center the design matrix, and rescale it by the\nnormalized weights prior to computing the gram matrix.\n\n"
55+
]
56+
},
57+
{
58+
"cell_type": "code",
59+
"execution_count": null,
60+
"metadata": {
61+
"collapsed": false
62+
},
63+
"outputs": [],
64+
"source": [
65+
"X_offset = np.average(X, axis=0, weights=normalized_weights)\nX_centered = (X - np.average(X, axis=0, weights=normalized_weights))\nX_scaled = X_centered * np.sqrt(normalized_weights)[:, np.newaxis]\ngram = np.dot(X_scaled.T, X_scaled)"
66+
]
67+
},
68+
{
69+
"cell_type": "markdown",
70+
"metadata": {},
71+
"source": [
72+
"We can now proceed with fitting. We must passed the centered design matrix to\n`fit` otherwise the elastic net estimator will detect that it is uncentered\nand discard the gram matrix we passed. However, if we pass the scaled design\nmatrix, the preprocessing code will incorrectly rescale it a second time.\n\n"
73+
]
74+
},
75+
{
76+
"cell_type": "code",
77+
"execution_count": null,
78+
"metadata": {
79+
"collapsed": false
80+
},
81+
"outputs": [],
82+
"source": [
83+
"from sklearn.linear_model import ElasticNet\n\nlm = ElasticNet(alpha=0.01, precompute=gram)\nlm.fit(X_centered, y, sample_weight=normalized_weights)"
84+
]
85+
}
86+
],
87+
"metadata": {
88+
"kernelspec": {
89+
"display_name": "Python 3",
90+
"language": "python",
91+
"name": "python3"
92+
},
93+
"language_info": {
94+
"codemirror_mode": {
95+
"name": "ipython",
96+
"version": 3
97+
},
98+
"file_extension": ".py",
99+
"mimetype": "text/x-python",
100+
"name": "python",
101+
"nbconvert_exporter": "python",
102+
"pygments_lexer": "ipython3",
103+
"version": "3.9.1"
104+
}
105+
},
106+
"nbformat": 4,
107+
"nbformat_minor": 0
108+
}
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
"""
2+
==========================================================================
3+
Fitting an Elastic Net with a precomputed Gram Matrix and Weighted Samples
4+
==========================================================================
5+
6+
The following example shows how to precompute the gram matrix
7+
while using weighted samples with an ElasticNet.
8+
9+
If weighted samples are used, the design matrix must be centered and then
10+
rescaled by the square root of the weight vector before the gram matrix
11+
is computed.
12+
13+
.. note::
14+
`sample_weight` vector is also rescaled to sum to `n_samples`, see the
15+
documentation for the `sample_weight` parameter to
16+
:func:`linear_model.ElasticNet.fit`.
17+
18+
"""
19+
20+
print(__doc__)
21+
22+
# %%
23+
# Let's start by loading the dataset and creating some sample weights.
24+
import numpy as np
25+
from sklearn.datasets import make_regression
26+
27+
rng = np.random.RandomState(0)
28+
29+
n_samples = int(1e5)
30+
X, y = make_regression(n_samples=n_samples, noise=0.5, random_state=rng)
31+
32+
sample_weight = rng.lognormal(size=n_samples)
33+
# normalize the sample weights
34+
normalized_weights = sample_weight * (n_samples / (sample_weight.sum()))
35+
36+
# %%
37+
# To fit the elastic net using the `precompute` option together with the sample
38+
# weights, we must first center the design matrix, and rescale it by the
39+
# normalized weights prior to computing the gram matrix.
40+
X_offset = np.average(X, axis=0, weights=normalized_weights)
41+
X_centered = (X - np.average(X, axis=0, weights=normalized_weights))
42+
X_scaled = X_centered * np.sqrt(normalized_weights)[:, np.newaxis]
43+
gram = np.dot(X_scaled.T, X_scaled)
44+
45+
# %%
46+
# We can now proceed with fitting. We must passed the centered design matrix to
47+
# `fit` otherwise the elastic net estimator will detect that it is uncentered
48+
# and discard the gram matrix we passed. However, if we pass the scaled design
49+
# matrix, the preprocessing code will incorrectly rescale it a second time.
50+
from sklearn.linear_model import ElasticNet
51+
52+
lm = ElasticNet(alpha=0.01, precompute=gram)
53+
lm.fit(X_centered, y, sample_weight=normalized_weights)

dev/_downloads/scikit-learn-docs.pdf

5.36 KB
Binary file not shown.

dev/_images/binder_badge_logo.png

0 Bytes

dev/_images/iris.png

0 Bytes
-337 Bytes
-337 Bytes
-311 Bytes

0 commit comments

Comments
 (0)