-
-
Notifications
You must be signed in to change notification settings - Fork 26.2k
FEA Add metadata routing to SelectFromModel #27490
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
844db02
3238b6c
aa902ac
e88e0bd
7c122fe
01b6aaf
bf0bd38
776f448
7c2c364
3a74974
8a5de31
bc75dfe
969ef28
508e012
8724ad3
2479530
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -11,8 +11,10 @@ | |||||||||||||||||
from ..utils._param_validation import HasMethods, Interval, Options | ||||||||||||||||||
from ..utils._tags import _safe_tags | ||||||||||||||||||
from ..utils.metadata_routing import ( | ||||||||||||||||||
_raise_for_unsupported_routing, | ||||||||||||||||||
_RoutingNotSupportedMixin, | ||||||||||||||||||
MetadataRouter, | ||||||||||||||||||
MethodMapping, | ||||||||||||||||||
_routing_enabled, | ||||||||||||||||||
process_routing, | ||||||||||||||||||
) | ||||||||||||||||||
from ..utils.metaestimators import available_if | ||||||||||||||||||
from ..utils.validation import _num_features, check_is_fitted, check_scalar | ||||||||||||||||||
|
@@ -82,9 +84,7 @@ def _estimator_has(attr): | |||||||||||||||||
) | ||||||||||||||||||
|
||||||||||||||||||
|
||||||||||||||||||
class SelectFromModel( | ||||||||||||||||||
_RoutingNotSupportedMixin, MetaEstimatorMixin, SelectorMixin, BaseEstimator | ||||||||||||||||||
): | ||||||||||||||||||
class SelectFromModel(MetaEstimatorMixin, SelectorMixin, BaseEstimator): | ||||||||||||||||||
"""Meta-transformer for selecting features based on importance weights. | ||||||||||||||||||
|
||||||||||||||||||
.. versionadded:: 0.17 | ||||||||||||||||||
|
@@ -341,14 +341,25 @@ def fit(self, X, y=None, **fit_params): | |||||||||||||||||
classification, real numbers in regression). | ||||||||||||||||||
|
||||||||||||||||||
**fit_params : dict | ||||||||||||||||||
Other estimator specific parameters. | ||||||||||||||||||
- If `enable_metadata_routing=False` (default): | ||||||||||||||||||
|
||||||||||||||||||
Parameters directly passed to the `partial_fit` method of the | ||||||||||||||||||
sub-estimator. They are ignored if `prefit=True`. | ||||||||||||||||||
|
||||||||||||||||||
- If `enable_metadata_routing=True`: | ||||||||||||||||||
|
||||||||||||||||||
Parameters safely routed to the `partial_fit` method of the | ||||||||||||||||||
sub-estimator. They are ignored if `prefit=True`. | ||||||||||||||||||
|
||||||||||||||||||
.. versionchanged:: 1.4 | ||||||||||||||||||
See :ref:`Metadata Routing User Guide <metadata_routing>` for | ||||||||||||||||||
more details. | ||||||||||||||||||
|
||||||||||||||||||
Returns | ||||||||||||||||||
------- | ||||||||||||||||||
self : object | ||||||||||||||||||
Fitted estimator. | ||||||||||||||||||
""" | ||||||||||||||||||
_raise_for_unsupported_routing(self, "fit", **fit_params) | ||||||||||||||||||
self._check_max_features(X) | ||||||||||||||||||
|
||||||||||||||||||
if self.prefit: | ||||||||||||||||||
|
@@ -361,8 +372,14 @@ def fit(self, X, y=None, **fit_params): | |||||||||||||||||
) from exc | ||||||||||||||||||
self.estimator_ = deepcopy(self.estimator) | ||||||||||||||||||
else: | ||||||||||||||||||
self.estimator_ = clone(self.estimator) | ||||||||||||||||||
self.estimator_.fit(X, y, **fit_params) | ||||||||||||||||||
if _routing_enabled(): | ||||||||||||||||||
routed_params = process_routing(self, "fit", **fit_params) | ||||||||||||||||||
self.estimator_ = clone(self.estimator) | ||||||||||||||||||
self.estimator_.fit(X, y, **routed_params.estimator.fit) | ||||||||||||||||||
else: | ||||||||||||||||||
# TODO(SLEP6): remove when metadata routing cannot be disabled. | ||||||||||||||||||
self.estimator_ = clone(self.estimator) | ||||||||||||||||||
self.estimator_.fit(X, y, **fit_params) | ||||||||||||||||||
|
||||||||||||||||||
if hasattr(self.estimator_, "feature_names_in_"): | ||||||||||||||||||
self.feature_names_in_ = self.estimator_.feature_names_in_ | ||||||||||||||||||
|
@@ -387,7 +404,7 @@ def threshold_(self): | |||||||||||||||||
# SelectFromModel.estimator is not validated yet | ||||||||||||||||||
prefer_skip_nested_validation=False | ||||||||||||||||||
) | ||||||||||||||||||
def partial_fit(self, X, y=None, **fit_params): | ||||||||||||||||||
def partial_fit(self, X, y=None, **partial_fit_params): | ||||||||||||||||||
"""Fit the SelectFromModel meta-transformer only once. | ||||||||||||||||||
|
||||||||||||||||||
Parameters | ||||||||||||||||||
|
@@ -399,8 +416,24 @@ def partial_fit(self, X, y=None, **fit_params): | |||||||||||||||||
The target values (integers that correspond to classes in | ||||||||||||||||||
classification, real numbers in regression). | ||||||||||||||||||
|
||||||||||||||||||
**fit_params : dict | ||||||||||||||||||
Other estimator specific parameters. | ||||||||||||||||||
**partial_fit_params : dict | ||||||||||||||||||
- If `enable_metadata_routing=False` (default): | ||||||||||||||||||
StefanieSenger marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||
|
||||||||||||||||||
Parameters directly passed to the `partial_fit` method of the | ||||||||||||||||||
sub-estimator. | ||||||||||||||||||
|
||||||||||||||||||
- If `enable_metadata_routing=True`: | ||||||||||||||||||
|
||||||||||||||||||
Parameters passed to the `partial_fit` method of the | ||||||||||||||||||
sub-estimator. They are ignored if `prefit=True`. | ||||||||||||||||||
|
||||||||||||||||||
.. versionchanged:: 1.4 | ||||||||||||||||||
`**partial_fit_params` are routed to the sub-estimator, if | ||||||||||||||||||
`enable_metadata_routing=True` is set via | ||||||||||||||||||
:func:`~sklearn.set_config`, which allows for aliasing. | ||||||||||||||||||
Comment on lines
+431
to
+433
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, I would suggest to leave this part below What about this?
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree. |
||||||||||||||||||
|
||||||||||||||||||
See :ref:`Metadata Routing User Guide <metadata_routing>` for | ||||||||||||||||||
more details. | ||||||||||||||||||
|
||||||||||||||||||
Returns | ||||||||||||||||||
------- | ||||||||||||||||||
|
@@ -426,7 +459,13 @@ def partial_fit(self, X, y=None, **fit_params): | |||||||||||||||||
|
||||||||||||||||||
if first_call: | ||||||||||||||||||
self.estimator_ = clone(self.estimator) | ||||||||||||||||||
self.estimator_.partial_fit(X, y, **fit_params) | ||||||||||||||||||
if _routing_enabled(): | ||||||||||||||||||
routed_params = process_routing(self, "partial_fit", **partial_fit_params) | ||||||||||||||||||
self.estimator_ = clone(self.estimator) | ||||||||||||||||||
self.estimator_.partial_fit(X, y, **routed_params.estimator.partial_fit) | ||||||||||||||||||
else: | ||||||||||||||||||
# TODO(SLEP6): remove when metadata routing cannot be disabled. | ||||||||||||||||||
self.estimator_.partial_fit(X, y, **partial_fit_params) | ||||||||||||||||||
|
||||||||||||||||||
if hasattr(self.estimator_, "feature_names_in_"): | ||||||||||||||||||
self.feature_names_in_ = self.estimator_.feature_names_in_ | ||||||||||||||||||
|
@@ -451,5 +490,27 @@ def n_features_in_(self): | |||||||||||||||||
|
||||||||||||||||||
return self.estimator_.n_features_in_ | ||||||||||||||||||
|
||||||||||||||||||
def get_metadata_routing(self): | ||||||||||||||||||
"""Get metadata routing of this object. | ||||||||||||||||||
|
||||||||||||||||||
Please check :ref:`User Guide <metadata_routing>` on how the routing | ||||||||||||||||||
mechanism works. | ||||||||||||||||||
|
||||||||||||||||||
.. versionadded:: 1.4 | ||||||||||||||||||
|
||||||||||||||||||
Returns | ||||||||||||||||||
------- | ||||||||||||||||||
routing : MetadataRouter | ||||||||||||||||||
A :class:`~sklearn.utils.metadata_routing.MetadataRouter` encapsulating | ||||||||||||||||||
routing information. | ||||||||||||||||||
""" | ||||||||||||||||||
router = MetadataRouter(owner=self.__class__.__name__).add( | ||||||||||||||||||
estimator=self.estimator, | ||||||||||||||||||
method_mapping=MethodMapping() | ||||||||||||||||||
.add(callee="partial_fit", caller="partial_fit") | ||||||||||||||||||
.add(callee="fit", caller="fit"), | ||||||||||||||||||
) | ||||||||||||||||||
return router | ||||||||||||||||||
|
||||||||||||||||||
def _more_tags(self): | ||||||||||||||||||
return {"allow_nan": _safe_tags(self.estimator, key="allow_nan")} |
Uh oh!
There was an error while loading. Please reload this page.