Note
Go to the end to download the full example code.
Isotonic regression
This script showcases the use of IsotonicRegression for predicting monotonic relationships.

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 ]
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()
Total running time of the script: (0 minutes 2.088 seconds)