Penalties¶
Penalty matrix generators

pygam.penalties.
concave
(n, coef)¶ Builds a penalty matrix for PSplines with continuous features. Penalizes violation of a concave feature function.
Parameters:  n (int) – number of splines
 coef (arraylike) – coefficients of the feature function
Returns: penalty matrix
Return type: sparse csc matrix of shape (n,n)

pygam.penalties.
convex
(n, coef)¶ Builds a penalty matrix for PSplines with continuous features. Penalizes violation of a convex feature function.
Parameters:  n (int) – number of splines
 coef (arraylike) – coefficients of the feature function
Returns: penalty matrix
Return type: sparse csc matrix of shape (n,n)

pygam.penalties.
convexity_
(n, coef, convex=True)¶ Builds a penalty matrix for PSplines with continuous features. Penalizes violation of convexity in the feature function.
Parameters: Returns: penalty matrix
Return type: sparse csc matrix of shape (n,n)

pygam.penalties.
derivative
(n, coef, derivative=2, periodic=False)¶ Builds a penalty matrix for PSplines with continuous features. Penalizes the squared differences between basis coefficients.
Parameters: Returns: penalty matrix
Return type: sparse csc matrix of shape (n,n)

pygam.penalties.
l2
(n, coef)¶ Builds a penalty matrix for PSplines with categorical features. Penalizes the squared value of each basis coefficient.
Parameters:  n (int) – number of splines
 coef (unused) – for compatibility with constraints
Returns: penalty matrix
Return type: sparse csc matrix of shape (n,n)

pygam.penalties.
monotonic_dec
(n, coef)¶ Builds a penalty matrix for PSplines with continuous features. Penalizes violation of a monotonic decreasing feature function.
Parameters:  n (int) – number of splines
 coef (arraylike) – coefficients of the feature function
Returns: penalty matrix
Return type: sparse csc matrix of shape (n,n)

pygam.penalties.
monotonic_inc
(n, coef)¶ Builds a penalty matrix for PSplines with continuous features. Penalizes violation of a monotonic increasing feature function.
Parameters:  n (int) – number of splines
 coef (arraylike, coefficients of the feature function) –
Returns: penalty matrix
Return type: sparse csc matrix of shape (n,n)

pygam.penalties.
monotonicity_
(n, coef, increasing=True)¶ Builds a penalty matrix for PSplines with continuous features. Penalizes violation of monotonicity in the feature function.
Parameters: Returns: penalty matrix
Return type: 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
Returns: penalty matrix
Return type: 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: Returns: diff_array – same shape as input array, but ‘axis’ dimension is smaller by ‘n’.
Return type: 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.) – penaltymatrixgenerating function.
 fit_linear (boolean.) – whether the current feature has a linear term or not.
 linear_penalty (float, default: 0.) – penalty on the linear term
Returns: wrapped_p – modified penaltymatrixgenerating function
Return type: callable