lpspline.spline.bspline.BSpline#

class lpspline.spline.bspline.BSpline(term: str, knots: int | ndarray, degree: int = 3, by: str | None = None, tag: str | None = 'bspline')[source]#

Bases: Spline

B-Spline implementation using the Cox-de Boor recursion algorithm.

add_constraint(*constraints)#

Add one or more shape constraints to the spline.

Not all splines can accept all constraints. This function validates the compatibility of the constraints with the current spline type.

Parameters:

*constraints (Constraint) – One or more Constraint objects to apply (e.g., Monotonic, Convex).

Returns:

Returns the spline instance to allow methodical chaining.

Return type:

Spline

Raises:

ValueError – If a given constraint is incompatible with this spline type.

add_penalty(*penalties)#

Add one or more regularization penalties to the spline coefficients.

Parameters:

*penalties (Penalty) – One or more Penalty objects to apply (e.g., Ridge, Lasso).

Returns:

Returns the spline instance to allow methodical chaining.

Return type:

Spline

Raises:

TypeError – If the supplied argument is not a Penalty instance.

property by: str | None#

Returns the grouping variable column name.

Returns:

The name of the grouping column, or None.

Return type:

Optional[str]

property coefficients: ndarray#

Returns the fitted coefficients of the spline.

Returns:

The computed coefficient values from the CVXPY variables.

Return type:

np.ndarray

property constraints: List[Constraint]#

Returns the CVXPY constraints associated with the spline.

Returns:

A list of all constraints that apply to this spline.

Return type:

List[cp.Constraint]

property degree: int#

Returns the degree of the spline.

Returns:

The polynomial degree.

Return type:

int

eval(x: ndarray, return_basis: bool = False, by: ndarray | None = None) ndarray#

Evaluates the fitted numeric spline values for the given input x.

Parameters:
  • x (np.ndarray) – The 1D input feature array to evaluate the spline on.

  • return_basis (bool, default=False) – Whether to return the raw basis matrix instead of the evaluated spline.

  • by (np.ndarray, default=None) – The 1D integer-encoded grouping array, if the by argument is specified.

Returns:

A 1D numpy array of shape (n_samples,) representing the predicted values.

Return type:

np.ndarray

Raises:

AssertionError – If the spline has not been fitted and coefficients are not available.

init_by(by: ndarray)#

Initialize internal tracking for the unique classes in the by variable.

Parameters:

by (np.ndarray) – A 1D array of the categorical/grouping values found in the data.

init_spline(x: ndarray, by: ndarray | None = None)[source]#

Initializes the spline parameters based on input data.

If knots was initially provided as an integer, it is correctly mapped to linearly spaced values spanning the minimum and maximum of the input array x.

Parameters:
  • x (np.ndarray) – The 1D input array of training features.

  • by (np.ndarray, default=None) – The array of grouping values.

property knots: int | ndarray#

Returns the knot definitions for this spline.

Returns:

The array of knot locations or integer count.

Return type:

Union[int, np.ndarray]

property penalties: List[Expression]#

Returns the penalty expressions associated with the spline.

Returns:

A list of CVXPY expressions representing the penalties.

Return type:

List[cp.Expression]

property tag: str | None#

Returns the custom tag for this spline component.

Returns:

The tag assigned to this spline component, or None.

Return type:

Optional[str]

property term: str#

Returns the column name or term this spline models.

Returns:

The column name or term string.

Return type:

str

property variables: List[Variable]#

Returns the CVXPY variables representing the spline coefficients.

Returns:

The list of CVXPY variables constructed during fitting.

Return type:

List[cp.Variable]