lpspline.spline.factor.Factor#

class lpspline.spline.factor.Factor(term: str, tag: str | None = 'factor', n_classes: int | None = None)[source]#

Bases: Spline

Categorical Factor mapping utilizing a one-hot-encoded basis.

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 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]#

Introspectively determines the number of categorical instances locally contained in x assuming none were globally established at initialization.

Parameters:
  • x (np.ndarray) – The evaluation group.

  • by (np.ndarray, default=None) – The grouped indexing column if modeling interactions.

property n_classes: int | None#

Returns the number of unique identified categorical classes.

Returns:

The number of classes evaluated in the factor basis system.

Return type:

Optional[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 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]