models.sklearn_wrap#

Module: models.sklearn_wrap#

Inheritance diagram for ISLP.models.sklearn_wrap:

digraph inheritance86e7436895 { bgcolor=transparent; rankdir=LR; size="8.0, 12.0"; "_repr_html.base.ReprHTMLMixin" [fillcolor=white,fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=10,height=0.25,shape=box,style="setlinewidth(0.5),filled",tooltip="Mixin to handle consistently the HTML representation."]; "models.sklearn_wrap.sklearn_selected" [URL="#ISLP.models.sklearn_wrap.sklearn_selected",fillcolor=white,fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=10,height=0.25,shape=box,style="setlinewidth(0.5),filled",target="_top",tooltip="Parameters"]; "models.sklearn_wrap.sklearn_sm" -> "models.sklearn_wrap.sklearn_selected" [arrowsize=0.5,style="setlinewidth(0.5)"]; "models.sklearn_wrap.sklearn_selection_path" [URL="#ISLP.models.sklearn_wrap.sklearn_selection_path",fillcolor=white,fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=10,height=0.25,shape=box,style="setlinewidth(0.5),filled",target="_top",tooltip="Parameters"]; "models.sklearn_wrap.sklearn_sm" -> "models.sklearn_wrap.sklearn_selection_path" [arrowsize=0.5,style="setlinewidth(0.5)"]; "models.sklearn_wrap.sklearn_sm" [URL="#ISLP.models.sklearn_wrap.sklearn_sm",fillcolor=white,fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=10,height=0.25,shape=box,style="setlinewidth(0.5),filled",target="_top",tooltip="Parameters"]; "sklearn.base.BaseEstimator" -> "models.sklearn_wrap.sklearn_sm" [arrowsize=0.5,style="setlinewidth(0.5)"]; "sklearn.base.RegressorMixin" -> "models.sklearn_wrap.sklearn_sm" [arrowsize=0.5,style="setlinewidth(0.5)"]; "sklearn.base.BaseEstimator" [URL="http://scikit-learn.org/stable/modules/generated/sklearn.base.BaseEstimator.html#sklearn.base.BaseEstimator",fillcolor=white,fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=10,height=0.25,shape=box,style="setlinewidth(0.5),filled",target="_top",tooltip="Base class for all estimators in scikit-learn."]; "_repr_html.base.ReprHTMLMixin" -> "sklearn.base.BaseEstimator" [arrowsize=0.5,style="setlinewidth(0.5)"]; "sklearn.base.RegressorMixin" [URL="http://scikit-learn.org/stable/modules/generated/sklearn.base.RegressorMixin.html#sklearn.base.RegressorMixin",fillcolor=white,fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=10,height=0.25,shape=box,style="setlinewidth(0.5),filled",target="_top",tooltip="Mixin class for all regression estimators in scikit-learn."]; }

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()

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 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#

Configure whether metadata should be requested to be passed to the score method.

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 (see sklearn.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 to score if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to score.

  • 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_weight parameter in score.

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()

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 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#

Configure whether metadata should be requested to be passed to the score method.

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 (see sklearn.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 to score if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to score.

  • 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_weight parameter in score.

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()

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 score method.

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 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#

Configure whether metadata should be requested to be passed to the score method.

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 (see sklearn.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 to score if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to score.

  • 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_weight parameter in score.

Returns:
selfobject

The updated object.