sklearn.utils.metadata_routing.MetadataRouter

class sklearn.utils.metadata_routing.MetadataRouter(owner)[source]

Stores and handles metadata routing for a router object.

This class is used by router objects to store and handle metadata routing. Routing information is stored as a dictionary of the form {"object_name": RouteMappingPair(method_mapping, routing_info)}, where method_mapping is an instance of MethodMapping and routing_info is either a MetadataRequest or a MetadataRouter instance.

New in version 1.3.

Parameters:
ownerstr

The name of the object to which these requests belong.

Methods

add(*, method_mapping, **objs)

Add named objects with their corresponding method mapping.

add_self_request(obj)

Add self (as a consumer) to the routing.

consumes(method, params)

Check whether the given parameters are consumed by the given method.

route_params(*, caller, params)

Return the input parameters requested by child objects.

validate_metadata(*, method, params)

Validate given metadata for a method.

add(*, method_mapping, **objs)[source]

Add named objects with their corresponding method mapping.

Parameters:
method_mappingMethodMapping or str

The mapping between the child and the parent’s methods. If str, the output of from_str is used.

**objsdict

A dictionary of objects from which metadata is extracted by calling get_routing_for_object on them.

Returns:
selfMetadataRouter

Returns self.

add_self_request(obj)[source]

Add self (as a consumer) to the routing.

This method is used if the router is also a consumer, and hence the router itself needs to be included in the routing. The passed object can be an estimator or a MetadataRequest.

A router should add itself using this method instead of add since it should be treated differently than the other objects to which metadata is routed by the router.

Parameters:
objobject

This is typically the router instance, i.e. self in a get_metadata_routing() implementation. It can also be a MetadataRequest instance.

Returns:
selfMetadataRouter

Returns self.

consumes(method, params)[source]

Check whether the given parameters are consumed by the given method.

New in version 1.4.

Parameters:
methodstr

The name of the method to check.

paramsiterable of str

An iterable of parameters to check.

Returns:
consumedset of str

A set of parameters which are consumed by the given method.

route_params(*, caller, params)[source]

Return the input parameters requested by child objects.

The output of this method is a bunch, which includes the inputs for all methods of each child object that are used in the router’s caller method.

If the router is also a consumer, it also checks for warnings of self’s/consumer’s requested metadata.

Parameters:
callerstr

The name of the method for which the parameters are requested and routed. If called inside the fit method of a router, it would be "fit".

paramsdict

A dictionary of provided metadata.

Returns:
paramsBunch

A Bunch of the form {"object_name": {"method_name": {prop: value}}} which can be used to pass the required metadata to corresponding methods or corresponding child objects.

validate_metadata(*, method, params)[source]

Validate given metadata for a method.

This raises a TypeError if some of the passed metadata are not understood by child objects.

Parameters:
methodstr

The name of the method for which the parameters are requested and routed. If called inside the fit method of a router, it would be "fit".

paramsdict

A dictionary of provided metadata.

Examples using sklearn.utils.metadata_routing.MetadataRouter

Metadata Routing

Metadata Routing