Skip to content

Commit 1c66894

Browse files
koaningmatekadlicskomatekadlicsko-efFBruzzesi
authored
MonotonicSplineTransformer (#709)
* first version * add-tests * docs * iterp for the win * docs * reran-plot-script * gogo * fixed-tests * gogo * Monotonicspline (#710) * fix: fixed an error in the implementation * feat: tests now directly test monotonicity and boundedness --------- Co-authored-by: Mate Kadlicsko <mate.kadlicsko@ef.com> * update-docs * base-tests * remove 3.8 * Update sklego/preprocessing/monotonicspline.py Co-authored-by: Francesco Bruzzesi <42817048+FBruzzesi@users.noreply.github.com> * Apply suggestions from code review Co-authored-by: Francesco Bruzzesi <42817048+FBruzzesi@users.noreply.github.com> * Update docs/_scripts/preprocessing.py * review * print check * prints-fixed * Update sklego/preprocessing/monotonicspline.py Co-authored-by: Francesco Bruzzesi <42817048+FBruzzesi@users.noreply.github.com> --------- Co-authored-by: mate.kadlicsko <kadlicsko.mate@gmail.com> Co-authored-by: Mate Kadlicsko <mate.kadlicsko@ef.com> Co-authored-by: Francesco Bruzzesi <42817048+FBruzzesi@users.noreply.github.com>
1 parent 0d6ef06 commit 1c66894

23 files changed

+253
-5
lines changed

.github/workflows/test.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ jobs:
1313
matrix:
1414
os: [ubuntu-latest, macos-latest, windows-latest]
1515
python-version: [
16-
"3.8",
1716
"3.9",
1817
"3.10",
1918
"3.11",

docs/_scripts/preprocessing.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,3 +325,66 @@ def generate_dataset(start, n=600):
325325

326326
plt.savefig(_static_path / "monotonic-3.png")
327327
plt.clf()
328+
329+
330+
######################################## Monotonic Spline #######################################
331+
332+
# --8<-- [start:monotonic-spline]
333+
import matplotlib.pylab as plt
334+
import numpy as np
335+
336+
N = 1000
337+
338+
np.random.seed(42)
339+
X = np.random.uniform(0, 1, size=N)
340+
y = X ** 2 + .1 * np.sin(20 * X) + 1 + .1 * np.random.randn(N)
341+
342+
plt.figure(figsize=(10, 4))
343+
plt.scatter(X, y)
344+
# --8<-- [end:monotonic-spline]
345+
346+
plt.savefig(_static_path / "monotonic-spline.png")
347+
plt.clf()
348+
349+
# --8<-- [start:monotonic-spline-transform]
350+
from sklego.preprocessing import MonotonicSplineTransformer
351+
352+
X_plt = np.sort(X)
353+
354+
plt.figure(figsize=(10, 4))
355+
tfm = MonotonicSplineTransformer(n_knots=10)
356+
X_out = tfm.fit_transform(X_plt.reshape(-1, 1))
357+
plt.plot(X_out);
358+
# --8<-- [end:monotonic-spline-transform]
359+
360+
plt.savefig(_static_path / "monotonic-spline-transform.png")
361+
plt.clf()
362+
363+
# --8<-- [start:monotonic-spline-regr]
364+
from sklearn.pipeline import make_pipeline
365+
from sklearn.linear_model import Ridge
366+
from sklearn.isotonic import IsotonicRegression
367+
from sklego.preprocessing import MonotonicSplineTransformer
368+
369+
pipe = make_pipeline(
370+
MonotonicSplineTransformer(n_knots=10),
371+
Ridge(positive=True),
372+
)
373+
pipe.fit(X.reshape(-1, 1), y)
374+
375+
iso = IsotonicRegression(out_of_bounds="clip")
376+
iso.fit(X, y)
377+
378+
X_test = np.linspace(-0.2, 1.2, 100)[:, None]
379+
380+
plt.figure(figsize=(10, 4))
381+
plt.plot(X_test[:, 0], pipe.predict(X_test), color="orange", linewidth=2, label="MonotonicSpline")
382+
plt.plot(
383+
X_test[:, 0], iso.predict(X_test), color="green", linewidth=2, label="Isotonic"
384+
)
385+
plt.scatter(X, y, alpha=0.3, label="Data")
386+
plt.legend()
387+
# --8<-- [end:monotonic-spline-regr]
388+
389+
plt.savefig(_static_path / "monotonic-spline-regr.png")
390+
plt.clf()
0 Bytes
Loading
0 Bytes
Loading
0 Bytes
Loading
0 Bytes
Loading
0 Bytes
Loading
95.1 KB
Loading
52.4 KB
Loading
41.2 KB
Loading

0 commit comments

Comments
 (0)