2012-08-25 21:20:58 +08:00
|
|
|
"""
|
|
|
|
|
===================
|
|
|
|
|
Isotonic Regression
|
|
|
|
|
===================
|
|
|
|
|
|
2012-09-15 23:16:40 +08:00
|
|
|
An illustration of the isotonic regression on generated data. The
|
|
|
|
|
isotonic regression finds a non-decreasing approximation of a function
|
|
|
|
|
while minimizing the mean squared error on the training data. The benefit
|
|
|
|
|
of such a model is that it does not assume any form for the target
|
|
|
|
|
function such as linearity. For comparison a linear regression is also
|
|
|
|
|
presented.
|
|
|
|
|
|
2012-08-25 21:20:58 +08:00
|
|
|
"""
|
2013-06-24 00:18:03 +08:00
|
|
|
print(__doc__)
|
2012-08-25 21:20:58 +08:00
|
|
|
|
|
|
|
|
# Author: Nelle Varoquaux <nelle.varoquaux@gmail.com>
|
2012-09-13 21:31:27 +08:00
|
|
|
# Alexandre Gramfort <alexandre.gramfort@inria.fr>
|
2016-04-01 08:25:31 +08:00
|
|
|
# License: BSD
|
2012-08-25 21:20:58 +08:00
|
|
|
|
|
|
|
|
import numpy as np
|
2013-07-24 03:50:29 +08:00
|
|
|
import matplotlib.pyplot as plt
|
2012-08-25 21:20:58 +08:00
|
|
|
from matplotlib.collections import LineCollection
|
|
|
|
|
|
2012-11-15 05:42:03 +08:00
|
|
|
from sklearn.linear_model import LinearRegression
|
|
|
|
|
from sklearn.isotonic import IsotonicRegression
|
2012-09-15 23:55:02 +08:00
|
|
|
from sklearn.utils import check_random_state
|
2012-08-25 21:20:58 +08:00
|
|
|
|
|
|
|
|
n = 100
|
2012-09-13 19:11:57 +08:00
|
|
|
x = np.arange(n)
|
2012-09-15 23:55:02 +08:00
|
|
|
rs = check_random_state(0)
|
2018-07-14 21:05:03 +08:00
|
|
|
y = rs.randint(-50, 50, size=(n,)) + 50. * np.log1p(np.arange(n))
|
2012-09-13 19:11:57 +08:00
|
|
|
|
2017-06-20 20:48:57 +08:00
|
|
|
# #############################################################################
|
2012-09-15 22:22:14 +08:00
|
|
|
# Fit IsotonicRegression and LinearRegression models
|
2012-09-13 19:11:57 +08:00
|
|
|
|
|
|
|
|
ir = IsotonicRegression()
|
2013-07-24 03:50:29 +08:00
|
|
|
|
2012-09-13 19:11:57 +08:00
|
|
|
y_ = ir.fit_transform(x, y)
|
2012-08-25 21:20:58 +08:00
|
|
|
|
2012-09-15 22:22:14 +08:00
|
|
|
lr = LinearRegression()
|
|
|
|
|
lr.fit(x[:, np.newaxis], y) # x needs to be 2d for LinearRegression
|
|
|
|
|
|
2017-06-20 20:48:57 +08:00
|
|
|
# #############################################################################
|
|
|
|
|
# Plot result
|
2012-09-13 19:11:57 +08:00
|
|
|
|
|
|
|
|
segments = [[[i, y[i]], [i, y_[i]]] for i in range(n)]
|
|
|
|
|
lc = LineCollection(segments, zorder=0)
|
|
|
|
|
lc.set_array(np.ones(len(y)))
|
2018-07-23 15:49:01 +08:00
|
|
|
lc.set_linewidths(np.full(n, 0.5))
|
2012-08-25 21:20:58 +08:00
|
|
|
|
2013-07-24 03:50:29 +08:00
|
|
|
fig = plt.figure()
|
|
|
|
|
plt.plot(x, y, 'r.', markersize=12)
|
|
|
|
|
plt.plot(x, y_, 'g.-', markersize=12)
|
|
|
|
|
plt.plot(x, lr.predict(x[:, np.newaxis]), 'b-')
|
|
|
|
|
plt.gca().add_collection(lc)
|
|
|
|
|
plt.legend(('Data', 'Isotonic Fit', 'Linear Fit'), loc='lower right')
|
|
|
|
|
plt.title('Isotonic regression')
|
|
|
|
|
plt.show()
|