RoPWR: Robust Piecewise Regression
RoPWR (Robust Piecewise Regression) is a Python library that implements robust piecewise regression using convex optimization techniques. It provides a flexible framework to model non-linear relationships with segments, supporting various solvers via CVXPY. The current version is 1.2.0, with releases occurring every few months, primarily driven by feature additions, improvements, and dependency updates.
Common errors
-
TypeError: force_all_finite received a sparse matrix... Expected an array, got a sparse matrix.
cause This error occurs when an older version of `ropwr` (prior to 1.2.0) is used with `scikit-learn` version 1.6 or newer. `scikit-learn` deprecated `force_all_finite` in favor of `ensure_all_finite`.fixUpgrade `ropwr` to `1.2.0` or higher: `pip install --upgrade ropwr`. Alternatively, if you cannot upgrade `ropwr`, downgrade `scikit-learn` to a version prior to 1.6: `pip install 'scikit-learn<1.6'`. -
AttributeError: 'RoPWR' object has no attribute 'max_iter'
cause You are attempting to set the `max_iter` parameter when initializing or fitting a `RoPWR` model, but your `ropwr` library version is older than 1.0.0.fixUpgrade `ropwr` to version 1.0.0 or higher: `pip install --upgrade ropwr`. If you cannot upgrade, remove the `max_iter` parameter from your `RoPWR` instantiation. -
cvxpy.error.SolverError: Solver 'OSQP' failed. Try another solver or set verbose=True for more details.
cause The default or specified CVXPY solver (OSQP in this case) failed to converge or solve the optimization problem. This can be due to problem conditioning, solver limitations, or specific data characteristics.fixTry explicitly specifying a different solver in the `RoPWR` constructor, for example, `RoPWR(..., solver='Clarabel')` or `RoPWR(..., solver='SCS')`. Ensure you have the necessary solvers installed (e.g., `pip install 'ropwr[all]'`). For detailed debugging, set `verbose=True` in the `RoPWR` constructor. -
ImportError: cannot import name 'RoPWR' from 'ropwr'
cause This usually means the `RoPWR` class is not found in the `ropwr` package. This could be a typo in the import statement or an issue with the installation.fixVerify that `ropwr` is correctly installed (`pip show ropwr`). Ensure the import statement is exactly `from ropwr import RoPWR`. If you've just installed it, restarting your interpreter or IDE might resolve module caching issues.
Warnings
- gotcha Incompatibility with scikit-learn 1.6+ due to `force_all_finite` deprecation.
- gotcha Older versions of RoPWR might experience OSQP solver convergence issues, especially with complex problems.
- gotcha The `max_iter` parameter for controlling solver iterations was introduced in v1.0.0. Attempting to use it with older versions will raise an AttributeError.
- gotcha Key features like enforcing continuous derivatives at split points and extrapolation methods were added in v1.0.0.
Install
-
pip install ropwr -
pip install 'ropwr[all]' # Installs common CVXPY solvers like Clarabel, SCS, OSQP, ECOS
Imports
- RoPWR
from ropwr import RoPWR
Quickstart
import numpy as np
from ropwr import RoPWR
# Generate some synthetic data
np.random.seed(0)
X = np.linspace(0, 10, 100).reshape(-1, 1)
y = np.where(X < 5, 2 * X, -X + 15).flatten() + np.random.normal(0, 1, 100)
# Initialize and fit the RoPWR model with 2 segments
model = RoPWR(n_segments=2, solver='Clarabel', random_state=42)
model.fit(X, y)
# Make predictions
y_pred = model.predict(X)
print(f"Predicted y for X[0]: {y_pred[0]:.2f}")
print(f"R^2 score: {model.score(X, y):.2f}")