Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding IsoReg folder and files #5983

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
112 changes: 112 additions & 0 deletions content/sklearn/concepts/isotonic-regression/isotonic-regression.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
---
Title: 'Isotonic Regression'
Description: 'Isotonic Regression is a technique of using a fre-form line to establish a non-linear path, created by a set of data points.'
Subjects:
- 'Data Science'
- 'Data Visualization'
Tags:
- 'Booleans'
- 'Datasets'
- 'Matplotlib'
- 'Numpy'
- 'Scikit-learn'
CatalogContent:
- 'learn-python-3'
- 'paths/intermediate-machine-learning-skill-path'
---

***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:

```pseudo
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:

```py
import numpy as np
import matplotlib.pyplot as plt
from sklearn.isotonic import IsotonicRegression

# Generate synthetic data with a non-monotonic relationship
np.random.seed(123)
x = np.linspace(0, 10, 15) # Predictor variables
y = np.sin(x) + np.random.normal(0, 0.2, len(x)) # Non-monotonic target variable with noise

# Initialize and fit isotonic regression model
iso_reg = IsotonicRegression(increasing=True)
y_isotonic = iso_reg.fit_transform(x, y)

# Plot the original data and isotonic regression fit
plt.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:

![Isotonic Regression Example] (https://github.com/S3an28/docs/blob/SK_5305/content/sklearn/concepts/isotonic-regression/isotonic-regression-example.jpg)

## Codebyte

```codebyte/python
import numpy as np
import matplotlib.pyplot as plt
from sklearn.isotonic import IsotonicRegression

# Generate some synthetic data
np.random.seed(0)
x = np.arange(1, 11)
y = np.array([1, 2, 1.5, 3, 2.5, 3.5, 4, 4.5, 5, 6]) + np.random.normal(0, 0.3, 10)

# Fit isotonic regression
iso_reg = IsotonicRegression(increasing=True)
y_pred = iso_reg.fit_transform(x, y)

# Plot
plt.figure(figsize=(8, 6))
plt.scatter(x, y, color='blue', label='Original Data')
plt.plot(x, y_pred, color='red', label='Isotonic Regression Fit', linewidth=2)
plt.title("Isotonic Regression")
plt.xlabel("X")
plt.ylabel("Y")
plt.legend()
plt.grid(True)
plt.show()
```