Skip to content

Commit e27ab92

Browse files
committed
Pushing the docs to dev/ for branch: master, commit 57c04f4e8d488352f020f9a89e516eda9fb88190
1 parent 4f716cb commit e27ab92

File tree

1,087 files changed

+4073
-3371
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,087 files changed

+4073
-3371
lines changed
4.27 KB
Binary file not shown.
3.28 KB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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# Curve Fitting with Bayesian Ridge Regression\n\n\nComputes a Bayesian Ridge Regression of Sinusoids.\n\nSee `bayesian_ridge_regression` for more information on the regressor.\n\nIn general, when fitting a curve with a polynomial by Bayesian ridge\nregression, the selection of initial values of\nthe regularization parameters (alpha, lambda) may be important.\nThis is because the regularization parameters are determined by an iterative\nprocedure that depends on initial values.\n\nIn this example, the sinusoid is approximated by a polynomial using different\npairs of initial values.\n\nWhen starting from the default values (alpha_init = 1.90, lambda_init = 1.),\nthe bias of the resulting curve is large, and the variance is small.\nSo, lambda_init should be relatively small (1.e-3) so as to reduce the bias.\n\nAlso, by evaluating log marginal likelihood (L) of\nthese models, we can determine which one is better.\nIt can be concluded that the model with larger L is more likely.\n\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__)\n\n# Author: Yoshihiro Uchida <[email protected]>\n\nimport numpy as np\nimport matplotlib.pyplot as plt\n\nfrom sklearn.linear_model import BayesianRidge\n\n\ndef func(x): return np.sin(2*np.pi*x)\n\n\n# #############################################################################\n# Generate sinusoidal data with noise\nsize = 25\nrng = np.random.RandomState(1234)\nx_train = rng.uniform(0., 1., size)\ny_train = func(x_train) + rng.normal(scale=0.1, size=size)\nx_test = np.linspace(0., 1., 100)\n\n\n# #############################################################################\n# Fit by cubic polynomial\nn_order = 3\nX_train = np.vander(x_train, n_order + 1, increasing=True)\nX_test = np.vander(x_test, n_order + 1, increasing=True)\n\n# #############################################################################\n# Plot the true and predicted curves with log marginal likelihood (L)\nreg = BayesianRidge(tol=1e-6, fit_intercept=False, compute_score=True)\nfig, axes = plt.subplots(1, 2, figsize=(8, 4))\nfor i, ax in enumerate(axes):\n # Bayesian ridge regression with different initial value pairs\n if i == 0:\n init = [1 / np.var(y_train), 1.] # Default values\n elif i == 1:\n init = [1., 1e-3]\n reg.set_params(alpha_init=init[0], lambda_init=init[1])\n reg.fit(X_train, y_train)\n ymean, ystd = reg.predict(X_test, return_std=True)\n\n ax.plot(x_test, func(x_test), color=\"blue\", label=\"sin($2\\\\pi x$)\")\n ax.scatter(x_train, y_train, s=50, alpha=0.5, label=\"observation\")\n ax.plot(x_test, ymean, color=\"red\", label=\"predict mean\")\n ax.fill_between(x_test, ymean-ystd, ymean+ystd,\n color=\"pink\", alpha=0.5, label=\"predict std\")\n ax.set_ylim(-1.3, 1.3)\n ax.legend()\n title = \"$\\\\alpha$_init$={:.2f},\\\\ \\\\lambda$_init$={}$\".format(\n init[0], init[1])\n if i == 0:\n title += \" (Default)\"\n ax.set_title(title, fontsize=12)\n text = \"$\\\\alpha={:.1f}$\\n$\\\\lambda={:.3f}$\\n$L={:.1f}$\".format(\n reg.alpha_, reg.lambda_, reg.scores_[-1])\n ax.text(0.05, -1.0, text, fontsize=12)\n\nplt.tight_layout()\nplt.show()"
30+
]
31+
}
32+
],
33+
"metadata": {
34+
"kernelspec": {
35+
"display_name": "Python 3",
36+
"language": "python",
37+
"name": "python3"
38+
},
39+
"language_info": {
40+
"codemirror_mode": {
41+
"name": "ipython",
42+
"version": 3
43+
},
44+
"file_extension": ".py",
45+
"mimetype": "text/x-python",
46+
"name": "python",
47+
"nbconvert_exporter": "python",
48+
"pygments_lexer": "ipython3",
49+
"version": "3.6.8"
50+
}
51+
},
52+
"nbformat": 4,
53+
"nbformat_minor": 0
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
"""
2+
============================================
3+
Curve Fitting with Bayesian Ridge Regression
4+
============================================
5+
6+
Computes a Bayesian Ridge Regression of Sinusoids.
7+
8+
See :ref:`bayesian_ridge_regression` for more information on the regressor.
9+
10+
In general, when fitting a curve with a polynomial by Bayesian ridge
11+
regression, the selection of initial values of
12+
the regularization parameters (alpha, lambda) may be important.
13+
This is because the regularization parameters are determined by an iterative
14+
procedure that depends on initial values.
15+
16+
In this example, the sinusoid is approximated by a polynomial using different
17+
pairs of initial values.
18+
19+
When starting from the default values (alpha_init = 1.90, lambda_init = 1.),
20+
the bias of the resulting curve is large, and the variance is small.
21+
So, lambda_init should be relatively small (1.e-3) so as to reduce the bias.
22+
23+
Also, by evaluating log marginal likelihood (L) of
24+
these models, we can determine which one is better.
25+
It can be concluded that the model with larger L is more likely.
26+
"""
27+
print(__doc__)
28+
29+
# Author: Yoshihiro Uchida <[email protected]>
30+
31+
import numpy as np
32+
import matplotlib.pyplot as plt
33+
34+
from sklearn.linear_model import BayesianRidge
35+
36+
37+
def func(x): return np.sin(2*np.pi*x)
38+
39+
40+
# #############################################################################
41+
# Generate sinusoidal data with noise
42+
size = 25
43+
rng = np.random.RandomState(1234)
44+
x_train = rng.uniform(0., 1., size)
45+
y_train = func(x_train) + rng.normal(scale=0.1, size=size)
46+
x_test = np.linspace(0., 1., 100)
47+
48+
49+
# #############################################################################
50+
# Fit by cubic polynomial
51+
n_order = 3
52+
X_train = np.vander(x_train, n_order + 1, increasing=True)
53+
X_test = np.vander(x_test, n_order + 1, increasing=True)
54+
55+
# #############################################################################
56+
# Plot the true and predicted curves with log marginal likelihood (L)
57+
reg = BayesianRidge(tol=1e-6, fit_intercept=False, compute_score=True)
58+
fig, axes = plt.subplots(1, 2, figsize=(8, 4))
59+
for i, ax in enumerate(axes):
60+
# Bayesian ridge regression with different initial value pairs
61+
if i == 0:
62+
init = [1 / np.var(y_train), 1.] # Default values
63+
elif i == 1:
64+
init = [1., 1e-3]
65+
reg.set_params(alpha_init=init[0], lambda_init=init[1])
66+
reg.fit(X_train, y_train)
67+
ymean, ystd = reg.predict(X_test, return_std=True)
68+
69+
ax.plot(x_test, func(x_test), color="blue", label="sin($2\\pi x$)")
70+
ax.scatter(x_train, y_train, s=50, alpha=0.5, label="observation")
71+
ax.plot(x_test, ymean, color="red", label="predict mean")
72+
ax.fill_between(x_test, ymean-ystd, ymean+ystd,
73+
color="pink", alpha=0.5, label="predict std")
74+
ax.set_ylim(-1.3, 1.3)
75+
ax.legend()
76+
title = "$\\alpha$_init$={:.2f},\\ \\lambda$_init$={}$".format(
77+
init[0], init[1])
78+
if i == 0:
79+
title += " (Default)"
80+
ax.set_title(title, fontsize=12)
81+
text = "$\\alpha={:.1f}$\n$\\lambda={:.3f}$\n$L={:.1f}$".format(
82+
reg.alpha_, reg.lambda_, reg.scores_[-1])
83+
ax.text(0.05, -1.0, text, fontsize=12)
84+
85+
plt.tight_layout()
86+
plt.show()

dev/_downloads/scikit-learn-docs.pdf

40.2 KB
Binary file not shown.

dev/_images/iris.png

0 Bytes
-591 Bytes
-103 Bytes
-103 Bytes
-1.49 KB
-155 Bytes
-155 Bytes
-136 Bytes
-136 Bytes
-275 Bytes
-275 Bytes
-224 Bytes
-182 Bytes
-200 Bytes
8 Bytes
8 Bytes
-33 Bytes
-33 Bytes
138 Bytes
138 Bytes
174 Bytes
174 Bytes
38 Bytes
38 Bytes
-115 Bytes
-115 Bytes
2 Bytes
40 Bytes
-18 Bytes
-18 Bytes
-33 Bytes
112 Bytes
-131 Bytes
-131 Bytes
-182 Bytes
-47 Bytes
-47 Bytes
14 Bytes
0 Bytes

dev/_sources/auto_examples/applications/plot_face_recognition.rst.txt

+17-17

dev/_sources/auto_examples/applications/plot_model_complexity_influence.rst.txt

+14-14

0 commit comments

Comments
 (0)