Welcome to polyfit’s documentation!

polyfit enables convenient multivariate sklearn-compatible polynomial regression. Compared to sklearn, the underlying convex optimization algorithms are sometimes more stable than the ones used by sklearn. Furthermore, polyfit enables shape constrained models to make the fit monotonic or convex for example. Note that these shape constraints are enforced by inequality constraints in the optimization problem which might be numerically unstable.

Installation

pip install polyfit

Example

[1]:
import numpy as np
from polyfit import load_example, PolynomRegressor, Constraints
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1.inset_locator import zoomed_inset_axes, mark_inset

X, y = load_example()
x_plot = np.linspace(0, np.amax(X) + 2, 200)

X = X.reshape((-1,1))

DEG = 3

np_coeffs = np.polyfit(X.ravel(), y, DEG)
polyestimator = PolynomRegressor(deg=DEG)
vander = np.vander(x_plot, N = DEG +1)
pred_numpy = vander@np_coeffs


polyestimator = PolynomRegressor(deg=DEG)
monotone_constraint = Constraints(monotonicity='inc')
polyestimator.fit(X, y, loss = 'l2', constraints={0: monotone_constraint})
pred_mon = polyestimator.predict(x_plot.reshape(-1, 1))

f, ax = plt.subplots(1, figsize = (9, 5))
ax.set_xlim(-0.01, 85)
ax.set_ylim(0.2, 1.03)
ax.set_title("Unconstrained polynom versus constrained polynom")
ax.scatter(X, y, c='k', s=8)

ax.plot(x_plot, pred_numpy, c='b', label='Degree=3 Unconstrained')
ax.plot(x_plot, pred_mon, c='r', label='Degree=3 Monotonic')

axins = zoomed_inset_axes(ax, 2, loc='lower right', borderpad=1.5)
axins.set_xlim(55, 84) # apply the x-limits
axins.set_ylim(0.93, 1.028)

axins.yaxis.set_visible(False)
axins.xaxis.set_visible(False)
mark_inset(ax, axins, loc1=2, loc2=4, fc="none", ec="0")

axins.scatter(X, y, c='k', s=8)
axins.plot(x_plot, pred_numpy, c='b')
axins.plot(x_plot, pred_mon, c='r')

ax.legend(loc='upper left', frameon=False)
plt.subplots_adjust(top=0.92,
bottom=0.06,
left=0.05,
right=0.99)
plt.show()
_images/Example_1_0.png

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

Indices and tables