sample.hinge module

Hinge functions and regression models

class sample.hinge.HingeRegression(linear_regressor=None, linear_regressor_k: str = 'coef_', linear_regressor_q: str = 'intercept_', method: str = 'dogbox', coeffs_init: Optional[Callable[[ndarray, ndarray, float, float], Tuple[float, float, float]]] = None, bounds: Optional[Callable[[ndarray, ndarray, float, float], Tuple[Tuple[float, float, float], Tuple[float, float, float]]]] = None)

Bases: RegressorMixin, BaseEstimator

Regressor for fitting to a hinge function

\(h(x) = k \cdot min(x, a) + q\)

Parameters:
  • linear_regressor (sklearn.base.BaseEstimator) – Linear regression model instance. Must be sklearn-compatible

  • linear_regressor_k (str) – Attribute name for the estimated slope coefficient of the linear regression

  • linear_regressor_q (str) – Attribute name for the estimated intercept coefficient of the linear regression

  • method (str) – Nonlinear least squares method See scipy.optimize.least_squares() for options and detailed explanation. Defaults to “dogbox”

  • coeffs_init (callable) – Initializer for hinge function coefficients. Signature should be coeffs_init(x, y, k, q) -> a, k, q. It should return initial parameters for the nonlinear least squares using input data x and y, and linearly estimated coefficients k and q. If None, use default

  • bounds (callable) – Callable for computing hinge function coefficient boundaries. Signature should be bounds(x, y, k, q) -> ((a_min, k_min, q_min), (a_max, k_max, q_max)). It should return lower and upper boundaries for all three parameters using input data x and y, and linearly estimated coefficients k and q. If None, use default

coeffs_

Learned parameters (a, k, q). They are also accessible via their individual properties

Type:

array

result_

Optimization result of the nonlinear least squares procedure

Type:

OptimizeResult

property a_: float

Learned knee point

property bounds

Callable for computing hinge function coefficient boundaries

property coeffs_init

Initializer for hinge function coefficients

fit(x: ndarray, y: ndarray, **kwargs)

Fit hinge function

Parameters:
  • x (array) – Independent variable. Must be a column vector

  • like ((shape) –

  • y (array) – Dependent variable

  • **kwargs – Keyword arguments for scipy.optimize.least_squares()

Returns:

self

Return type:

HingeRegression

property k_: float

Learned slope

property linear_regressor

Linear regression model

predict(x: ndarray)

Evaluate learned hinge function

Parameters:

x (array) – Input independent variable array

Returns:

h(x)

Return type:

array

property q_: float

Learned intercept

sample.hinge.hinge_function(x: ndarray, a: float, k: float, q: float, out: Optional[ndarray] = None) ndarray

Hinge function

Parameters:
  • x (array) – Independent variable

  • a (float) – Knee point

  • k (float) – Slope

  • q (float) – Intercept

  • out (array) – Optional. Array to use for storing results

Returns:

\(h(x) = k \cdot min(x, a) + q\)

Return type:

array