lpspline.spline.piecewise_linear.PiecewiseLinear#
- class lpspline.spline.piecewise_linear.PiecewiseLinear(term: str, knots: int | ndarray, tag: str | None = 'pwl', by: str | None = None)[source]#
Bases:
SplinePiecewise Linear Spine framework built primarily around discrete ReLU knot bases.
- 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:
- 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.
- property by: str | None#
Returns the grouping variable column name.
- Returns:
The mapping group attribute column name.
- 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]
- 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 parameter bounds dynamically assuming knots were expressed as integer lengths.
- Parameters:
x (np.ndarray) – The 1D input target observation set.
by (np.ndarray, default=None) – Assigned category classes if grouping interactively.
- property knots: int | ndarray#
Returns the knot breaking parameters natively tracked within.
- Returns:
The 1D locations sequentially mapping piecewise shift points.
- 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]