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])Configure whether metadata should be requested to be passed to the
scoremethod.- __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
MetadataRequestencapsulating 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#
Configure whether metadata should be requested to be passed to the
scoremethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed toscoreif 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.
- Parameters:
- sample_weightstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
sample_weightparameter 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])Configure whether metadata should be requested to be passed to the
scoremethod.- __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
MetadataRequestencapsulating 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#
Configure whether metadata should be requested to be passed to the
scoremethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed toscoreif 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.
- Parameters:
- sample_weightstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
sample_weightparameter 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.
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])Configure whether metadata should be requested to be passed to the
scoremethod.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.
- __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
MetadataRequestencapsulating 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#
Configure whether metadata should be requested to be passed to the
scoremethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed toscoreif 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.
- Parameters:
- sample_weightstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
sample_weightparameter inscore.
- Returns:
- selfobject
The updated object.