Isotonic Regression
Published Jan 25, 2025
Contribute to Docs
Isotonic Regression is a technique of using a free-form line to establish a non-linear path. The path is created by a set of data points. In this technique, predictor variables and target vairables increase or decrease, monotonically, in a non-oscilatory manner. The word “isotonic” (iso and tonos) comes from the Greek, meaning equal and to stretch, respectively.
Syntax
This is the basic syntax for implementing Isotonic Regression in Python:
import numpy as np
from sklearn.isotonic import IsotonicRegression
import matplotlib.pyplot as plt
# Define predictor and target data
X = [list of predictor values]
Y = [list of target values]
# Initialize Isotonic Regression model
model = IsotonicRegression(increasing=True or False)
# Fit the model and transform the target data
Y_transformed = model.fit_transform(X, Y)
# Visualize the original data and the regression fit
plot.scatter(X, Y, label="Original Data")
plot.line(X, Y_transformed, label="Isotonic Fit", color="red")
plot.title("Isotonic Regression Example")
plot.xlabel("Predictor Variable")
plot.ylabel("Target Variable")
plot.legend()
plot.show()
increasing
: This is a boolean parameter that specifies whether the fitted values should increase monotonically (True
) or decrease monotonically (False
).increasing=True
enforces a non-decreasing relationship between the predictor (x
) and the target (y
) variables.
Example
The following is an example of Isotonic Regression in action:
import numpy as npimport matplotlib.pyplot as pltfrom sklearn.isotonic import IsotonicRegression# Generate synthetic data with a non-monotonic relationshipnp.random.seed(123)x = np.linspace(0, 10, 15) # Predictor variablesy = np.sin(x) + np.random.normal(0, 0.2, len(x)) # Non-monotonic target variable with noise# Initialize and fit Isotonic Regression modeliso_reg = IsotonicRegression(increasing=True)y_isotonic = iso_reg.fit_transform(x, y)# Plot the original data and Isotonic Regression fitplt.figure(figsize=(8, 6))plt.scatter(x, y, color='green', label='Original Data', marker='o')plt.step(x, y_isotonic, color-'orange', label='Isotonic Fit', where='post', linewidth=2)plt.title('Isotonic Regression on Non-Monotonic Data')plt.xlabel('Predictor Variable (X)')plt.ylabel('Target Variable (Y)')plt.legend()plt.grid(True)plt.show()
The example results in the following output:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python:Sklearn on Codecademy
- Skill path
Intermediate Machine Learning
Level up your machine learning skills with tuning methods, advanced models, and dimensionality reduction.Includes 5 CoursesWith CertificateIntermediate8 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours