# Penalties¶

Penalty matrix generators

pygam.penalties.concave(n, coef)

Builds a penalty matrix for P-Splines with continuous features. Penalizes violation of a concave feature function.

Parameters: n (int) – number of splines coef (array-like) – coefficients of the feature function penalty matrix sparse csc matrix of shape (n,n)
pygam.penalties.convex(n, coef)

Builds a penalty matrix for P-Splines with continuous features. Penalizes violation of a convex feature function.

Parameters: n (int) – number of splines coef (array-like) – coefficients of the feature function penalty matrix sparse csc matrix of shape (n,n)
pygam.penalties.convexity_(n, coef, convex=True)

Builds a penalty matrix for P-Splines with continuous features. Penalizes violation of convexity in the feature function.

Parameters: n (int) – number of splines coef (array-like) – coefficients of the feature function convex (bool, default: True) – whether to enforce convex, or concave functions penalty matrix sparse csc matrix of shape (n,n)
pygam.penalties.derivative(n, coef, derivative=2, periodic=False)

Builds a penalty matrix for P-Splines with continuous features. Penalizes the squared differences between basis coefficients.

Parameters: n (int) – number of splines coef (unused) – for compatibility with constraints derivative (int, default: 2) – which derivative do we penalize. derivative is 1, we penalize 1st order derivatives, derivative is 2, we penalize 2nd order derivatives, etc penalty matrix sparse csc matrix of shape (n,n)
pygam.penalties.l2(n, coef)

Builds a penalty matrix for P-Splines with categorical features. Penalizes the squared value of each basis coefficient.

Parameters: n (int) – number of splines coef (unused) – for compatibility with constraints penalty matrix sparse csc matrix of shape (n,n)
pygam.penalties.monotonic_dec(n, coef)

Builds a penalty matrix for P-Splines with continuous features. Penalizes violation of a monotonic decreasing feature function.

Parameters: n (int) – number of splines coef (array-like) – coefficients of the feature function penalty matrix sparse csc matrix of shape (n,n)
pygam.penalties.monotonic_inc(n, coef)

Builds a penalty matrix for P-Splines with continuous features. Penalizes violation of a monotonic increasing feature function.

Parameters: n (int) – number of splines coef (array-like, coefficients of the feature function) – penalty matrix sparse csc matrix of shape (n,n)
pygam.penalties.monotonicity_(n, coef, increasing=True)

Builds a penalty matrix for P-Splines with continuous features. Penalizes violation of monotonicity in the feature function.

Parameters: n (int) – number of splines coef (array-like) – coefficients of the feature function increasing (bool, default: True) – whether to enforce monotic increasing, or decreasing functions penalty matrix sparse csc matrix of shape (n,n)
pygam.penalties.none(n, coef)

Build a matrix of zeros for features that should go unpenalized

Parameters: n (int) – number of splines coef (unused) – for compatibility with constraints penalty matrix sparse csc matrix of shape (n,n)
pygam.penalties.periodic(n, coef, derivative=2, _penalty=<function derivative>)
pygam.penalties.sparse_diff(array, n=1, axis=-1)

A ported sparse version of np.diff. Uses recursion to compute higher order differences

Parameters: array (sparse array) – n (int, default: 1) – differencing order axis (int, default: -1) – axis along which differences are computed diff_array – same shape as input array, but ‘axis’ dimension is smaller by ‘n’. sparse array
pygam.penalties.wrap_penalty(p, fit_linear, linear_penalty=0.0)

tool to account for unity penalty on the linear term of any feature.

Example

p = wrap_penalty(derivative, fit_linear=True)(n, coef)

Parameters: p (callable.) – penalty-matrix-generating function. fit_linear (boolean.) – whether the current feature has a linear term or not. linear_penalty (float, default: 0.) – penalty on the linear term wrapped_p – modified penalty-matrix-generating function callable