API Reference¶
pwlreg ¶
PWLReg: A flexible implementation of piecewise least squares regression.
AutoPiecewiseRegression ¶
AutoPiecewiseRegression(
n_segments,
*,
degree=1,
continuity="c0",
solver="auto",
random_state=None
)
Bases: BaseEstimator
, _BasePiecewiseRegressor
Piecewise linear regression with unknown breakpoint locations.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
n_segments |
int
|
The number of line segments to fit. |
required |
degree |
int | list[int]
|
The polynomial degree(s) of the line segments. If it is a
single integer, all segments will have the same degree. If it is
specified as a list, its length must be equal to |
1
|
continuity |
str
|
The degree of continuity for the line segments at the
breakpoints. The default of |
'c0'
|
solver |
str
|
The optimization routine to use in finding the breakpoints. |
'auto'
|
random_state |
Any
|
Used in stochastic solvers for reproducibility. |
None
|
Attributes:
Name | Type | Description |
---|---|---|
breakpoints_ |
Optimal breakpoint locations. The first and last elements will always be the min and max of the input data. |
|
coef_ |
Vector coefficients that minimize the sum of squared errors. |
|
ssr_ |
Sum of squared errors resulting from the fit. |
|
n_params_ |
Number of estimated parameters. |
|
n_iter |
Number of iterations the solver needed to converge. |
Initialize the auto regression object.
Source code in src/pwlreg/pwlreg.py
fit ¶
Fit piecewise regression model, finding optimal breakpoints.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X |
ArrayLike
|
Input data |
required |
y |
ArrayLike
|
Target values |
required |
weights |
ArrayLike
|
Individual weights for each sample. If given a float, every sample will have the same weight. |
None
|
Returns:
Type | Description |
---|---|
AutoPiecewiseRegression
|
Fitted estimator. |
Source code in src/pwlreg/pwlreg.py
predict ¶
Predict using the fitted piecewise regression model with optimal breakpoints.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X |
ArrayLike
|
Input data |
required |
Returns:
Type | Description |
---|---|
ArrayLike
|
Predicted values |
Source code in src/pwlreg/pwlreg.py
PiecewiseLinearRegression ¶
Bases: BaseEstimator
, _BasePiecewiseRegressor
Piecewise linear regression with known breakpoint locations.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
breakpoints |
ArrayLike
|
Array of breakpoint locations. Must include the minimum
and maximum points of your input data. If |
None
|
degree |
int | list[int]
|
The polynomial degree(s) of the line segments. If it is a single integer, all segments will have the same degree. If it is specified as a list, its length must be one less than the number of breakpoints. A degree of 0 will fit a constant (flat) line, 1 will fit a straight line, and 2 will fit a quadratic curve. |
1
|
continuity |
str
|
The degree of continuity for the line segments at the
breakpoints. The default of |
'c0'
|
Attributes:
Name | Type | Description |
---|---|---|
coef_ |
Vector coefficients that minimize the sum of squared errors. |
|
ssr_ |
Sum of squared errors resulting from the fit. |
|
n_params_ |
Number of estimated parameters. |
Initialize the regression object.
Source code in src/pwlreg/pwlreg.py
fit ¶
Fit piecewise regression model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X |
ArrayLike
|
Input data |
required |
y |
ArrayLike
|
Target values |
required |
weights |
ArrayLike
|
Individual weights for each sample. If given a float, every sample will have the same weight. |
None
|
Returns:
Type | Description |
---|---|
PiecewiseLinearRegression
|
Fitted estimator. |
Source code in src/pwlreg/pwlreg.py
predict ¶
Predict using the fitted piecewise regression model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X |
ArrayLike
|
Input data |
required |
Returns:
Type | Description |
---|---|
ArrayLike
|
Predicted values |