.. 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 <sphx_glr_download_auto_examples_miscellaneous_plot_isotonic_regression.py>`
        to download the full example code or to run this example in your browser via JupyterLite or Binder

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_auto_examples_miscellaneous_plot_isotonic_regression.py:


===================
Isotonic Regression
===================

An illustration of the isotonic regression on generated data (non-linear
monotonic trend with homoscedastic uniform noise).

The isotonic regression algorithm finds a non-decreasing approximation of a
function while minimizing the mean squared error on the training data. The
benefit of such a non-parametric model is that it does not assume any shape for
the target function besides monotonicity. For comparison a linear regression is
also presented.

The plot on the right-hand side shows the model prediction function that
results from the linear interpolation of thresholds points. The thresholds
points are a subset of the training input observations and their matching
target values are computed by the isotonic non-parametric fit.

.. GENERATED FROM PYTHON SOURCE LINES 21-39

.. code-block:: default


    # Author: Nelle Varoquaux <nelle.varoquaux@gmail.com>
    #         Alexandre Gramfort <alexandre.gramfort@inria.fr>
    # License: BSD

    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 40-41

Fit IsotonicRegression and LinearRegression models:

.. GENERATED FROM PYTHON SOURCE LINES 41-48

.. code-block:: default


    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

    <div class="output_subarea output_html rendered_html output_result">
    <style>#sk-container-id-47 {color: black;}#sk-container-id-47 pre{padding: 0;}#sk-container-id-47 div.sk-toggleable {background-color: white;}#sk-container-id-47 label.sk-toggleable__label {cursor: pointer;display: block;width: 100%;margin-bottom: 0;padding: 0.3em;box-sizing: border-box;text-align: center;}#sk-container-id-47 label.sk-toggleable__label-arrow:before {content: "▸";float: left;margin-right: 0.25em;color: #696969;}#sk-container-id-47 label.sk-toggleable__label-arrow:hover:before {color: black;}#sk-container-id-47 div.sk-estimator:hover label.sk-toggleable__label-arrow:before {color: black;}#sk-container-id-47 div.sk-toggleable__content {max-height: 0;max-width: 0;overflow: hidden;text-align: left;background-color: #f0f8ff;}#sk-container-id-47 div.sk-toggleable__content pre {margin: 0.2em;color: black;border-radius: 0.25em;background-color: #f0f8ff;}#sk-container-id-47 input.sk-toggleable__control:checked~div.sk-toggleable__content {max-height: 200px;max-width: 100%;overflow: auto;}#sk-container-id-47 input.sk-toggleable__control:checked~label.sk-toggleable__label-arrow:before {content: "▾";}#sk-container-id-47 div.sk-estimator input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-47 div.sk-label input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-47 input.sk-hidden--visually {border: 0;clip: rect(1px 1px 1px 1px);clip: rect(1px, 1px, 1px, 1px);height: 1px;margin: -1px;overflow: hidden;padding: 0;position: absolute;width: 1px;}#sk-container-id-47 div.sk-estimator {font-family: monospace;background-color: #f0f8ff;border: 1px dotted black;border-radius: 0.25em;box-sizing: border-box;margin-bottom: 0.5em;}#sk-container-id-47 div.sk-estimator:hover {background-color: #d4ebff;}#sk-container-id-47 div.sk-parallel-item::after {content: "";width: 100%;border-bottom: 1px solid gray;flex-grow: 1;}#sk-container-id-47 div.sk-label:hover label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-47 div.sk-serial::before {content: "";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: 0;}#sk-container-id-47 div.sk-serial {display: flex;flex-direction: column;align-items: center;background-color: white;padding-right: 0.2em;padding-left: 0.2em;position: relative;}#sk-container-id-47 div.sk-item {position: relative;z-index: 1;}#sk-container-id-47 div.sk-parallel {display: flex;align-items: stretch;justify-content: center;background-color: white;position: relative;}#sk-container-id-47 div.sk-item::before, #sk-container-id-47 div.sk-parallel-item::before {content: "";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: -1;}#sk-container-id-47 div.sk-parallel-item {display: flex;flex-direction: column;z-index: 1;position: relative;background-color: white;}#sk-container-id-47 div.sk-parallel-item:first-child::after {align-self: flex-end;width: 50%;}#sk-container-id-47 div.sk-parallel-item:last-child::after {align-self: flex-start;width: 50%;}#sk-container-id-47 div.sk-parallel-item:only-child::after {width: 0;}#sk-container-id-47 div.sk-dashed-wrapped {border: 1px dashed gray;margin: 0 0.4em 0.5em 0.4em;box-sizing: border-box;padding-bottom: 0.4em;background-color: white;}#sk-container-id-47 div.sk-label label {font-family: monospace;font-weight: bold;display: inline-block;line-height: 1.2em;}#sk-container-id-47 div.sk-label-container {text-align: center;}#sk-container-id-47 div.sk-container {/* jupyter's `normalize.less` sets `[hidden] { display: none; }` but bootstrap.min.css set `[hidden] { display: none !important; }` so we also need the `!important` here to be able to override the default hidden behavior on the sphinx rendered scikit-learn.org. See: https://github.com/scikit-learn/scikit-learn/issues/21755 */display: inline-block !important;position: relative;}#sk-container-id-47 div.sk-text-repr-fallback {display: none;}</style><div id="sk-container-id-47" class="sk-top-container"><div class="sk-text-repr-fallback"><pre>LinearRegression()</pre><b>In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook. <br />On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.</b></div><div class="sk-container" hidden><div class="sk-item"><div class="sk-estimator sk-toggleable"><input class="sk-toggleable__control sk-hidden--visually" id="sk-estimator-id-215" type="checkbox" checked><label for="sk-estimator-id-215" class="sk-toggleable__label sk-toggleable__label-arrow">LinearRegression</label><div class="sk-toggleable__content"><pre>LinearRegression()</pre></div></div></div></div></div>
    </div>
    <br />
    <br />

.. GENERATED FROM PYTHON SOURCE LINES 49-50

Plot results:

.. GENERATED FROM PYTHON SOURCE LINES 50-72

.. code-block:: default


    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 73-77

Note that we explicitly passed `out_of_bounds="clip"` to the constructor of
`IsotonicRegression` to control the way the model extrapolates outside of the
range of data observed in the training set. This "clipping" extrapolation can
be seen on the plot of the decision function on the right-hand.


.. rst-class:: sphx-glr-timing

   **Total running time of the script:** (0 minutes 0.160 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/1.3.X?urlpath=lab/tree/notebooks/auto_examples/miscellaneous/plot_isotonic_regression.ipynb
        :alt: Launch binder
        :width: 150 px



    .. container:: lite-badge

      .. image:: images/jupyterlite_badge_logo.svg
        :target: ../../lite/lab/?path=auto_examples/miscellaneous/plot_isotonic_regression.ipynb
        :alt: Launch JupyterLite
        :width: 150 px

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: plot_isotonic_regression.py <plot_isotonic_regression.py>`

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: plot_isotonic_regression.ipynb <plot_isotonic_regression.ipynb>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_