transforms#
Module: transforms
#
Inheritance diagram for ISLP.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
Classes#
BSpline
#
- class ISLP.transforms.BSpline(degree=3, intercept=False, lower_bound=None, upper_bound=None, internal_knots=None, df=None, ext=0)#
Bases:
TransformerMixin
,BaseEstimator
- Parameters:
- degreeint, default=3
Degree of polynomial.
- interceptbool, default=False
If False, a column of basis is dropped so that by adding an intercept column design stays full rank.
- lower_boundfloat, default=None
Lower boundary not. Will be set to minimal value if not supplied.
- upper_boundfloat, default=None
Upper boundary not. Will be set to maximal value if not supplied.
- internal_knotsarray-like (optional)
Optional internal knots of B-spline. Will be set to appropriate quantiles based on df.
- dfint, default=None
Degrees of freedom for spline. Defaults to degree + intercept.
- extint
How B-splines are to be extended beyond the boundary using scipy.interpolate.splev.
Methods
fit
(X[, y])Compute knots for B-spline representation.
fit_transform
(X[, y])Fit to data, then transform it.
Get metadata routing of this object.
get_params
([deep])Get parameters for this estimator.
set_output
(*[, transform])Set output container.
set_params
(**params)Set the parameters of this estimator.
transform
(X)Construct design for B-splines using features X.
- __init__(degree=3, intercept=False, lower_bound=None, upper_bound=None, internal_knots=None, df=None, ext=0)#
- fit(X, y=None)#
Compute knots for B-spline representation.
- Parameters:
- Xarray-like
Single feature on which B-spline will be evaluated.
- yNone
Ignored. This parameter exists only for compatibility with
Pipeline
.
- fit_transform(X, y=None, **fit_params)#
Fit to data, then transform it.
Fits transformer to X and y with optional parameters fit_params and returns a transformed version of X.
- Parameters:
- Xarray-like of shape (n_samples, n_features)
Input samples.
- yarray-like of shape (n_samples,) or (n_samples, n_outputs), default=None
Target values (None for unsupervised transformations).
- **fit_paramsdict
Additional fit parameters.
- Returns:
- X_newndarray array of shape (n_samples, n_features_new)
Transformed array.
- get_metadata_routing()#
Get metadata routing of this object.
Please check User Guide on how the routing mechanism works.
- Returns:
- routingMetadataRequest
A
MetadataRequest
encapsulating routing information.
- get_params(deep=True)#
Get parameters for this estimator.
- Parameters:
- deepbool, default=True
If True, will return the parameters for this estimator and contained subobjects that are estimators.
- Returns:
- paramsdict
Parameter names mapped to their values.
- set_output(*, transform=None)#
Set output container.
See Introducing the set_output API for an example on how to use the API.
- Parameters:
- transform{“default”, “pandas”, “polars”}, default=None
Configure output of transform and fit_transform.
“default”: Default output format of a transformer
“pandas”: DataFrame output
“polars”: Polars output
None: Transform configuration is unchanged
Added in version 1.4: “polars” option was added.
- Returns:
- selfestimator instance
Estimator instance.
- set_params(**params)#
Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects (such as
Pipeline
). The latter have parameters of the form<component>__<parameter>
so that it’s possible to update each component of a nested object.- Parameters:
- **paramsdict
Estimator parameters.
- Returns:
- selfestimator instance
Estimator instance.
- transform(X)#
Construct design for B-splines using features X.
- Parameters:
- Xarray-like
X on which splines will be evaluated.
- Returns:
- XSnp.ndarray
Evaluated splines.
Interaction
#
- class ISLP.transforms.Interaction(variables, columns, column_names)#
Bases:
TransformerMixin
,BaseEstimator
Form the tensor product interaction from a group of columns.
- Parameters:
- variablessequence
Variables in the interactions.
- columnsdict
Mapping from variable names to columns.
- column_namesdict
Mapping from variable names to lists of column names.
Methods
fit
(X[, y])Nothing to be computed for the fit.
fit_transform
(X[, y])Fit to data, then transform it.
Get metadata routing of this object.
get_params
([deep])Get parameters for this estimator.
set_output
(*[, transform])Set output container.
set_params
(**params)Set the parameters of this estimator.
transform
(X)Construct columns representing interactions of relevant variables.
- __init__(variables, columns, column_names)#
- fit(X, y=None)#
Nothing to be computed for the fit.
- Parameters:
- Xarray-like
- ydefault=None
Ignored. This parameter exists only for compatibility with
Pipeline
.
- fit_transform(X, y=None, **fit_params)#
Fit to data, then transform it.
Fits transformer to X and y with optional parameters fit_params and returns a transformed version of X.
- Parameters:
- Xarray-like of shape (n_samples, n_features)
Input samples.
- yarray-like of shape (n_samples,) or (n_samples, n_outputs), default=None
Target values (None for unsupervised transformations).
- **fit_paramsdict
Additional fit parameters.
- Returns:
- X_newndarray array of shape (n_samples, n_features_new)
Transformed array.
- get_metadata_routing()#
Get metadata routing of this object.
Please check User Guide on how the routing mechanism works.
- Returns:
- routingMetadataRequest
A
MetadataRequest
encapsulating routing information.
- get_params(deep=True)#
Get parameters for this estimator.
- Parameters:
- deepbool, default=True
If True, will return the parameters for this estimator and contained subobjects that are estimators.
- Returns:
- paramsdict
Parameter names mapped to their values.
- set_output(*, transform=None)#
Set output container.
See Introducing the set_output API for an example on how to use the API.
- Parameters:
- transform{“default”, “pandas”, “polars”}, default=None
Configure output of transform and fit_transform.
“default”: Default output format of a transformer
“pandas”: DataFrame output
“polars”: Polars output
None: Transform configuration is unchanged
Added in version 1.4: “polars” option was added.
- Returns:
- selfestimator instance
Estimator instance.
- set_params(**params)#
Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects (such as
Pipeline
). The latter have parameters of the form<component>__<parameter>
so that it’s possible to update each component of a nested object.- Parameters:
- **paramsdict
Estimator parameters.
- Returns:
- selfestimator instance
Estimator instance.
- transform(X)#
Construct columns representing interactions of relevant variables.
- Parameters:
- Xarray-like
X on which features will be evaluated.
- Returns:
- XInp.ndarray
Evaluated interaction features.
NaturalSpline
#
- class ISLP.transforms.NaturalSpline(intercept=False, lower_bound=None, upper_bound=None, internal_knots=None, df=None, ext=0)#
Bases:
TransformerMixin
,BaseEstimator
Natural cubic spline.
- Parameters:
- interceptbool, default=False
If False, a column of basis is dropped so that by adding an intercept column design stays full rank.
- lower_boundfloat, default=None
Lower boundary not. Will be set to minimal value if not supplied.
- upper_boundfloat, default=None
Upper boundary not. Will be set to maximal value if not supplied.
- internal_knotsarray-like (optional)
Optional internal knots of B-spline. Will be set to appropriate quantiles based on df.
- dfint, default=None
Degrees of freedom for spline. Defaults to 3 + intercept.
- extint, default=0
How B-splines are to be extended beyond the boundary using scipy.interpolate.splev.
Methods
fit
(X[, y])Compute knots for natural spline representation.
fit_transform
(X[, y])Fit to data, then transform it.
Get metadata routing of this object.
get_params
([deep])Get parameters for this estimator.
set_output
(*[, transform])Set output container.
set_params
(**params)Set the parameters of this estimator.
transform
(X)Construct design for natural cubic splines for features X.
- __init__(intercept=False, lower_bound=None, upper_bound=None, internal_knots=None, df=None, ext=0)#
- fit(X, y=None)#
Compute knots for natural spline representation.
- Parameters:
- Xarray-like
Single feature on which B-spline will be evaluated.
- yNone
Ignored. This parameter exists only for compatibility with
Pipeline
.
- fit_transform(X, y=None, **fit_params)#
Fit to data, then transform it.
Fits transformer to X and y with optional parameters fit_params and returns a transformed version of X.
- Parameters:
- Xarray-like of shape (n_samples, n_features)
Input samples.
- yarray-like of shape (n_samples,) or (n_samples, n_outputs), default=None
Target values (None for unsupervised transformations).
- **fit_paramsdict
Additional fit parameters.
- Returns:
- X_newndarray array of shape (n_samples, n_features_new)
Transformed array.
- get_metadata_routing()#
Get metadata routing of this object.
Please check User Guide on how the routing mechanism works.
- Returns:
- routingMetadataRequest
A
MetadataRequest
encapsulating routing information.
- get_params(deep=True)#
Get parameters for this estimator.
- Parameters:
- deepbool, default=True
If True, will return the parameters for this estimator and contained subobjects that are estimators.
- Returns:
- paramsdict
Parameter names mapped to their values.
- set_output(*, transform=None)#
Set output container.
See Introducing the set_output API for an example on how to use the API.
- Parameters:
- transform{“default”, “pandas”, “polars”}, default=None
Configure output of transform and fit_transform.
“default”: Default output format of a transformer
“pandas”: DataFrame output
“polars”: Polars output
None: Transform configuration is unchanged
Added in version 1.4: “polars” option was added.
- Returns:
- selfestimator instance
Estimator instance.
- set_params(**params)#
Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects (such as
Pipeline
). The latter have parameters of the form<component>__<parameter>
so that it’s possible to update each component of a nested object.- Parameters:
- **paramsdict
Estimator parameters.
- Returns:
- selfestimator instance
Estimator instance.
- transform(X)#
Construct design for natural cubic splines for features X.
- Parameters:
- Xarray-like
X on which natural cubic splines will be evaluated.
- Returns:
- XNnp.ndarray
Evaluated natural cubic spline features.
Poly
#
- class ISLP.transforms.Poly(degree=1, intercept=False, raw=False)#
Bases:
TransformerMixin
,BaseEstimator
- Parameters:
- degreeint, default=1
Degree of polynomial.
- interceptbool, default=False
Include a column for intercept?
- rawbool, default=False
If False, perform a QR decomposition on the resulting matrix of powers of centered and / or scaled features.
Methods
fit
(X[, y])Construct parameters for orthogonal polynomials in the feature X.
fit_transform
(X[, y])Fit to data, then transform it.
Get metadata routing of this object.
get_params
([deep])Get parameters for this estimator.
set_output
(*[, transform])Set output container.
set_params
(**params)Set the parameters of this estimator.
transform
(X)Construct parameters for orthogonal polynomials in the feature X.
- __init__(degree=1, intercept=False, raw=False)#
- fit(X, y=None)#
Construct parameters for orthogonal polynomials in the feature X.
- Parameters:
- Xarray-like of shape (n_samples,)
Features used in fitting svm. Assumed to have at least 2 columns.
- ydefault=None
Ignored. This parameter exists only for compatibility with
Pipeline
.
- fit_transform(X, y=None, **fit_params)#
Fit to data, then transform it.
Fits transformer to X and y with optional parameters fit_params and returns a transformed version of X.
- Parameters:
- Xarray-like of shape (n_samples, n_features)
Input samples.
- yarray-like of shape (n_samples,) or (n_samples, n_outputs), default=None
Target values (None for unsupervised transformations).
- **fit_paramsdict
Additional fit parameters.
- Returns:
- X_newndarray array of shape (n_samples, n_features_new)
Transformed array.
- get_metadata_routing()#
Get metadata routing of this object.
Please check User Guide on how the routing mechanism works.
- Returns:
- routingMetadataRequest
A
MetadataRequest
encapsulating routing information.
- get_params(deep=True)#
Get parameters for this estimator.
- Parameters:
- deepbool, default=True
If True, will return the parameters for this estimator and contained subobjects that are estimators.
- Returns:
- paramsdict
Parameter names mapped to their values.
- set_output(*, transform=None)#
Set output container.
See Introducing the set_output API for an example on how to use the API.
- Parameters:
- transform{“default”, “pandas”, “polars”}, default=None
Configure output of transform and fit_transform.
“default”: Default output format of a transformer
“pandas”: DataFrame output
“polars”: Polars output
None: Transform configuration is unchanged
Added in version 1.4: “polars” option was added.
- Returns:
- selfestimator instance
Estimator instance.
- set_params(**params)#
Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects (such as
Pipeline
). The latter have parameters of the form<component>__<parameter>
so that it’s possible to update each component of a nested object.- Parameters:
- **paramsdict
Estimator parameters.
- Returns:
- selfestimator instance
Estimator instance.
- transform(X)#
Construct parameters for orthogonal polynomials in the feature X.
- Parameters:
- Xarray-like
X on which features will be evaluated.
- Returns:
- XPnp.ndarray
Evaluated polynomial features.