API Reference

class ropwr.RobustPWRegression(objective='l2', degree=1, continuous=True, continuous_deriv=True, monotonic_trend=None, solver='auto', h_epsilon=1.35, quantile=0.5, regularization=None, reg_l1=1.0, reg_l2=1.0, max_iter=None, extrapolation='constant', extrapolation_bounds=None, space='linear', verbose=False)

Bases: sklearn.base.BaseEstimator

Robust piecewise regression.

Parameters
  • objective (str, optional (default="l2")) – The objective function. Supported objectives are “l2”, “l1”, “huber” and “quantile”. Note that “l1”, “huber” and “quantile” are robust objective functions.

  • regularization (str or None (default=None)) – Type of regularization. Supported regularization are “l1” (Lasso) and “l2” (Ridge). If None, no regularization is applied.

  • degree (int (default=1)) –

    The degree of the polynomials.

    • degree = 0: piecewise constant functions.

    • degree = 1: piecewise linear functions.

    • degree > 1: piecewise polynomial functions.

  • continuous (bool (default=True)) – Whether to fit a continuous or discontinuous piecewise regression.

  • continuous_deriv (bool (default=True)) – Whether to fit a polynomial with continuous derivatives. This option fits a smooth degree d-polynomial with d-1 continuity in derivatives (splines).

  • monotonic_trend (str or None, optional (default=None)) – The monotonic trend. Supported trends are “ascending”, “descending”, “convex” and “concave”. If None, then the monotonic constraint is disabled.

  • solver (str, optional (default="auto")) – The optimizer to solve the underlying mathematical optimization problem. Supported solvers are “ecos”, “osqp”, “direct”, to choose the direct solver, and “auto”, to choose the most appropriate solver for the problem. Version 0.3.0 added support to solvers “scs” and “highs”.

  • h_epsilon (float (default=1.35)) – The parameter h_epsilon used when objective="huber", controls the number of samples that should be classified as outliers.

  • quantile (float (default=0.5)) – The parameter quantile is the q-th quantile to be used when objective="quantile".

  • reg_l1 (float (default=1.0)) – L1 regularization term. Increasing this value will smooth the regression model. Only applicable if regularization="l1".

  • reg_l2 (float (default=1.0)) – L2 regularization term. Increasing this value will smooth the regression model. Only applicable if regularization="l2".

  • max_iter (int or None (default=None)) –

    Maximum number of iterations. If max_iter=None, the default maximum number of iterations for each solver is set. See solver options.

    New in version 1.0.0.

  • extrapolation (str or None (default="constant")) –

    If None, an exception is raised if values outside the range (min, max) of the fitting. If ‘constant’, the value of the regression at min, max value of the fitting is used for constant extrapolation. If ‘continue’, the regression is extrapolated as is. If ‘linear’, a linear extrapolation is used.

    New in version 1.0.0.

  • extrapolation_bounds (tuple or None (default=None)) –

    If tuple (lb, ub), the regression prediction is limited to values within bounds. If None, the regression prediction is not limited.

    New in version 1.0.0.

  • space (str, optional (default="linear")) –

    The y space. If space="log", a logarithm transformation is applied to the input data y.

    New in version 1.0.0.

  • verbose (bool (default=False)) – Enable verbose output.

coef_

Coefficients for each bin. Number of bins = n_splits + 1.

Type

numpy.ndarray of shape (n_splits + 1, degree + 1)

fit(x, y, splits, n_bins=None, lb=None, ub=None)

Fit the piecewise regression according to the given training data.

Parameters
  • x (array-like, shape = (n_samples,)) – Training vector, where n_samples is the number of samples.

  • y (array-like, shape = (n_samples,)) – Target vector relative to x.

  • splits (str or array-like.) – If split array is not provided, the available methods are ‘uniform’ and ‘quantile’.

  • n_bins (int or None (default=None)) –

    The number of bins to produce. Only applicable if splits is ‘uniform’ or ‘quantile’.

    New in version 0.4.0.

  • lb (float or None (default=None)) – Add constraints to avoid values below the lower bound lb.

  • ub (float or None (default=None)) – Add constraints to avoid values above the upper bound ub.

Returns

self – Fitted piecewise regression.

Return type

object

fit_predict(x, y, splits, n_bins=None, lb=None, ub=None)

Fit the piecewise regression according to the given training data, then predict.

Parameters
  • x (array-like, shape = (n_samples,)) – Training vector, where n_samples is the number of samples.

  • y (array-like, shape = (n_samples,)) – Target vector relative to x.

  • splits (str or array-like.) – If split array is not provided, the available methods are ‘uniform’ and ‘quantile’.

  • n_bins (int or None (default=None)) – The number of bins to produce. Only applicable if splits is ‘uniform’ or ‘quantile’.

  • lb (float or None (default=None)) – Fit impose constraints to satisfy that values are greater or equal than lb. In predict, values below the lower bound lb are clipped to lb.

  • ub (float or None (default=None)) – Fit impose constraints to satisfy that values are less or equal than ub. In predict, values above the upper bound ub are clipped to ub.

Returns

p – Predicted array.

Return type

numpy array, shape = (n_samples,)

get_params(deep=True)

Get parameters for this estimator.

Parameters

deep (bool, default=True) – If True, will return the parameters for this estimator and contained subobjects that are estimators.

Returns

params – Parameter names mapped to their values.

Return type

dict

predict(x)

Predict using the piecewise regression.

Parameters

x (array-like, shape = (n_samples,)) – Training vector, where n_samples is the number of samples.

Returns

p – Predicted array.

Return type

numpy array, shape = (n_samples,)

set_params(**params)

Set the parameters of this estimator.

The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.

Parameters

**params (dict) – Estimator parameters.

Returns

self – Estimator instance.

Return type

estimator instance

property stats

The number of variables and constraints of the underlying optimization problem.

Returns

stats

Return type

dict

property status

The status of the underlying optimization solver.

Returns

status

Return type

str