Skip to content

Spline Smoothing Expr

Spline Smoothing

Iteration related helper expressions

Functions:

Name Description
smooth_spline

Fits a smoothing cubic spline f and returns f(x). The user must make sure

smooth_spline(x, y, lambda_)

Fits a smoothing cubic spline f and returns f(x). The user must make sure that x is sorted and strictly increasing.

For more details, see the maths/ folder in the repo.

Parameters:

Name Type Description Default
x str | Expr

The x values of the points

required
y str | Expr

The y values of the points

required
lambda_ float

The regularization factor. The larger, the smoother and less kinks the curve will have.

required
Source code in python/polars_ds/exprs/expr_spline.py
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
def smooth_spline(x: str | pl.Expr, y: str | pl.Expr, lambda_: float) -> pl.Expr:
    """
    Fits a smoothing cubic spline f and returns f(x). The user must make sure
    that x is sorted and strictly increasing.

    For more details, see the maths/ folder in the repo.

    Parameters
    ----------
    x
        The x values of the points
    y
        The y values of the points
    lambda_
        The regularization factor. The larger, the smoother and less kinks
        the curve will have.
    """
    if lambda_ < 0.0:
        raise ValueError("Input `lambda_` must be nonnegative.")

    xx, yy = to_expr(x), to_expr(y)
    return pl_plugin(
        symbol="pl_smooth_spline",
        args=[xx.cast(pl.Float64).rechunk(), yy.cast(pl.Float64).rechunk()],
        kwargs={"lambda": lambda_},
    )