Skip to content

Commit 97a1fc3

Browse files
committed
Pushing the docs to dev/ for branch: master, commit acb8ac5145cfd88fdbd2d381b34883b2c212c8c5
1 parent fc715c5 commit 97a1fc3

File tree

1,205 files changed

+3897
-3700
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,205 files changed

+3897
-3700
lines changed
Binary file not shown.

dev/_downloads/8209ef76ac59bf01aad3721a522859ef/plot_isotonic_regression.py

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,19 @@
33
Isotonic Regression
44
===================
55
6-
An illustration of the isotonic regression on generated data. The
7-
isotonic regression finds a non-decreasing approximation of a function
8-
while minimizing the mean squared error on the training data. The benefit
9-
of such a model is that it does not assume any form for the target
10-
function such as linearity. For comparison a linear regression is also
11-
presented.
6+
An illustration of the isotonic regression on generated data (non-linear
7+
monotonic trend with homoscedastic uniform noise).
8+
9+
The isotonic regression algorithm finds a non-decreasing approximation of a
10+
function while minimizing the mean squared error on the training data. The
11+
benefit of such a non-parametric model is that it does not assume any shape for
12+
the target function besides monotonicity. For comparison a linear regression is
13+
also presented.
14+
15+
The plot on the right-hand side shows the model prediction function that
16+
results from the linear interpolation of thresholds points. The thresholds
17+
points are a subset of the training input observations and their matching
18+
target values are computed by the isotonic non-parametric fit.
1219
1320
"""
1421
print(__doc__)
@@ -30,29 +37,41 @@
3037
rs = check_random_state(0)
3138
y = rs.randint(-50, 50, size=(n,)) + 50. * np.log1p(np.arange(n))
3239

33-
# #############################################################################
34-
# Fit IsotonicRegression and LinearRegression models
35-
36-
ir = IsotonicRegression()
40+
# %%
41+
# Fit IsotonicRegression and LinearRegression models:
3742

43+
ir = IsotonicRegression(out_of_bounds="clip")
3844
y_ = ir.fit_transform(x, y)
3945

4046
lr = LinearRegression()
4147
lr.fit(x[:, np.newaxis], y) # x needs to be 2d for LinearRegression
4248

43-
# #############################################################################
44-
# Plot result
49+
# %%
50+
# Plot results:
4551

4652
segments = [[[i, y[i]], [i, y_[i]]] for i in range(n)]
4753
lc = LineCollection(segments, zorder=0)
4854
lc.set_array(np.ones(len(y)))
4955
lc.set_linewidths(np.full(n, 0.5))
5056

51-
fig = plt.figure()
52-
plt.plot(x, y, 'r.', markersize=12)
53-
plt.plot(x, y_, 'b.-', markersize=12)
54-
plt.plot(x, lr.predict(x[:, np.newaxis]), 'b-')
55-
plt.gca().add_collection(lc)
56-
plt.legend(('Data', 'Isotonic Fit', 'Linear Fit'), loc='lower right')
57-
plt.title('Isotonic regression')
57+
fig, (ax0, ax1) = plt.subplots(ncols=2, figsize=(12, 6))
58+
59+
ax0.plot(x, y, 'C0.', markersize=12)
60+
ax0.plot(x, y_, 'C1.-', markersize=12)
61+
ax0.plot(x, lr.predict(x[:, np.newaxis]), 'C2-')
62+
ax0.add_collection(lc)
63+
ax0.legend(('Training data', 'Isotonic fit', 'Linear fit'), loc='lower right')
64+
ax0.set_title('Isotonic regression fit on noisy data (n=%d)' % n)
65+
66+
x_test = np.linspace(-10, 110, 1000)
67+
ax1.plot(x_test, ir.predict(x_test), 'C1-')
68+
ax1.plot(ir.X_thresholds_, ir.y_thresholds_, 'C1.', markersize=12)
69+
ax1.set_title("Prediction function (%d thresholds)" % len(ir.X_thresholds_))
70+
5871
plt.show()
72+
73+
# %%
74+
# Note that we explicitly passed `out_of_bounds="clip"` to the constructor of
75+
# `IsotonicRegression` to control the way the model extrapolates outside of the
76+
# range of data observed in the training set. This "clipping" extrapolation can
77+
# be seen on the plot of the decision function on the right-hand.
Binary file not shown.

dev/_downloads/f2e78295c97b04635d9e749896f8e08b/plot_isotonic_regression.ipynb

Lines changed: 45 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# Isotonic Regression\n\n\nAn illustration of the isotonic regression on generated data. The\nisotonic regression finds a non-decreasing approximation of a function\nwhile minimizing the mean squared error on the training data. The benefit\nof such a model is that it does not assume any form for the target\nfunction such as linearity. For comparison a linear regression is also\npresented.\n"
18+
"\n# Isotonic Regression\n\n\nAn illustration of the isotonic regression on generated data (non-linear\nmonotonic trend with homoscedastic uniform noise).\n\nThe isotonic regression algorithm finds a non-decreasing approximation of a\nfunction while minimizing the mean squared error on the training data. The\nbenefit of such a non-parametric model is that it does not assume any shape for\nthe target function besides monotonicity. For comparison a linear regression is\nalso presented.\n\nThe plot on the right-hand side shows the model prediction function that\nresults from the linear interpolation of thresholds points. The thresholds\npoints are a subset of the training input observations and their matching\ntarget values are computed by the isotonic non-parametric fit.\n"
1919
]
2020
},
2121
{
@@ -26,7 +26,50 @@
2626
},
2727
"outputs": [],
2828
"source": [
29-
"print(__doc__)\n\n# Author: Nelle Varoquaux <[email protected]>\n# Alexandre Gramfort <[email protected]>\n# License: BSD\n\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom matplotlib.collections import LineCollection\n\nfrom sklearn.linear_model import LinearRegression\nfrom sklearn.isotonic import IsotonicRegression\nfrom sklearn.utils import check_random_state\n\nn = 100\nx = np.arange(n)\nrs = check_random_state(0)\ny = rs.randint(-50, 50, size=(n,)) + 50. * np.log1p(np.arange(n))\n\n# #############################################################################\n# Fit IsotonicRegression and LinearRegression models\n\nir = IsotonicRegression()\n\ny_ = ir.fit_transform(x, y)\n\nlr = LinearRegression()\nlr.fit(x[:, np.newaxis], y) # x needs to be 2d for LinearRegression\n\n# #############################################################################\n# Plot result\n\nsegments = [[[i, y[i]], [i, y_[i]]] for i in range(n)]\nlc = LineCollection(segments, zorder=0)\nlc.set_array(np.ones(len(y)))\nlc.set_linewidths(np.full(n, 0.5))\n\nfig = plt.figure()\nplt.plot(x, y, 'r.', markersize=12)\nplt.plot(x, y_, 'b.-', markersize=12)\nplt.plot(x, lr.predict(x[:, np.newaxis]), 'b-')\nplt.gca().add_collection(lc)\nplt.legend(('Data', 'Isotonic Fit', 'Linear Fit'), loc='lower right')\nplt.title('Isotonic regression')\nplt.show()"
29+
"print(__doc__)\n\n# Author: Nelle Varoquaux <[email protected]>\n# Alexandre Gramfort <[email protected]>\n# License: BSD\n\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom matplotlib.collections import LineCollection\n\nfrom sklearn.linear_model import LinearRegression\nfrom sklearn.isotonic import IsotonicRegression\nfrom sklearn.utils import check_random_state\n\nn = 100\nx = np.arange(n)\nrs = check_random_state(0)\ny = rs.randint(-50, 50, size=(n,)) + 50. * np.log1p(np.arange(n))"
30+
]
31+
},
32+
{
33+
"cell_type": "markdown",
34+
"metadata": {},
35+
"source": [
36+
"Fit IsotonicRegression and LinearRegression models:\n\n"
37+
]
38+
},
39+
{
40+
"cell_type": "code",
41+
"execution_count": null,
42+
"metadata": {
43+
"collapsed": false
44+
},
45+
"outputs": [],
46+
"source": [
47+
"ir = IsotonicRegression(out_of_bounds=\"clip\")\ny_ = ir.fit_transform(x, y)\n\nlr = LinearRegression()\nlr.fit(x[:, np.newaxis], y) # x needs to be 2d for LinearRegression"
48+
]
49+
},
50+
{
51+
"cell_type": "markdown",
52+
"metadata": {},
53+
"source": [
54+
"Plot results:\n\n"
55+
]
56+
},
57+
{
58+
"cell_type": "code",
59+
"execution_count": null,
60+
"metadata": {
61+
"collapsed": false
62+
},
63+
"outputs": [],
64+
"source": [
65+
"segments = [[[i, y[i]], [i, y_[i]]] for i in range(n)]\nlc = LineCollection(segments, zorder=0)\nlc.set_array(np.ones(len(y)))\nlc.set_linewidths(np.full(n, 0.5))\n\nfig, (ax0, ax1) = plt.subplots(ncols=2, figsize=(12, 6))\n\nax0.plot(x, y, 'C0.', markersize=12)\nax0.plot(x, y_, 'C1.-', markersize=12)\nax0.plot(x, lr.predict(x[:, np.newaxis]), 'C2-')\nax0.add_collection(lc)\nax0.legend(('Training data', 'Isotonic fit', 'Linear fit'), loc='lower right')\nax0.set_title('Isotonic regression fit on noisy data (n=%d)' % n)\n\nx_test = np.linspace(-10, 110, 1000)\nax1.plot(x_test, ir.predict(x_test), 'C1-')\nax1.plot(ir.X_thresholds_, ir.y_thresholds_, 'C1.', markersize=12)\nax1.set_title(\"Prediction function (%d thresholds)\" % len(ir.X_thresholds_))\n\nplt.show()"
66+
]
67+
},
68+
{
69+
"cell_type": "markdown",
70+
"metadata": {},
71+
"source": [
72+
"Note that we explicitly passed `out_of_bounds=\"clip\"` to the constructor of\n`IsotonicRegression` to control the way the model extrapolates outside of the\nrange of data observed in the training set. This \"clipping\" extrapolation can\nbe seen on the plot of the decision function on the right-hand.\n\n"
3073
]
3174
}
3275
],

dev/_downloads/scikit-learn-docs.pdf

48.7 KB
Binary file not shown.

dev/_images/iris.png

0 Bytes

0 commit comments

Comments
 (0)