lpspline.spline.cyclic_spline.CyclicSpline#
- class lpspline.spline.cyclic_spline.CyclicSpline(term: str, order: int, period: float | None = None, tag: str | None = 'cyclicspline', by: str | None = None)[source]#
Bases:
SplinePeriodic Cyclic Spline defined by Fourier series expansions.
- 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 column name used to group spline coefficients.
- Returns:
The name of the column used for the by argument, 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]
- 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 periodic boundary conditions based on data.
If period is not specified directly during Initialization, it calculates the period automatically across the maximum observed range in x.
- Parameters:
x (np.ndarray) – The 1D input array of training periodic measurements.
by (np.ndarray, default=None) – The array of grouping values.
- property order: int#
Returns the number of generated Fourier term pairs.
- Returns:
The cyclic order describing polynomial flexibility.
- Return type:
int
- 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 period: float#
Returns the determined period of the cyclic spline.
- Returns:
The numeric period length.
- Return type:
float
- 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]