process_routing#
- sklearn.utils.metadata_routing.process_routing(_obj, _method, /, **kwargs)[source]#
Validate and route metadata.
This function is used inside a router’s method, e.g. fit, to validate the metadata and handle the routing.
Assuming this signature of a router’s fit method:
fit(self, X, y, sample_weight=None, **fit_params), a call to this function would be:process_routing(self, "fit", sample_weight=sample_weight, **fit_params).Internally, the function uses the router’s
MetadataRouterobject (as returned by a call to itsget_metadata_routingmethod) to validate per method that the routed metadata had been requested by the underlying estimator, and extracts a mapping of the given metadata to the requested metadata based on the routing information defined by theMetadataRouter.Note that if routing is not enabled and
kwargsis empty, then it returns an empty routing whereprocess_routing(...).ANYTHING.ANY_METHODis always an empty dictionary.The output of this function is a
Bunchthat has a key for each consuming object and those hold keys for their consuming methods, which then contain keys for the metadata which should be routed to them.Read more on developing custom estimators that can route metadata in the Metadata Routing Developing Guide.
Added in version 1.3.
- Parameters:
- _objobject
An object implementing
get_metadata_routing. Typically a meta-estimator.- _methodstr
The name of the router’s method in which this function is called.
- **kwargsdict
Metadata to be routed.
- Returns:
- routed_paramsBunch
A
Bunchof the form{"object_name": {"method_name": {metadata: value}}}which can be used to pass the required metadata to corresponding methods or corresponding child objects. The object names are those defined inobj.get_metadata_routing().
Examples
>>> import numpy as np >>> from sklearn import set_config >>> from sklearn.utils.metadata_routing import process_routing >>> from sklearn.linear_model import Ridge >>> from sklearn.feature_selection import SelectFromModel >>> set_config(enable_metadata_routing=True) >>> process_routing( ... SelectFromModel(Ridge().set_fit_request(sample_weight=True)), ... "fit", ... sample_weight=np.array([1, 1, 2]), ... ) {'estimator': {'fit': {'sample_weight': array([1, 1, 2])}}} >>> set_config(enable_metadata_routing=False)
Gallery examples#
Developing Estimators Compliant with Metadata Routing