models.sklearn_wrap#
Module: models.sklearn_wrap
#
Inheritance diagram for ISLP.models.sklearn_wrap
:
Wrappers for statsmodels#
Classes#
sklearn_selected
#
- class ISLP.models.sklearn_wrap.sklearn_selected(model_type, strategy, model_args={}, scoring=None, cv=None)#
Bases:
sklearn_sm
- Parameters:
- model_typeclass
A model type from statsmodels, e.g. sm.OLS or sm.GLM
- strategyStrategy
A search strategy
- model_argsdict (optional)
Arguments passed to the statsmodels model.
- scoringstr or callable, default=None
A str (see model evaluation documentation) or a scorer callable object / function with signature scorer(estimator, X, y) which should return only a single value.
- cv: int, cross-validation generator or an iterable, default=None
Determines the cross-validation splitting strategy.
Methods
fit
(X, y)First, select a model with design matrix determined from X and response y.
Get metadata routing of this object.
get_params
([deep])Get parameters for this estimator.
predict
(X)Compute predictions for design matrix X in selected model.
score
(X, y[, sample_weight])Score a statsmodel model with test design matrix X and test response y.
set_params
(**params)Set the parameters of this estimator.
set_score_request
(*[, sample_weight])Request metadata passed to the
score
method.- __init__(model_type, strategy, model_args={}, scoring=None, cv=None)#
- fit(X, y)#
First, select a model with design matrix determined from X and response y. Then, fit selected model.
- Parameters:
- Xarray-like
Design matrix.
- yarray-like
Response vector.
- 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.
- predict(X)#
Compute predictions for design matrix X in selected model.
- Parameters:
- Xarray-like
Design matrix.
- score(X, y, sample_weight=None)#
Score a statsmodel model with test design matrix X and test response y.
If model_type is OLS, use MSE. For a GLM this computes (average) deviance.
- Parameters:
- Xarray-like
Design matrix.
- yarray-like
Response vector.
- sample_weightNone
Optional sample weights.
- 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.
- set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') sklearn_selected #
Request metadata passed to the
score
method.Note that this method is only relevant if
enable_metadata_routing=True
(seesklearn.set_config()
). Please see User Guide on how the routing mechanism works.The options for each parameter are:
True
: metadata is requested, and passed toscore
if provided. The request is ignored if metadata is not provided.False
: metadata is not requested and the meta-estimator will not pass it toscore
.None
: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str
: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED
) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
Note
This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a
Pipeline
. Otherwise it has no effect.- Parameters:
- sample_weightstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
sample_weight
parameter inscore
.
- Returns:
- selfobject
The updated object.
sklearn_selection_path
#
- class ISLP.models.sklearn_wrap.sklearn_selection_path(model_type, strategy, model_args={}, scoring=None, cv=None)#
Bases:
sklearn_sm
- Parameters:
- model_typeclass
A model type from statsmodels, e.g. sm.OLS or sm.GLM
- strategyStrategy
A search strategy
- model_argsdict (optional)
Arguments passed to the statsmodels model.
- scoringstr or callable, default=None
A str (see model evaluation documentation) or a scorer callable object / function with signature scorer(estimator, X, y) which should return only a single value.
- cv: int, cross-validation generator or an iterable, default=None
Determines the cross-validation splitting strategy.
Methods
fit
(X, y)First, select a model with design matrix determined from X and response y.
Get metadata routing of this object.
get_params
([deep])Get parameters for this estimator.
predict
(X)Compute predictions along selection path for design matrix X.
score
(X, y[, sample_weight])Score a statsmodel model with test design matrix X and test response y.
set_params
(**params)Set the parameters of this estimator.
set_score_request
(*[, sample_weight])Request metadata passed to the
score
method.- __init__(model_type, strategy, model_args={}, scoring=None, cv=None)#
- fit(X, y)#
First, select a model with design matrix determined from X and response y. Then, fit selected model.
- Parameters:
- Xarray-like
Design matrix.
- yarray-like
Response vector.
- 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.
- predict(X)#
Compute predictions along selection path for design matrix X.
- Parameters:
- Xarray-like
Design matrix.
- score(X, y, sample_weight=None)#
Score a statsmodel model with test design matrix X and test response y.
If model_type is OLS, use MSE. For a GLM this computes (average) deviance.
- Parameters:
- Xarray-like
Design matrix.
- yarray-like
Response vector.
- sample_weightNone
Optional sample weights.
- 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.
- set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') sklearn_selection_path #
Request metadata passed to the
score
method.Note that this method is only relevant if
enable_metadata_routing=True
(seesklearn.set_config()
). Please see User Guide on how the routing mechanism works.The options for each parameter are:
True
: metadata is requested, and passed toscore
if provided. The request is ignored if metadata is not provided.False
: metadata is not requested and the meta-estimator will not pass it toscore
.None
: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str
: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED
) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
Note
This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a
Pipeline
. Otherwise it has no effect.- Parameters:
- sample_weightstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
sample_weight
parameter inscore
.
- Returns:
- selfobject
The updated object.
sklearn_sm
#
- class ISLP.models.sklearn_wrap.sklearn_sm(model_type, model_spec=None, model_args={})#
Bases:
BaseEstimator
,RegressorMixin
- Parameters:
- model_type: class
A model type from statsmodels, e.g. sm.OLS or sm.GLM
- model_spec: ModelSpec
Specify the design matrix.
- model_args: dict (optional)
Arguments passed to the statsmodels model.
Notes
If model_str is present, then X and Y are presumed to be pandas objects that are placed into a dataframe before formula is evaluated. This affects fit and predict methods.
Methods
fit
(X, y)Fit a statsmodel model with design matrix determined from X and response y.
Get metadata routing of this object.
get_params
([deep])Get parameters for this estimator.
predict
(X)Compute predictions for design matrix X.
score
(X, y[, sample_weight])Score a statsmodel model with test design matrix X and test response y.
set_params
(**params)Set the parameters of this estimator.
set_score_request
(*[, sample_weight])Request metadata passed to the
score
method.- __init__(model_type, model_spec=None, model_args={})#
- fit(X, y)#
Fit a statsmodel model with design matrix determined from X and response y.
- Parameters:
- Xarray-like
Design matrix.
- yarray-like
Response vector.
- 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.
- predict(X)#
Compute predictions for design matrix X.
- Parameters:
- Xarray-like
Design matrix.
- score(X, y, sample_weight=None)#
Score a statsmodel model with test design matrix X and test response y.
If model_type is OLS, use MSE. For a GLM this computes (average) deviance.
- Parameters:
- Xarray-like
Design matrix.
- yarray-like
Response vector.
- sample_weightNone
Optional sample weights.
- 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.
- set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') sklearn_sm #
Request metadata passed to the
score
method.Note that this method is only relevant if
enable_metadata_routing=True
(seesklearn.set_config()
). Please see User Guide on how the routing mechanism works.The options for each parameter are:
True
: metadata is requested, and passed toscore
if provided. The request is ignored if metadata is not provided.False
: metadata is not requested and the meta-estimator will not pass it toscore
.None
: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str
: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED
) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
Note
This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a
Pipeline
. Otherwise it has no effect.- Parameters:
- sample_weightstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
sample_weight
parameter inscore
.
- Returns:
- selfobject
The updated object.