from
sklearn.preprocessing
import
PolynomialFeatures
from
sklearn.linear_model
import
LinearRegression
from
sklearn.isotonic
import
IsotonicRegression
import
numpy as np
import
matplotlib.pyplot as plt
n
=
20
x
=
np.arange(n)
print
(
'Input:\n'
, x)
y
=
np.random.randint(
0
,
20
, size
=
n)
+
10
*
np.log1p(np.arange(n))
print
(
"Target :\n"
, y)
ir
=
IsotonicRegression()
y_ir
=
ir.fit_transform(x, y)
lr
=
LinearRegression()
lr.fit(x.reshape(
-
1
,
1
), y)
y_lr
=
lr.predict(x.reshape(
-
1
,
1
))
poly
=
PolynomialFeatures(degree
=
2
)
x_poly
=
poly.fit_transform(x.reshape(
-
1
,
1
))
lr_poly
=
LinearRegression()
lr_poly.fit(x_poly, y)
y_poly
=
lr_poly.predict(x_poly)
plt.plot(x, y,
'o'
, label
=
'data'
)
plt.plot(x, y_ir, label
=
'isotonic regression'
)
plt.plot(x, y_lr, label
=
'linear regression'
)
plt.plot(x, y_poly, label
=
'polynomial regression'
)
plt.legend()
plt.xlabel(
'X'
)
plt.ylabel(
'Y'
)
plt.title(
'Comparison of Regression Techniques'
)
plt.show()