API reference

class polyfit.Constraints(monotonicity=None, curvature=None, sign=None, constraint_range=None, gridpoints=20)

Bases: object

Constraints class

Note

Shape constraints potentially make the model fit numerically unstable. Use at your own risk!

Parameters
  • monotonicity (string, optional) – Monotonicty of the model. Should be ‘inc’ or ‘dec’

  • curvature (string, optional) – Curvature of the model . Should be ‘convex’ or ‘concave’.

  • sign (string, optional) – Sign of the polynomial coefficients . Should be ‘positive’ or ‘negative’.

  • constraint_range (list, optional) – Range over which the constraints should be enforced. Must be of the form [lb, ub] with lower bounds lb and upper bounds ub

  • gridpoints (int, optional, default 20) – Number of grid points on which the constraints are imposed for the optimization problem

class polyfit.PolynomRegressor(deg=None, regularization=None, lam=0, interactions=False)

Bases: sklearn.base.BaseEstimator, sklearn.base.RegressorMixin

Polynomregressor class

Fits a multivariate polynomial model to arbitrary numerical data.

Parameters
  • deg (int) – Degree of the polynomial

  • regularization (string, optional) – Regularization to be used. Should be ‘l1’ or ‘l2’.

  • lam (float, optional, default 0) – Regularization coefficient

  • interactions (bool, optional, default False) – If True, also uses interaction terms of all predictor variables

coeffs_

Estimated polynomial coefficients

Type

ndarray (n, )

fit(x, y, loss='l2', m=1, constraints=None, verbose=False)

Fits the polynomial model to data x and y via cvxpy

Parameters
  • x (ndarray (m, n)) – Predictor variables of m samples and n features

  • y (ndarray (n, )) – Target variable

  • loss (string, optional, default 'l2') –

    Loss function to use. Can be one of

    • ’l2’

    • ’l1’

    • ’huber’

  • m (float, optional, default 1) – Threshold between linear and quadratic loss for huber loss

  • constraints (dict, optional, default None) – Dictionary of instances of Constraints. Must be of the form {i: constraints_i, j: constraints_j, ...} where i and j are indices of the features.

  • verbose (bool, optional, default False) – If True, print optimizer progress

predict(x)

Predict the polynomial model for data x

Parameters

x (ndarray (m, n)) – Test data for which predictions should be made

Returns

y – Model prediction

Return type

ndarray (n, )

polyfit.load_example()

Loads example data

Returns

  • X (ndarray (m, n)) – Predictor data

  • y (ndarray (n, )) – Target data