.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/Calibration/IsotonicRegression.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. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_Calibration_IsotonicRegression.py: Isotonic regression ======================= This script showcases the use of IsotonicRegression for predicting monotonic relationships. .. GENERATED FROM PYTHON SOURCE LINES 7-39 .. image-sg:: /auto_examples/Calibration/images/sphx_glr_IsotonicRegression_001.png :alt: IsotonicRegression :srcset: /auto_examples/Calibration/images/sphx_glr_IsotonicRegression_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none tensor([1.3007, 1.1584, 1.1584, 1.0047, 1.3007, 1.2268, 1.6313, 1.1178, 1.1126, 1.3007, 1.5274, 1.0047, 1.1584, 0.9212, 1.9415, 1.1178, 1.5274, 0.8849, 1.0805, 1.1584]) [1.30070541 1.1584315 1.1584315 1.00466986 1.30070541 1.22684785 1.63132361 1.11777065 1.11262473 1.30070541 1.52742959 1.00466986 1.1584315 0.92119819 nan 1.11777065 1.52742959 nan 1.08046234 1.1584315 ] | .. code-block:: Python import torch import matplotlib.pyplot as plt from sklearn.isotonic import IsotonicRegression as sk_isotonic from DLL.Data.Preprocessing import data_split from DLL.MachineLearning.SupervisedLearning.Calibration import IsotonicRegression torch.manual_seed(0) X = torch.linspace(0, 1, 100) y = X ** 2 + 1 + 0.1 * torch.randn_like(X) Xtrain, ytrain, _, _, Xtest, ytest = data_split(X, y, train_split=0.8, validation_split=0) increasing = True if not increasing: ytrain, ytest = -ytrain, -ytest model = IsotonicRegression() model.fit(Xtrain, ytrain, increasing=increasing) ypred = model.predict(Xtest) print(ypred) model = sk_isotonic(increasing=increasing) model.fit(Xtrain, ytrain) sk_ypred = model.predict(Xtest) print(sk_ypred) plt.scatter(Xtrain, ytrain, label="Train", alpha=0.5) plt.scatter(Xtest, ytest, label="Test", alpha=0.5) plt.scatter(Xtest, ypred, label="Pred", alpha=0.5) plt.scatter(Xtest, sk_ypred, label="SKlearn pred", alpha=0.5) plt.legend() plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 2.088 seconds) .. _sphx_glr_download_auto_examples_Calibration_IsotonicRegression.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: IsotonicRegression.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: IsotonicRegression.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: IsotonicRegression.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_