Transforms#
Transformers#
This module defines some sklearn transformers useful for defining flexible regression models for single features, as well as interactions between sets of columns.
Poly: for orthogonalized polynomial regression
Interaction: computing pairwise product for interactions
BSpline: arbitrary degree B-splines
NaturalSpline: natural cubic splines
- class modelmatrix.transforms.BSpline(degree=3, intercept=False, lower_bound=None, upper_bound=None, internal_knots=None, df=None, ext=0)#
Bases:
TransformerMixin,BaseEstimator- Parameters:
degree (int, default=3) – Degree of polynomial.
intercept (bool, default=False) – If False, a column of basis is dropped so that by adding an intercept column design stays full rank.
lower_bound (float, default=None) – Lower boundary not. Will be set to minimal value if not supplied.
upper_bound (float, default=None) – Upper boundary not. Will be set to maximal value if not supplied.
internal_knots (array-like (optional)) – Optional internal knots of B-spline. Will be set to appropriate quantiles based on df.
df (int, default=None) – Degrees of freedom for spline. Defaults to degree + intercept.
ext (int) – How B-splines are to be extended beyond the boundary using scipy.interpolate.splev.
- fit(X, y=None)#
Compute knots for B-spline representation.
- Parameters:
X (array-like) – Single feature on which B-spline will be evaluated.
y (None) – Ignored. This parameter exists only for compatibility with
Pipeline.
- transform(X)#
Construct design for B-splines using features X.
- Parameters:
X (array-like) – X on which splines will be evaluated.
- Returns:
XS – Evaluated splines.
- Return type:
np.ndarray
- class modelmatrix.transforms.Interaction(variables, columns, column_names)#
Bases:
TransformerMixin,BaseEstimatorForm the tensor product interaction from a group of columns.
- Parameters:
variables (sequence) – Variables in the interactions.
columns (dict) – Mapping from variable names to columns.
column_names (dict) – Mapping from variable names to lists of column names.
- fit(X, y=None)#
Nothing to be computed for the fit.
- Parameters:
X (array-like)
y (default=None) – Ignored. This parameter exists only for compatibility with
Pipeline.
- transform(X)#
Construct columns representing interactions of relevant variables.
- Parameters:
X (array-like) – X on which features will be evaluated.
- Returns:
XI – Evaluated interaction features.
- Return type:
np.ndarray
- class modelmatrix.transforms.NaturalSpline(intercept=False, lower_bound=None, upper_bound=None, internal_knots=None, df=None, ext=0)#
Bases:
TransformerMixin,BaseEstimatorNatural cubic spline.
- Parameters:
intercept (bool, default=False) – If False, a column of basis is dropped so that by adding an intercept column design stays full rank.
lower_bound (float, default=None) – Lower boundary not. Will be set to minimal value if not supplied.
upper_bound (float, default=None) – Upper boundary not. Will be set to maximal value if not supplied.
internal_knots (array-like (optional)) – Optional internal knots of B-spline. Will be set to appropriate quantiles based on df.
df (int, default=None) – Degrees of freedom for spline. Defaults to 3 + intercept.
ext (int, default=0) – How B-splines are to be extended beyond the boundary using scipy.interpolate.splev.
- fit(X, y=None)#
Compute knots for natural spline representation.
- Parameters:
X (array-like) – Single feature on which B-spline will be evaluated.
y (None) – Ignored. This parameter exists only for compatibility with
Pipeline.
- transform(X)#
Construct design for natural cubic splines for features X.
- Parameters:
X (array-like) – X on which natural cubic splines will be evaluated.
- Returns:
XN – Evaluated natural cubic spline features.
- Return type:
np.ndarray
- class modelmatrix.transforms.Poly(degree=1, intercept=False, raw=False)#
Bases:
TransformerMixin,BaseEstimator- Parameters:
degree (int, default=1) – Degree of polynomial.
intercept (bool, default=False) – Include a column for intercept?
raw (bool, default=False) – If False, perform a QR decomposition on the resulting matrix of powers of centered and / or scaled features.
- fit(X, y=None)#
Construct parameters for orthogonal polynomials in the feature X.
- Parameters:
X (array-like of shape (n_samples,)) – Features used in fitting svm. Assumed to have at least 2 columns.
y (default=None) – Ignored. This parameter exists only for compatibility with
Pipeline.
- transform(X)#
Construct parameters for orthogonal polynomials in the feature X.
- Parameters:
X (array-like) – X on which features will be evaluated.
- Returns:
XP – Evaluated polynomial features.
- Return type:
np.ndarray