.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/miscellaneous/plot_isotonic_regression.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. or to run this example in your browser via Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_miscellaneous_plot_isotonic_regression.py: =================== 保序回归 =================== 在生成的数据上展示保序回归(具有同方差均匀噪声的非线性单调趋势)。 保序回归算法在训练数据上找到函数的非递减近似,同时最小化均方误差。这种非参数模型的优点在于,它除了单调性之外,不对目标函数的形状做任何假设。为了比较,还展示了线性回归。 右侧的图显示了通过阈值点的线性插值得到的模型预测函数。阈值点是训练输入观测值的一个子集,其匹配的目标值由保序非参数拟合计算得出。 .. GENERATED FROM PYTHON SOURCE LINES 13-30 .. code-block:: Python # 作者:scikit-learn 开发者 # SPDX-License-Identifier: BSD-3-Clause import matplotlib.pyplot as plt import numpy as np from matplotlib.collections import LineCollection from sklearn.isotonic import IsotonicRegression from sklearn.linear_model import LinearRegression from sklearn.utils import check_random_state n = 100 x = np.arange(n) rs = check_random_state(0) y = rs.randint(-50, 50, size=(n,)) + 50.0 * np.log1p(np.arange(n)) .. GENERATED FROM PYTHON SOURCE LINES 31-32 拟合等渗回归和线性回归模型: .. GENERATED FROM PYTHON SOURCE LINES 32-40 .. code-block:: Python ir = IsotonicRegression(out_of_bounds="clip") y_ = ir.fit_transform(x, y) lr = LinearRegression() lr.fit(x[:, np.newaxis], y) # x needs to be 2d for LinearRegression .. raw:: html
LinearRegression()
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.


.. GENERATED FROM PYTHON SOURCE LINES 41-42 绘制结果: .. GENERATED FROM PYTHON SOURCE LINES 42-64 .. code-block:: Python segments = [[[i, y[i]], [i, y_[i]]] for i in range(n)] lc = LineCollection(segments, zorder=0) lc.set_array(np.ones(len(y))) lc.set_linewidths(np.full(n, 0.5)) fig, (ax0, ax1) = plt.subplots(ncols=2, figsize=(12, 6)) ax0.plot(x, y, "C0.", markersize=12) ax0.plot(x, y_, "C1.-", markersize=12) ax0.plot(x, lr.predict(x[:, np.newaxis]), "C2-") ax0.add_collection(lc) ax0.legend(("Training data", "Isotonic fit", "Linear fit"), loc="lower right") ax0.set_title("Isotonic regression fit on noisy data (n=%d)" % n) x_test = np.linspace(-10, 110, 1000) ax1.plot(x_test, ir.predict(x_test), "C1-") ax1.plot(ir.X_thresholds_, ir.y_thresholds_, "C1.", markersize=12) ax1.set_title("Prediction function (%d thresholds)" % len(ir.X_thresholds_)) plt.show() .. image-sg:: /auto_examples/miscellaneous/images/sphx_glr_plot_isotonic_regression_001.png :alt: Isotonic regression fit on noisy data (n=100), Prediction function (36 thresholds) :srcset: /auto_examples/miscellaneous/images/sphx_glr_plot_isotonic_regression_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 65-66 请注意,我们显式地将 `out_of_bounds="clip"` 传递给 `IsotonicRegression` 的构造函数,以控制模型在训练集中观察到的数据范围之外进行外推的方法。这种“剪辑”外推可以在右侧的决策函数图中看到。 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.066 seconds) .. _sphx_glr_download_auto_examples_miscellaneous_plot_isotonic_regression.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/scikit-learn/scikit-learn/main?urlpath=lab/tree/notebooks/auto_examples/miscellaneous/plot_isotonic_regression.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_isotonic_regression.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_isotonic_regression.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_isotonic_regression.zip ` .. include:: plot_isotonic_regression.recommendations .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_