Note
Go to the end
to download the full example code. or to run this example in your browser via JupyterLite or Binder
Metadata Routing#
This document shows how you can use the metadata routing mechanism in scikit-learn to route metadata to the estimators,
scorers, and CV splitters consuming them.
To better understand the following document, we need to introduce two concepts:
routers and consumers. A router is an object which forwards some given data and
metadata to other objects. In most cases, a router is a meta-estimator,
i.e. an estimator which takes another estimator as a parameter. A function such
as sklearn.model_selection.cross_validate
which takes an estimator as a
parameter and forwards data and metadata, is also a router.
A consumer, on the other hand, is an object which accepts and uses some given
metadata. For instance, an estimator taking into account sample_weight
in
its fit method is a consumer of sample_weight
.
It is possible for an object to be both a router and a consumer. For instance,
a meta-estimator may take into account sample_weight
in certain
calculations, but it may also route it to the underlying estimator.
First a few imports and some random data for the rest of the script.
# Authors: The scikit-learn developers
# SPDX-License-Identifier: BSD-3-Clause
importwarnings
frompprintimport pprint
importnumpyasnp
fromsklearnimport set_config
fromsklearn.baseimport (
BaseEstimator ,
ClassifierMixin ,
MetaEstimatorMixin ,
RegressorMixin ,
TransformerMixin ,
clone,
)
fromsklearn.linear_modelimport LinearRegression
fromsklearn.utilsimport metadata_routing
fromsklearn.utils.metadata_routingimport (
MetadataRouter ,
MethodMapping ,
get_routing_for_object ,
process_routing ,
)
fromsklearn.utils.validationimport check_is_fitted
n_samples, n_features = 100, 4
rng = np.random.RandomState (42)
X = rng.rand(n_samples, n_features)
y = rng.randint(0, 2, size=n_samples)
my_groups = rng.randint(0, 10, size=n_samples)
my_weights = rng.rand(n_samples)
my_other_weights = rng.rand(n_samples)
Metadata routing is only available if explicitly enabled:
This utility function is a dummy to check if a metadata is passed:
defcheck_metadata(obj, **kwargs):
for key, value in kwargs.items():
if value is not None:
print(
f"Received {key} of length = {len(value)} in {obj.__class__.__name__}."
)
else:
print(f"{key} is None in {obj.__class__.__name__}.")
A utility function to nicely print the routing information of an object:
defprint_routing(obj):
pprint (obj.get_metadata_routing()._serialize())
Consuming Estimator#
Here we demonstrate how an estimator can expose the required API to support
metadata routing as a consumer. Imagine a simple classifier accepting
sample_weight
as a metadata on its fit
and groups
in its
predict
method:
classExampleClassifier(ClassifierMixin , BaseEstimator ):
deffit(self, X, y, sample_weight=None):
check_metadata(self, sample_weight=sample_weight)
# all classifiers need to expose a classes_ attribute once they're fit.
self.classes_ = np.array ([0, 1])
return self
defpredict(self, X, groups=None):
check_metadata(self, groups=groups)
# return a constant value of 1, not a very smart classifier!
return np.ones (len(X))
The above estimator now has all it needs to consume metadata. This is
accomplished by some magic done in BaseEstimator
. There are
now three methods exposed by the above class: set_fit_request
,
set_predict_request
, and get_metadata_routing
. There is also a
set_score_request
for sample_weight
which is present since
ClassifierMixin
implements a score
method accepting
sample_weight
. The same applies to regressors which inherit from
RegressorMixin
.
By default, no metadata is requested, which we can see as:
print_routing(ExampleClassifier())
{'fit': {'sample_weight': None},
'predict': {'groups': None},
'score': {'sample_weight': None}}
The above output means that sample_weight
and groups
are not
requested by ExampleClassifier
, and if a router is given those metadata, it
should raise an error, since the user has not explicitly set whether they are
required or not. The same is true for sample_weight
in the score
method, which is inherited from ClassifierMixin
. In order to
explicitly set request values for those metadata, we can use these methods:
est = (
ExampleClassifier()
.set_fit_request(sample_weight=False)
.set_predict_request(groups=True)
.set_score_request(sample_weight=False)
)
print_routing(est)
{'fit': {'sample_weight': False},
'predict': {'groups': True},
'score': {'sample_weight': False}}
Note
Please note that as long as the above estimator is not used in a
meta-estimator, the user does not need to set any requests for the
metadata and the set values are ignored, since a consumer does not
validate or route given metadata. A simple usage of the above estimator
would work as expected.
est = ExampleClassifier()
est.fit(X, y, sample_weight=my_weights)
est.predict(X[:3, :], groups=my_groups)
Received sample_weight of length = 100 in ExampleClassifier.
Received groups of length = 100 in ExampleClassifier.
array([1., 1., 1.])
Note that the above example is calling our utility function
check_metadata()
via the ExampleClassifier
. It checks that
sample_weight
is correctly passed to it. If it is not, like in the
following example, it would print that sample_weight
is None
:
sample_weight is None in ExampleClassifier.
MetaClassifier(estimator=ExampleClassifier())
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
If we pass an unknown metadata, an error is raised:
try:
meta_est.fit(X, y, test=my_weights)
except TypeError as e:
print(e)
MetaClassifier.fit got unexpected argument(s) {'test'}, which are not routed to any object.
And if we pass a metadata which is not explicitly requested:
try:
meta_est.fit(X, y, sample_weight=my_weights).predict(X, groups=my_groups)
except ValueError as e:
print(e)
Received sample_weight of length = 100 in ExampleClassifier.
[groups] are passed but are not explicitly set as requested or not requested for ExampleClassifier.predict, which is used within MetaClassifier.predict. Call `ExampleClassifier.set_predict_request({metadata}=True/False)` for each metadata you want to request/ignore. See the Metadata Routing User guide <https://scikit-learn.org/stable/metadata_routing.html> for more information.
Also, if we explicitly set it as not requested, but it is provided:
meta_est = MetaClassifier(
estimator=ExampleClassifier()
.set_fit_request(sample_weight=True)
.set_predict_request(groups=False)
)
try:
meta_est.fit(X, y, sample_weight=my_weights).predict(X[:3, :], groups=my_groups)
except TypeError as e:
print(e)
Received sample_weight of length = 100 in ExampleClassifier.
MetaClassifier.predict got unexpected argument(s) {'groups'}, which are not routed to any object.
Another concept to introduce is aliased metadata. This is when an
estimator requests a metadata with a different variable name than the default
variable name. For instance, in a setting where there are two estimators in a
pipeline, one could request sample_weight1
and the other
sample_weight2
. Note that this doesn’t change what the estimator expects,
it only tells the meta-estimator how to map the provided metadata to what is
required. Here’s an example, where we pass aliased_sample_weight
to the
meta-estimator, but the meta-estimator understands that
aliased_sample_weight
is an alias for sample_weight
, and passes it as
sample_weight
to the underlying estimator:
meta_est = MetaClassifier(
estimator=ExampleClassifier().set_fit_request(sample_weight="aliased_sample_weight")
)
meta_est.fit(X, y, aliased_sample_weight=my_weights)
Received sample_weight of length = 100 in ExampleClassifier.
MetaClassifier(estimator=ExampleClassifier())
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
Passing sample_weight
here will fail since it is requested with an
alias and sample_weight
with that name is not requested:
try:
meta_est.fit(X, y, sample_weight=my_weights)
except TypeError as e:
print(e)
MetaClassifier.fit got unexpected argument(s) {'sample_weight'}, which are not routed to any object.
This leads us to the get_metadata_routing
. The way routing works in
scikit-learn is that consumers request what they need, and routers pass that
along. Additionally, a router exposes what it requires itself so that it can
be used inside another router, e.g. a pipeline inside a grid search object.
The output of the get_metadata_routing
which is a dictionary
representation of a MetadataRouter
, includes
the complete tree of requested metadata by all nested objects and their
corresponding method routings, i.e. which method of a sub-estimator is used
in which method of a meta-estimator:
{'estimator': {'mapping': [{'callee': 'fit', 'caller': 'fit'},
{'callee': 'predict', 'caller': 'predict'},
{'callee': 'score', 'caller': 'score'}],
'router': {'fit': {'sample_weight': 'aliased_sample_weight'},
'predict': {'groups': None},
'score': {'sample_weight': None}}}}
As you can see, the only metadata requested for method fit
is
"sample_weight"
with "aliased_sample_weight"
as the alias. The
~utils.metadata_routing.MetadataRouter
class enables us to easily create
the routing object which would create the output we need for our
get_metadata_routing
.
In order to understand how aliases work in meta-estimators, imagine our
meta-estimator inside another one:
meta_meta_est = MetaClassifier(estimator=meta_est).fit(
X, y, aliased_sample_weight=my_weights
)
Received sample_weight of length = 100 in ExampleClassifier.
In the above example, this is how the fit
method of meta_meta_est
will call their sub-estimator’s fit
methods:
# user feeds `my_weights` as `aliased_sample_weight` into `meta_meta_est`:
meta_meta_est.fit(X, y, aliased_sample_weight=my_weights):
...
# the first sub-estimator (`meta_est`) expects `aliased_sample_weight`
self.estimator_.fit(X, y, aliased_sample_weight=aliased_sample_weight):
...
# the second sub-estimator (`est`) expects `sample_weight`
self.estimator_.fit(X, y, sample_weight=aliased_sample_weight):
...
Simple Pipeline#
A slightly more complicated use-case is a meta-estimator resembling a
Pipeline
. Here is a meta-estimator, which accepts a
transformer and a classifier. When calling its fit
method, it applies the
transformer’s fit
and transform
before running the classifier on the
transformed data. Upon predict
, it applies the transformer’s transform
before predicting with the classifier’s predict
method on the transformed
new data.
classSimplePipeline(ClassifierMixin , BaseEstimator ):
def__init__(self, transformer, classifier):
self.transformer = transformer
self.classifier = classifier
defget_metadata_routing(self):
router = (
MetadataRouter (owner=self.__class__.__name__)
# We add the routing for the transformer.
.add(
transformer=self.transformer,
method_mapping=MethodMapping ()
# The metadata is routed such that it retraces how
# `SimplePipeline` internally calls the transformer's `fit` and
# `transform` methods in its own methods (`fit` and `predict`).
.add(caller="fit", callee="fit")
.add(caller="fit", callee="transform")
.add(caller="predict", callee="transform"),
)
# We add the routing for the classifier.
.add(
classifier=self.classifier,
method_mapping=MethodMapping ()
.add(caller="fit", callee="fit")
.add(caller="predict", callee="predict"),
)
)
return router
deffit(self, X, y, **fit_params):
routed_params = process_routing (self, "fit", **fit_params)
self.transformer_ = clone(self.transformer).fit(
X, y, **routed_params.transformer.fit
)
X_transformed = self.transformer_.transform(
X, **routed_params.transformer.transform
)
self.classifier_ = clone(self.classifier).fit(
X_transformed, y, **routed_params.classifier.fit
)
return self
defpredict(self, X, **predict_params):
routed_params = process_routing (self, "predict", **predict_params)
X_transformed = self.transformer_.transform(
X, **routed_params.transformer.transform
)
return self.classifier_.predict(
X_transformed, **routed_params.classifier.predict
)
Note the usage of MethodMapping
to
declare which methods of the child estimator (callee) are used in which
methods of the meta estimator (caller). As you can see, SimplePipeline
uses
the transformer’s transform
and fit
methods in fit
, and its
transform
method in predict
, and that’s what you see implemented in
the routing structure of the pipeline class.
Another difference in the above example with the previous ones is the usage
of process_routing
, which processes the input
parameters, does the required validation, and returns the routed_params
which we had created in previous examples. This reduces the boilerplate code
a developer needs to write in each meta-estimator’s method. Developers are
strongly recommended to use this function unless there is a good reason
against it.
In order to test the above pipeline, let’s add an example transformer.
classExampleTransformer(TransformerMixin , BaseEstimator ):
deffit(self, X, y, sample_weight=None):
check_metadata(self, sample_weight=sample_weight)
return self
deftransform(self, X, groups=None):
check_metadata(self, groups=groups)
return X
deffit_transform(self, X, y, sample_weight=None, groups=None):
return self.fit(X, y, sample_weight).transform(X, groups)
Note that in the above example, we have implemented fit_transform
which
calls fit
and transform
with the appropriate metadata. This is only
required if transform
accepts metadata, since the default fit_transform
implementation in TransformerMixin
doesn’t pass metadata to
transform
.
Now we can test our pipeline, and see if metadata is correctly passed around.
This example uses our SimplePipeline
, our ExampleTransformer
, and our
RouterConsumerClassifier
which uses our ExampleClassifier
.
pipe = SimplePipeline(
transformer=ExampleTransformer()
# we set transformer's fit to receive sample_weight
.set_fit_request(sample_weight=True)
# we set transformer's transform to receive groups
.set_transform_request(groups=True),
classifier=RouterConsumerClassifier(
estimator=ExampleClassifier()
# we want this sub-estimator to receive sample_weight in fit
.set_fit_request(sample_weight=True)
# but not groups in predict
.set_predict_request(groups=False),
)
# and we want the meta-estimator to receive sample_weight as well
.set_fit_request(sample_weight=True),
)
pipe.fit(X, y, sample_weight=my_weights, groups=my_groups).predict(
X[:3], groups=my_groups
)
Received sample_weight of length = 100 in ExampleTransformer.
Received groups of length = 100 in ExampleTransformer.
Received sample_weight of length = 100 in RouterConsumerClassifier.
Received sample_weight of length = 100 in ExampleClassifier.
Received groups of length = 100 in ExampleTransformer.
groups is None in ExampleClassifier.
array([1., 1., 1.])
Deprecation / Default Value Change#
In this section we show how one should handle the case where a router becomes
also a consumer, especially when it consumes the same metadata as its
sub-estimator, or a consumer starts consuming a metadata which it wasn’t in
an older release. In this case, a warning should be raised for a while, to
let users know the behavior is changed from previous versions.
classMetaRegressor(MetaEstimatorMixin , RegressorMixin , BaseEstimator ):
def__init__(self, estimator):
self.estimator = estimator
deffit(self, X, y, **fit_params):
routed_params = process_routing (self, "fit", **fit_params)
self.estimator_ = clone(self.estimator).fit(X, y, **routed_params.estimator.fit)
defget_metadata_routing(self):
router = MetadataRouter (owner=self.__class__.__name__).add(
estimator=self.estimator,
method_mapping=MethodMapping ().add(caller="fit", callee="fit"),
)
return router
As explained above, this is a valid usage if my_weights
aren’t supposed
to be passed as sample_weight
to MetaRegressor
:
reg = MetaRegressor(estimator=LinearRegression ().set_fit_request(sample_weight=True))
reg.fit(X, y, sample_weight=my_weights)
Now imagine we further develop MetaRegressor
and it now also consumes
sample_weight
:
classWeightedMetaRegressor(MetaEstimatorMixin , RegressorMixin , BaseEstimator ):
# show warning to remind user to explicitly set the value with
# `.set_{method}_request(sample_weight={boolean})`
__metadata_request__fit = {"sample_weight": metadata_routing.WARN}
def__init__(self, estimator):
self.estimator = estimator
deffit(self, X, y, sample_weight=None, **fit_params):
routed_params = process_routing (
self, "fit", sample_weight=sample_weight, **fit_params
)
check_metadata(self, sample_weight=sample_weight)
self.estimator_ = clone(self.estimator).fit(X, y, **routed_params.estimator.fit)
defget_metadata_routing(self):
router = (
MetadataRouter (owner=self.__class__.__name__)
.add_self_request(self)
.add(
estimator=self.estimator,
method_mapping=MethodMapping ().add(caller="fit", callee="fit"),
)
)
return router
The above implementation is almost the same as MetaRegressor
, and
because of the default request value defined in __metadata_request__fit
there is a warning raised when fitted.
with warnings.catch_warnings (record=True) as record:
WeightedMetaRegressor(
estimator=LinearRegression ().set_fit_request(sample_weight=False)
).fit(X, y, sample_weight=my_weights)
for w in record:
print(w.message)
Received sample_weight of length = 100 in WeightedMetaRegressor.
Support for sample_weight has recently been added to this class. To maintain backward compatibility, it is ignored now. Using `set_fit_request(sample_weight={True, False})` on this method of the class, you can set the request value to False to silence this warning, or to True to consume and use the metadata.
When an estimator consumes a metadata which it didn’t consume before, the
following pattern can be used to warn the users about it.
classExampleRegressor(RegressorMixin , BaseEstimator ):
__metadata_request__fit = {"sample_weight": metadata_routing.WARN}
deffit(self, X, y, sample_weight=None):
check_metadata(self, sample_weight=sample_weight)
return self
defpredict(self, X):
return np.zeros (shape=(len(X)))
with warnings.catch_warnings (record=True) as record:
MetaRegressor(estimator=ExampleRegressor()).fit(X, y, sample_weight=my_weights)
for w in record:
print(w.message)
sample_weight is None in ExampleRegressor.
Support for sample_weight has recently been added to this class. To maintain backward compatibility, it is ignored now. Using `set_fit_request(sample_weight={True, False})` on this method of the class, you can set the request value to False to silence this warning, or to True to consume and use the metadata.
At the end we disable the configuration flag for metadata routing:
Third Party Development and scikit-learn Dependency#
As seen above, information is communicated between classes using
MetadataRequest
and
MetadataRouter
. It is strongly not advised,
but possible to vendor the tools related to metadata-routing if you strictly
want to have a scikit-learn compatible estimator, without depending on the
scikit-learn package. If all of the following conditions are met, you do NOT
need to modify your code at all:
your estimator inherits from BaseEstimator
the parameters consumed by your estimator’s methods, e.g. fit
, are
explicitly defined in the method’s signature, as opposed to being
*args
or *kwargs
.
your estimator does not route any metadata to the underlying objects, i.e.
it’s not a router.
Total running time of the script: (0 minutes 0.046 seconds)
Related examples
Gallery generated by Sphinx-Gallery